-
Notifications
You must be signed in to change notification settings - Fork 2
/
action.js
430 lines (399 loc) · 11.1 KB
/
action.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
'use strict';
/* Extensions state vars */
var ext_browser;
var ext_firefox=false;
if(typeof chrome !== 'undefined'){
ext_browser=chrome;
}
else{
if(typeof browser !== 'undefined'){
ext_browser=browser;
ext_firefox=true;
}
}
var current_user='';
var account={
regular_key:'',
memo_key:'',
active_key:'',
};
var settings={
energy_step:20,
award_energy:200,
dark:false,
lang:'en',
};
var rules={};
var users={};
var state={};
var current_energy=0;
var ltmp_arr={};
var available_langs={
'en':'English',
'ru':'Русский',
};
var langs_arr={
'en-gb':'en',
'en-us':'en',
'en':'en',
'ru-ru':'ru',
'ru':'ru',
};
function get_state(callback){
if(typeof callback === 'undefined'){callback=function(){};}
ext_browser.runtime.sendMessage({get_state:true},function(response){
console.log('get_state response',response);
if(null===response){
return;
}
if(false!==response.decoded){
state=response.state;
users=state.users;
current_user=state.current_user;
if(typeof users !== 'undefined'){
if(typeof users[current_user] !== 'undefined'){
account=users[current_user];
}
}
if(typeof state.settings !== 'undefined'){
settings=state.settings;
}
if(typeof state.rules === 'undefined'){
rules={};
}
else{
rules=state.rules;
}
ltmp_arr=window['ltmp_'+settings.lang+'_arr'];
if(typeof localStorage['current_energy'] !== 'undefined'){
current_energy=localStorage['current_energy'];
}
callback(true);
}
else{
if(typeof localStorage['lang'] !== 'undefined'){
settings.lang=localStorage['lang'];
}
ltmp_arr=window['ltmp_'+settings.lang+'_arr'];
if(typeof localStorage['dark'] !== 'undefined'){
settings.dark=localStorage['dark'];
if(typeof settings.dark == 'string'){
if('false'==settings.dark){
settings.dark=false;
}
else{
settings.dark=true;
}
}
}
callback(false);
}
});
}
var action={};
var energy=100;
var options=function(){
if(ext_browser.runtime.openOptionsPage){
ext_browser.runtime.openOptionsPage();
}
else{
window.open(ext_browser.runtime.getURL('options.html'));
}
window.close();
}
var unlock_action=function(){
let password=$('.decode_password').val();
if(''!=password){
$('.decode_password').removeClass('error');
ext_browser.runtime.sendMessage({encode_state:true,password:password},function(response){
console.log('unlock_action response',response);
if(null===response){
return;
}
get_state(function(status){
console.log('get_state return',status);
if(settings.dark){
$('body').addClass('dark');
}
else{
$('body').removeClass('dark');
}
if(status){
easter_egg();
main_app();
}
else{
$('.decode_password').addClass('error');
}
resize_app();
});
});
}
}
var need_encode=function(){
if(turbo_cat){
$('.logo img').attr('src','/images/turbo-cat/password.png');
}
let result='';
result+='<p class="red">'+ltmp_arr.need_encode_state+'<p>';
result+='<hr>'+ltmp(ltmp_arr.unlock_form,{icon:ltmp_icons.icon_unlock});
$('.info').html(result);
$('.action').html('');
$('.unlock-action').off('click',unlock_action);
$('.unlock-action').on('click',unlock_action);
$('.decode_password').off('keypress');
$('.decode_password').on('keypress',function(e){
if(e.keyCode == 13){
let event=document.createEvent('MouseEvents');
event.initEvent('click',true,true);
$('.unlock-action')[0].dispatchEvent(event);
}
});
}
var need_configure=function(){
let result='';
result+='<hr>';
result+='<a class="options" href="#">'+ltmp_arr.configure_account+'</a>';
$('.info').html(result);
$('.options').off('click');
$('.options').on('click',options);
}
var assigned_account=function(){
let result='';
result+='<hr>';
result+='<span class="current-account">'+ltmp_arr.used_account+': <b>'+current_user+'</b></span>';
$('.info').html(result);
}
function escape_html(text) {
var map = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
return text.replace(/[&<>"']/g,function(m){return map[m];});
}
function select_energy_view(){
let result='';
let min=parseInt($('.select_energy').attr('data-min'));
let default_value=parseInt($('.select_energy').attr('data-default'));
let max=parseInt($('.select_energy').attr('data-max'));
let step=parseInt($('.select_energy').attr('data-step'));
let limit=parseInt($('.select_energy').attr('data-limit'));
if(default_value>max){
default_value=max;
}
for(let i=min;i<=max;i+=step){
let i_class='energy_active';
if(i>limit){
i_class='energy_inactive';
}
result+='<div class="'+i_class+'" rel="'+i+'"></div>';
}
$('.select_energy').html(result);
$('.energy_active').off('click');
$('.energy_active').on('click',function(){
$('.energy_active').removeClass('selected');
energy=$(this).attr('rel');
for(let j=min;j<=energy;j+=step){
$('.energy_active[rel="'+j+'"]').addClass('selected');
}
$('.spend_energy').html((energy/100)+'%');
});
$('.energy_inactive').off('click');
$('.energy_inactive').on('click',function(){
$('.energy_active').removeClass('selected');
energy=limit;
for(let j=min;j<=energy;j+=step){
$('.energy_active[rel="'+j+'"]').addClass('selected');
}
$('.spend_energy').html((energy/100)+'%');
});
var event=document.createEvent('MouseEvents');
event.initEvent('click',true,true);
if(default_value>limit){
$('.energy_inactive[rel="'+default_value+'"]')[0].dispatchEvent(event);
}
else{
$('.energy_active[rel="'+default_value+'"]')[0].dispatchEvent(event);
}
}
function refuse_action(){
action.vizonator_action=true;
action.refuse=true;
action.award=false;
action.energy=energy;
ext_browser.runtime.sendMessage(action,function(response){
console.log('refuse_action response',response);
if(null===response){
return;
}
if(response){
action_executed=true;
window.close();
}
});
}
var action_executed=false;
window.onbeforeunload=function(){
if(!action_executed){
refuse_action();
}
}
function bind_actions(){
$('.refuse-action').off('click');
$('.refuse-action').on('click',function(){
if(!$('.refuse-action').hasClass('disabled')){
$('.award-action').addClass('disabled');
$('.refuse-action').addClass('disabled');
refuse_action();
}
});
$('.award-action').off('click');
$('.award-action').on('click',function(){
if(!$('.award-action').hasClass('disabled')){
$('.award-action').addClass('disabled');
$('.refuse-action').addClass('disabled');
action.vizonator_action=true;
action.award=true;
action.refuse=false;
action.energy=energy;
if($('.trust input[name="save"]').prop("checked")){
action.save=true;
}
ext_browser.runtime.sendMessage(action,function(response){
console.log('.award-action response',response);
if(null===response){
return;
}
if(response){
action_executed=true;
window.close();
}
});
}
});
}
function action_info(){
let hash=window.location.hash.substring(1);
if(''!=hash){
hash=decodeURI(hash);
action=JSON.parse(hash);
console.log(action);
let result='';
result+='<p class="caption">'+ltmp_arr.award_action+' '+escape_html(action.login)+'</p>';
result+='<p>'+ltmp_arr.memo_caption+':</p><span class="gray monospace limit-height">'+escape_html(action.memo)+'</span>';
if(action.sequence>0){
result+='<p class="gray">'+ltmp_arr.sequence_caption+': '+parseInt(action.sequence)+'</p>';
}
if(action.beneficiaries.length>0){
result+='<p>'+ltmp_arr.beneficiaries_caption+':</p><span class="gray monospace limit-height">'+escape_html(JSON.stringify(action.beneficiaries))+'</span>';
}
//result+='<p class="gray">Action id: '+action.id+'</p>';
//result+='<p class="gray">Tab id: '+action.tab_id+'</p>';
result+='<p class="orange">'+ltmp_arr.origin_caption+': '+escape_html(action.origin)+'</p>';
if(''!=current_user){
result+='<p class="blue">'+ltmp_arr.spend_energy_caption+': <span class="spend_energy">…</span></p>';
result+='<p><div class="select_energy" data-min="'+settings.energy_step+'" data-default="'+settings.award_energy+'" data-step="'+settings.energy_step+'" data-max="'+(Math.min(settings.energy_step*100,10000))+'" data-limit="'+current_energy+'"></div></p>';
result+='<div class="text-right trust"><label class="unselectable"><input type="checkbox" name="save"> — '+ltmp_arr.save_rule_caption+'</label></div>';
result+='<div class="text-right">';
result+='<a class="refuse-action button negative unselectable"><span class="icon"><img src="images/cross.svg"></span> '+ltmp_arr.refuse_caption+'</a>';
result+='<a class="award-action button unselectable"><span class="icon"><img src="images/check.svg"></span> '+ltmp_arr.award_action+'</a>';
result+='</div>';
}
$('.action').html(result);
if(''!=current_user){
select_energy_view();
bind_actions();
}
}
else{
$('.action').html(ltmp_arr.operation_error);
}
}
function main_app(){
if(settings.dark){
$('body').addClass('dark');
}
else{
$('body').removeClass('dark');
}
if(''==current_user){
need_configure();
}
else{
assigned_account();
}
action_info();
}
function is_retina(){
return ((window.matchMedia && (window.matchMedia('only screen and (min-resolution: 192dpi), only screen and (min-resolution: 2dppx), only screen and (min-resolution: 75.6dpcm)').matches || window.matchMedia('only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (-o-min-device-pixel-ratio: 2/1), only screen and (min--moz-device-pixel-ratio: 2), only screen and (min-device-pixel-ratio: 2)').matches)) || (window.devicePixelRatio && window.devicePixelRatio >= 2)) && /(Mac OS|iPad|iPhone|iPod)/g.test(navigator.userAgent);
}
function resize_app(){
let fix=1;
let fix_retina_width=0;
let fix_retina_height=0;
if(is_retina()){
fix=0.5;
fix_retina_width=-14;
fix_retina_height=-10;
}
let height_addon=36;
if(ext_firefox){
height_addon=40;
}
let is_linux=navigator.userAgent.toLowerCase().indexOf('linux')>-1;
if(is_linux){
ext_browser.windows.update(ext_browser.windows.WINDOW_ID_CURRENT, {
width: parseInt($('body').outerWidth()),
height: parseInt($('body').outerHeight()),
});
}
else{
ext_browser.windows.update(ext_browser.windows.WINDOW_ID_CURRENT, {
width: parseInt(fix*window.devicePixelRatio*(document.body.clientWidth+16+fix_retina_width)),
height: parseInt(fix*window.devicePixelRatio*(document.body.clientHeight+height_addon+fix_retina_height)),
});
}
}
var turbo_cat=false;
var turbo_cat_images=[
'upside.png',
'glasses.png',
'lets-start.png',
'victory.png',
'wink.png',
'like.png',
];
if(11==new Date().getMonth()){//december
turbo_cat_images.push('present.png');
}
function easter_egg(){
let random=Math.random();
if((random<0.1)||turbo_cat){//10%
turbo_cat=true;
let random_image=Math.floor(Math.random() * turbo_cat_images.length);
$('.logo').addClass('easter-egg');
$('.logo img').attr('src','/images/turbo-cat/'+turbo_cat_images[random_image]);
}
}
$(function(){
easter_egg();
get_state(function(status){
console.log('get_state return',status);
if(settings.dark){
$('body').addClass('dark');
}
else{
$('body').removeClass('dark');
}
if(status){
main_app();
}
else{
need_encode();
}
resize_app();
});
});