-
Notifications
You must be signed in to change notification settings - Fork 3
/
background.js
1617 lines (1549 loc) · 59.5 KB
/
background.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
let log=console.log;
function clearString(str) {
return str.length < 12 ? str : (' ' + str).slice(1);
}
let rps = 0;
if (0) setInterval(e=>{
if (rps >= 10) log('RPS:',rps);
rps -= 10;
if (rps < 0) rps = 0;
},1000);
function getURL(url,callback, on_fail) {
//console.log('URL:',url);
let xhr = new XMLHttpRequest();
xhr.timeout = 13000;
xhr.onreadystatechange = function() {
if (this.readyState == 4) {
//console.log('success');
if (this.status != 200) {
console.log("error", this.status);
if (on_fail) on_fail();
return;
}
//window[back] = xhr.responseText;
if(callback)callback(xhr.responseText);
}
};
//xhr.ontimeout = function() {
//console.log('timeout');
//}
xhr.open("GET", url, true);
xhr.send(); rps++;
}
let leadingZero = function(num) {
let s = "0" + num;
if (s.length > 2) return num;
return s;
}
let logg = function() { //Выводим лог с меткой времени.
let dt = new Date();
log(dt.getHours()+':'+leadingZero(dt.getMinutes())+':'+leadingZero(dt.getSeconds()),...arguments);
}
const DAY_TIME = 24*60*60*1000;
const db_clean_steps = [7, 3, 2, 1, 0.5, 0.2];
function clean_db(timeout_days) {
const timeout = timeout_days * DAY_TIME;
//remove pending status
for(let id in db.user) {
if (!db.user[id]) {
delete db.user[id];
continue;
}
delete db.user[id].solutions_pending;
delete db.user[id].karma_pending;
}
//remove users
let now = (new Date()).getTime();
for(let id in db.user) {
let user = db.user[id];
if (!(now - user.update_time < timeout)) delete db.user[id]; // n days
}
//remove questions
for(let id in db.question) {
if (tracker_q_ids[id]) continue;
let q = db.question[id];
//ignore subscribtions
if (q.sub) {
if (!(now - q.ut < 400 * 24 * 60 * 60000)) delete db.question[id]; // 1 year
continue;
}
//ut means update_time
if (!(now - q.ut < timeout)) delete db.question[id]; // n days
}
}
function clean_db_users() {
db.user = {};
}
const MAX_DB_TIMEOUT = 60000;
let saveDB_timer;
let db_saved_called;
function saveDB(no_pause) {
let now = (new Date()).getTime();
db_saved_called = db_saved_called || now;
if (saveDB_timer !== undefined) clearTimeout(saveDB_timer);
function saveDB_Now() {
//let check1 = performance.now();
for(let i=0;i<db_clean_steps.length;i++) {
clean_db(db_clean_steps[i]);
try {
localStorage.db = JSON.stringify(db);
break;
} catch(e) {
console.log("Can't save DB");
}
}
db_saved_called = undefined;
//let check2 = performance.now();
//console.log('DB_SAVED!',(check2-check1)+'мс');
}
if ((now - db_saved_called > MAX_DB_TIMEOUT)||no_pause) saveDB_Now();
else saveDB_timer = setTimeout(saveDB_Now,15000);
}
const C_MONTHS = {
['января']: 'Jan', ['февраля']: 'Feb', ['марта']: 'Mar', ['апреля']: 'Apr',
['мая']: 'May', ['июня']: 'Jun', ['июля']: 'Jul', ['августа']: 'Aug',
['сентября']: 'Sep', ['октября']: 'Oct', ['ноября']: 'Nov', ['декабря']: 'Dec',
}
if (localStorage.cut_karma === undefined) localStorage.cut_karma = 1;
//
function updateUser(nickname,timeout) {
//console.log('update:',nickname);
if (!nickname) return console.log('No nickname!'); //impossible
let user = db.user[nickname];
if (!user) user = db.user[nickname] = {}; //impossible
user.nickname = nickname;
let now = (new Date()).getTime();
let need_update = false;
if (!(now - user.update_time < (timeout || 24*60*60*1000))) {
need_update = true;
user.update_time = now; //error. not updated yet.
}
//questions
if (need_update || user.solutions === undefined && !user.solutions_pending) {
user.solutions_pending = true;
saveDB();
let old_perc_sol_marks = localStorage.old_perc_sol_marks === '1';
getURL('https://qna.habr.com/user/'+nickname+'/questions',(text)=>{ //log('User:',nickname);
delete user.solutions_pending;
//solutions
//let r = /\s*(\d+)\s{8}<\/div>/g; //todo: very very bad, need better algorytm!
let r = /<li class="content-list__item" role="content-list_item"([\s\S]*?)<\/div>\s*<\/li>/g;
let a;
let sum = 0, cnt = 0, score = 0; //кол-во нормальных вопросов, кол-во решений, сумма очков
while ((a = r.exec(text)) !== null) {
let t = a[1];
//вопросы без ответов не учитываются
let sol = t.match(/\s*(\d+)\s{8}<\/div>/);
if (!sol) { log('user parse error',nickname,t); break; }
if (sol[1] === '0') continue;
//решения считаем как 1 очко
let is_sol = t.match(/icon_svg icon_check/);
if (is_sol) { sum++; cnt++; score++; continue; }
//сегодняшние вопросы не учитываются
let tm = t.match(/\s*(.*?)\s*<\/time>/);
if (!tm) { log('user parse error',nickname,t); break; }
if (tm[1] == 'только что' || tm[1].indexOf('минут') !== -1 || tm[1].indexOf('час') !== -1) continue;
sum++;
//проверка сложности. Простые +0, Средние +0.25, Сложные +0.5 очков.
let complex = old_perc_sol_marks ? 0 : t.match(/<span class="question__complexity-text">\s*(.*?)\s*<\/span>/);
if (complex) { complex = complex[1] === 'Средний' ? 0.25 : complex[1] === 'Сложный' ? 0.5 : 0; }
else complex = 0; //log('complex',complex);
score += complex;
}
freeRegExp(); //log('sum=',sum);
if (!sum) user.solutions = '-1'; //impossible
else {
if (cnt < 4) {
if (cnt == 0) score = 0;
else {
let koef = cnt == 1 ? 0.5 : cnt == 2 ? 0.75 : 0.9; //максимальный понижающий коэффициент для доп. очков
let max = 20 - cnt; //максимально свободных вопросов
let free = sum - cnt; //свободные вопросы
koef = (max - free) / max * (1 - koef) + koef; //корректируем коэффициент от количества свободных вопросов (1 .. 0.75 для 2 решений).
score = cnt + (score - cnt) * koef; //корректируем довесок очков.
}
}
user.solutions = Math.floor( score / sum * 100 + 0.5); //log(user.solutions+'%', cnt, sum)
}
//stats
a = text.match(/<a href="\/help\/rating" class="mini-counter__rating">[\s\S]*?(\d+)[\s]*<\/a>[\s\S]*?<li class="inline-list__item inline-list__item_bordered">[\s\S]*?<meta itemprop="interactionCount"[\s\S]*?<div class="mini-counter__count">(\d+)[\s\S]*?<div class="mini-counter__count">(\d+)[\s\S]*?<div class="mini-counter__count mini-counter__count-solutions">(\d+)/);
if (a) {
let contribution = a[1]-0; //вклад
if (contribution > 0) user.con = contribution;
user.cnt_q = a[2]-0; //questions
user.cnt_a = a[3]-0; //answers
user.cnt_s = a[4]-0; //perc solutions
} else console.log("Stats not found, user:",nickname);
});
}
//karma & stats from habr
if (need_update || user.karma === undefined && !user.karma_pending) {
user.karma_pending = true;
saveDB();
getURL('https://habr.com/users/'+nickname+'/',(text)=>{
delete user.karma_pending;
let a = text.match(/<div class="stacked-counter__value[^>]*>(.*)<\/div>\s*<div class="stacked-counter__label">Карма<\/div>/);
if (!a) a = text.match(/<div class="stacked-counter__value[^>]*>(.*)<\/div>\s*<div class="stacked-counter__label">Karma<\/div>/);
if (a) {
user.karma = a[1].replace(',','.').replace('–','-');
let karma = parseFloat(user.karma);
if (!isNaN(karma)) { // !!!
if (localStorage.cut_karma == 1) karma = Math.floor(karma);
user.karma = karma;
} else {
console.log('Ошибка кармы:',nickname,user.karma);
user.karma = clearString(user.karma);
}
} else {
user.karma = "read-only";
//console.log('Karma not found, user:',nickname);
}
a = text.match(/<span class="tabs-menu__item-counter tabs-menu__item-counter_total" title="Публикации: (\d+)">/);
if (!a) a = text.match(/<span class="tabs-menu__item-counter tabs-menu__item-counter_total" title="Posts: (\d+)">/);
if (a) {
user.stat_pub = parseInt(a[1]);
}
a = text.match(/<span class="tabs-menu__item-counter tabs-menu__item-counter_total" title="Комментарии: (\d+)">/);
if (!a) a = text.match(/<span class="tabs-menu__item-counter tabs-menu__item-counter_total" title="Comments: (\d+)">/);
if (a) {
user.stat_comment = parseInt(a[1]);
}
a = text.match(/Зарегистрирован<\/span>[\s\n]*<span class="defination-list__value">(.*?)<\/span>/);
if (!a) a = text.match(/Registered<\/span>[\s\n]*<span class="defination-list__value">(.*?)<\/span>/);
if (a) {
//console.log('Time:',a[1]);
let date_str = a[1].replace(' г.','');
for(let k in C_MONTHS) if (date_str.indexOf(k) !== -1) {
//console.log('found',date_str.indexOf(k));
date_str = date_str.replace(k, C_MONTHS[k]);
break;
}
let date = Date.parse(date_str);
if (!date) console.log('Error parsing date:',clearString(a[1]),date_str);
else {
user.reg = Math.floor(date / 1000);
}
}
}, ()=>{ //todo: такой статус нужен лишь при ответе 404
delete user.karma_pending; user.karma = 'n';
});
}
}
function parseTags(txt) {
let tags = {};
let r = /<a href="https?:\/\/qna\.habr\.com\/tag\/([^">]*)">\s*([\S ]+?)\s*<\/a>/g
let a = r.exec(txt);
while (a) {
tags[clearString(a[1])] = clearString(a[2]);
a = r.exec(txt);
}
return tags;
}
//todo: удалить tracker_q_ids
function analyzeQuestion(question_id, now, is_fresh) { //logg('Analize!')
if (question_id<10) return console.log("q_id too low:",question_id); //throw "q_id too low: "+question_id;
let add_e = !is_fresh ? '?e='+Math.floor(Math.random()*6566811 + 1000000) : '';
if (!now) now = (new Date()).getTime();
let q = {is_pending:true, ut:now, cnt_a:0};
let qq = db.question[question_id];
if (qq) {
if(qq.cnt_a) q.cnt_a = qq.cnt_a;
//q.user_id = db.question[question_id].user_id;
}
db.question[question_id] = q;
saveDB();
getURL('https://qna.habr.com/q/' + question_id + add_e, function(text) {
//get title
const index_title_str = '<h1 class="question__title" itemprop="name ">';
let index_title = text.indexOf(index_title_str);
if (index_title > -1) {
let index_title2 = text.indexOf("</h1>\n",index_title);
let txt = text.substring(index_title + index_title_str.length + 1, index_title2).trim();
if (txt) q.t = clearString(txt); //title!
}
//logg('Analized',q.t);
//get user name
let index_name = text.indexOf('<meta itemprop="name" content="');
if (index_name > -1) {
let index_name2 = text.indexOf('</span>', index_name);
let txt = text.substr(index_name, index_name2 - index_name);
let m = txt.match(/<meta itemprop=\"name\" content=\"([^"]*)\">/);
let user_name = m && m[1] || '???';
//console.log('user_name',user_name);
m = txt.match(/<meta itemprop=\"alternateName\" content=\"([^"]*)\">/);
let user_nickname = m && m[1];
//console.log('user_nickname',user_nickname);
if (user_nickname) {
user_nickname=clearString(user_nickname);
delete q.is_pending;
q.user_id = user_nickname;
let user = db.user[user_nickname];
if (!user) user = db.user[user_nickname] = {};
user.name = clearString(user_name);
user.nickname = user_nickname;
updateUser(user_nickname);
}
}
//get question tags
let index_tags = text.indexOf('<ul class="tags-list">');
if (index_tags > -1) {
let index_tags2 = text.indexOf('</ul>', index_tags || 0);
let txt = text.substr(index_tags, index_tags2 - index_tags);
q.tags = parseTags(txt);
}
//check subscribtions
if (text.indexOf('"btn btn_subscribe btn_active"') > -1) q.sb = 1;
else {
delete q.sb;
}
//count answers
if(localStorage.check_online==1 && localStorage.is_widget==1 && localStorage.enable_notify_action==1 && localStorage.enable_notifications==1){
q.cnt_a = (text.match(/class="answer__text/g)||[]).length;
let r_desc = /<a class="user-summary__avatar"/g;
let r_img = /(?:<img src="https:\/\/habrastorage\.org\/([^"]+)"|<(svg) class)/g;
let r_name = /<meta itemprop="alternateName" content="([^"]*)">/g;
let r_time = /(?:(.*?)<\/time>|<textarea )/g;
let m;
let u = {};
while (m=r_desc.exec(text)) {
r_img.lastIndex = r_desc.lastIndex;
if(!(m=r_img.exec(text))) {console.log('Error in img'); break;}
let o={};
if(m[1])o.img=clearString(m[1]);
r_name.lastIndex = r_img.lastIndex;
if(!(m=r_name.exec(text))) {console.log('Error in name'); break;}
let nick = clearString(m[1]);
o.nick=nick;
r_time.lastIndex = r_name.lastIndex;
if(!(m=r_time.exec(text))) {console.log('Error in time'); break;}
if(!m[1]) continue; //нашли textarea
o.time = getFreshTime(m[1].trim());
if(o.time>90)continue; //старики не нужны
if(!u[nick] || u[nick]&&u[nick].time>o.time)u[nick]=o;
}
for(let n in u){
//console.log('user:',u[n]);
onlineUserUpdate(n,now,u[n].img,u[n].time);
}
}
//check all users
}, e=>{
//log('ERROR:', xhr.status);
delete q.is_pending;
} );
return q;
}
let db;
function reset_db() {
db = {
user:{}, // user_id => { name: name, nickname: nickname, ... }
question:{}, // q_id => { is_pending:bool, user_id:string, ut:integer }
};
}
reset_db();
function load_db() {
try {
db = JSON.parse(localStorage.db);
} catch(e) {
//
}
}
if (localStorage.save_form_to_storage) { //last added option (reset on each update)
load_db();
}
let outer_tag = ()=>{};
var condition_error_string;
//Prepare environment and call eval()
function checkCondition(cond, current_data) {
condition_error_string = '';
//Special function triggered from eval() - check if a question has the tag
outer_tag = function(s) {
let tag_lower = s.toLowerCase();
let tags = current_data.q.tags;
if (!tags) return false;
for(let k in tags) {
if (tags[k].toLowerCase() == tag_lower) return true;
}
return false;
}
//Environment
let env;
if (current_data.u) {
let u = current_data.u;
let q = current_data.q;
env = {
questions: u.cnt_q == undefined ? 999 : u.cnt_q,
answers: u.cnt_a == undefined ? 999 : u.cnt_a,
solutions: u.solutions == undefined ? 101 : u.solutions,
karma: u.karma == undefined ? 0 : u.karma,
comments: u.stat_comment == undefined ? 0 : u.stat_comment,
articles: u.stat_pub == undefined ? 0 : u.stat_pub,
nickname: u.nickname || '',
title: q.t || '', //текст вопроса
views: q.v == undefined ? 0 : q.v,
honor: q.con == undefined ? -1 : q.con,
}
env.q = env.questions;
env.a = env.answers;
env.s = env.solutions;
env.k = env.karma;
env.c = env.comments;
env.publications = env.articles;
env.p = env.publications;
env.nick = env.nickname;
env.n = env.nick;
env.t = env.title;
env.v = env.views;
env.h = env.honor;
env.respect = env.a && env.h != -1 && Math.round(env.h / env.a * 10) * 0.1 || -1;
env.r = env.respect;
} else env = current_data;
try {
return eval_lite(cond, env);
} catch(e) {
if ((typeof e == 'object') && (typeof e.message == 'string')) condition_error_string = e.message;
else condition_error_string = e + '';
return false;
}
}
function makeInfo(now) {
now = now || (new Date()).getTime();
let info = {}; //всякая разна инфа. Например, кеш страницы.
if (now - cache_page_1_tm < 30000) {
info.cache_page_1_tm = cache_page_1_tm;
info.cache_page_1 = cache_page_1;
}
if (now - cache_my_feed_tm < 30000) {
info.cache_my_feed_tm = cache_my_feed_tm;
info.cache_my_feed = cache_my_feed;
}
if (localStorage.check_online == 1 && save_current_user && localStorage.is_widget==1
&& localStorage.enable_notify_action==1 && localStorage.enable_notifications==1)
{
info.users = onlineUsersArr();
if(now-last_check_online > 50000) {
info.need_check=1;
last_check_online=now;
}
if(now-last_vote_online > 60000 && online_like && save_current_user) {
if(online_like[save_current_user]) info.need_vote=2;
else info.need_vote=1;
last_vote_online=now;
}
}
return info;
}
let online = {}, last_check_online=0, online_like,last_vote_online=0;
function onlineUserUpdate(nick,now,img,tm){ //console.log('+nick',nick,tm);
tm=tm||0;
online[nick]=online[nick]||{};
online[nick].ut=now-tm*60000;
if(img){
online[nick].img=img;
if(db.user[nick])db.user[nick].img = img;
}
if(!online[nick].img && db.user[nick] && db.user[nick].img){
online[nick].img = db.user[nick].img;
}
}
function onlineClean(){
let now = (new Date()).getTime();
for(let nick in online){
if (now - online[nick].ut > 7 * 60000) delete online[nick];
}
}
let last_onl=0;
function onlineUsersArr(){
onlineClean();
let users=[];
for(let nick in online){
let o = online[nick];
users.push({
nick:nick,
img:o.img,
ut:o.ut,
});
}
users.sort((a,b) => (a.ut > b.ut) ? -1 : ((b.ut > a.ut) ? 1 : 0));
//if (last_onl!=users.length){
//console.log(users);
//last_onl=users.length;
//}
return users;
}
//Кэш тегов, которые интересны пользователю. Живет до перезагрузки, но не более суток.
let userTagsCache = {};
function updateUserTagsCache(nick) {
let cache = userTagsCache[nick];
if (!cache) {
cache = {ut:0, data:{cnt:-1}};
userTagsCache[nick] = cache;
}
if (cache.is_pending) return;
cache.is_pending = true;
let data = cache.data;
getURL('https://qna.habr.com/user/'+nick+'/tags', html=>{ //console.log('onSuccess');
cache.is_pending = false;
cache.ut = (new Date()).getTime();
let html_cards = html.match(/<article class="card"[\s\S]*?<\/article>/g);
if (!html_cards) {
data.cnt = 0;
data.tags = [];
return;
}
//Фасуем теги
let tags = [];
html_cards.forEach(h=>{
//<a class="card__head-image card__head-image_tag" href="https://qna.habr.com/tag/тестирование-по">
// <img class="tag__image tag__image_bg" src="https://habrastorage.org/r/w120/files/4d0/738/fc1/4d0738fc1d9b4e0b8818bea5ac8a4923.png" alt="тестирование-по">
//</a>
let m = h.match(/<meta itemprop="name" content="([^"]*)">[\s\S]*?<meta itemprop="interactionCount" content="(\d+) answers">[\s\S]*?<meta itemprop="interactionCount" content="(\d+) contribute">/);
if(!m)return; //console.log("Can't read tag card:",{s:h});
let tag = {
name: clearString(m[1]),
cnt_a: m[2]-0,
honor: m[3]-0,
};
if (tag.honor === 0) return;
tags.push(tag);
});
//console.log('tags.length =',tags.length);
//Отбираем значимые теги. Это те, которые до крутого излома.
let ratio = [];
for(let i=0;i<tags.length-2;i++){
let tag1 = tags[i];
let tag2 = tags[i+1];
ratio[i] = (tag1.honor / tag2.honor) * ((i+2) / (i+3));
}
//Поправки на ветер.
let maxtag = Math.min(tags.length-1, 5);
for(let i=maxtag-1; i>=0; i--) {
if (ratio[i] > ratio[maxtag]) maxtag = i;
}
//console.log(maxtag,ratio);
//if (maxtag < 0) tags=[];
//else tags.splice(maxtag+1);
data.tags = tags;
data.cnt = maxtag+1; //tags.length;
}, ()=>{ //console.log('onFail');
cache.is_pending = false;
});
}
let c_kick_truba;
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if(!db) reset_db(); //imppossible. for debugging
if (request.type == "getQuestions") {
let a = {};
let now = (new Date()).getTime();
//console.log('getQuestions:',request.arr);
request.arr.forEach(data=>{
let q_id = data.id-0;
let question = db.question[q_id];
if (question) {
question.ut = now;
if (data.v) question.v = data.v-0;
let user_id = question.user_id;
let user = user_id && db.user[user_id];
let tags = question.tags;
if (tags) {
for(let k in tags) {
if (db_tags_blacklist[tags[k].toLowerCase()]) {
a[q_id] = {hide:true, q:question};
if (user) a[q_id].u = user;
return;
}
}
}
if (user) {
let current_data = {q:question, u:user};
a[q_id] = current_data;
updateUser(user_id);
//Проверка доп. условий
for(let i=0;i<db_conditions.length;i++){
let rule = db_conditions[i];
if (checkCondition(rule.cond, current_data)) {
if (rule.act == 'hide') current_data.hide = true;
else if (rule.act == 'notify') continue; //при раскраске нам это не нужно
else current_data.color = rule.act;
break;
}
}
}
else if (!question.is_pending) analyzeQuestion(q_id, now);
}
else {
question = analyzeQuestion(q_id, now);
if (data.v) question.v = data.v-0;
}
});
sendResponse(a);
} else if (request.type == "analyzeUserTags") {
let data = userTagsCache[request.nickname];
if (data && data.ut + DAY_TIME > (new Date()).getTime() ) data = data.data;
else {
updateUserTagsCache(request.nickname);
data = {cnt:-1};
}
sendResponse(data);
} else if (request.type == "getInfo") {
sendResponse(makeInfo());
} else if (request.type == "getHints") {
let data = {}
request.nicknames.forEach(n=>{
if (CHARACTERS[n]) data[n]=CHARACTERS[n];
});
sendResponse(data);
} else if (request.type == "getUsers") {
let is_hint = localStorage.show_psycho == 1;
let u = {};
//console.log('getUsers',request.arr);
for(let nickname in request.arr) {
if (db_user_blacklist[nickname]) {
u[nickname] = {ban:1};
continue;
}
let user = db.user[nickname];
if (user) {
user = Object.assign({},user);
if (is_hint && CHARACTERS[nickname]) {
user = Object.assign({},user,CHARACTERS[nickname]);
}
let is_achiever = user.cnt_s > 49 && user.cnt_a > 9 && !NOT_ACHIEVERS[nickname] || ACHIEVERS[nickname];
if (is_achiever && localStorage.show_status_achiever == 1 && !(user.hint && localStorage.show_psycho_over_achiever == 1)) {
if (ACHIEVERS[nickname]) is_achiever = 3;
else if (user.cnt_s > 89) is_achiever = 2;
else is_achiever = 1;
user.ach = is_achiever;
}
u[nickname] = user;
//user.respect = user.cnt_a && Math.round(user.con / user.cnt_a * 10) * 0.1 || 0;
}
if (request.arr[nickname] === 1) { //Принудительное обновление, если 1, а не просто true.
//console.log('Fast update:',nickname);
updateUser(nickname, 300000);
}
else updateUser(nickname);
}
sendResponse(u);
} else if (request.type == "getOptions") {
let now = (new Date()).getTime();
let options = {};
if (!versionUpdated) updateVersion();
if (now - versionUpdated < 86400000 && localStorage.is_widget) options.is_new=true;
let q = request.q;
if (q) {
if (tracker_q_ids[q.id]) delete tracker_q_ids[q.id];
}
TOSTER_OPTIONS.forEach((opt)=>{
options[opt] = parseFloat(localStorage[opt]) || 0;
});
if(options.check_online){
options.check_online = localStorage.enable_notify_action==1 && options.enable_notifications && options.is_widget;
}
sendResponse(options);
} else if (request.type == "getOptionsHabr") {
let options = {};
HABR_OPTIONS.forEach((opt)=>{
options[opt] = parseInt(localStorage[opt]);
});
if (localStorage.habr_fix_lines == 1) {
options.habr_css = habr_css_fix_lines;
}
sendResponse(options);
} else if (request.type == "getNotifications") {
let is_active = request.active;
let now = (new Date()).getTime();
if (is_active && localStorage.notify_if_inactive==1) notifications_pause = now;
let el_hash = { hash:events_list_navbar_hash };
let info = makeInfo(now); //всякая разна инфа. Например, кеш страницы.
if (now - notifications_pause < 30000) { //Выдерживаем паузу.
sendResponse({1:el_hash,2:info}); //Но не обнуляем
return;
}
if (now - tm_browser_load < 40000) { //После загрузки расширения не паникуем, не спамим, молчим.
arr_notifications = {}; //Сливаем все уведомления в трубу.
if (c_kick_truba===undefined) {
c_kick_truba=false;
console.log('Уведомления временно отключены');
}
} else if(!c_kick_truba) {
c_kick_truba = true;
console.log('Уведомления включены');
}
getNotifications_last_time = now;
for(q_id in arr_notifications) { //Чистим от старых
let item = arr_notifications[q_id];
if (now - item.tm > 30000) {
console.log('Удаляем по таймауту',now,arr_notifications[q_id]);
delete arr_notifications[q_id];
}
}
//console.log('Отправляем уведомления:',Object.keys(arr_notifications).length);
arr_notifications[1] = el_hash;
arr_notifications[2] = info;
sendResponse(arr_notifications);
arr_notifications = {};
} else if (request.type == "checkOnline") { //get info from page
let now = (new Date()).getTime();
last_check_online = now;
let obj = request.obj;
if (online_like) { //compare
for(let nick in online_like) {
if(obj[nick]){ //удаляем, кто совпал
//delete obj[nick];
//online_like[nick].ut = now;
} else { //удалился
onlineUserUpdate(nick,now,online_like[nick].img);
}
}
for(let nick in obj) {
if(online_like[nick]); //online_like[nick].ut = now;
else onlineUserUpdate(nick,now,obj[nick].img);
}
}
online_like = obj;
sendResponse();
} else if (request.type == "disableNotifications") {
localStorage.enable_notifications = 0;
} else if (request.type == "updateIconNum") {
if (request.hash != events_list_navbar_hash) {
events_list_navbar_hash = request.hash;
events_list_navbar = request.html;
//console.log('Новый хеш от страницы:',request.hash,request.html);
}
chrome.browserAction.setBadgeText({text:""+request.cnt});
} else if (request.type == "getSubStatus") {
let qnum = request.qnum;
let question = db.question[qnum];
sendResponse(question && !!question.sub);
} else if (request.type == "setSubStatus") {
let qnum = request.qnum;
let question = db.question[qnum];
if (!question) question = analyzeQuestion(qnum);
if (question.sub) {
delete question.sub;
} else {
question.sub = 1;
}
question.ut = (new Date()).getTime();
sendResponse(!!question.sub);
saveDB();
} else if (request.type == "directQuestionUpdate") {
if (!request.nickname || !request.q_id) return;
let q = db.question[request.q_id];
if (!q) {
q = {};
db.question[request.q_id] = q;
}
q.user_id = request.nickname;
if (request.tags_html) q.tags = parseTags(request.tags_html);
if (request.title) q.t = request.title;
if (request.sb) q.sb = 1;
else if (request.sb === false) delete q.sb;
q.ut = (new Date()).getTime();
} else if (request.type == "getAside") {
//console.log('Посылаем хеш:',events_list_navbar_hash,events_list_navbar);
sendResponse({
html:events_list_navbar,
hash:events_list_navbar_hash,
});
} else if (request.type == 'clearQuestion') {
analyzeQuestion(request.q_id, 0, true);
} else if (request.type == 'Reload') {
chrome.runtime.reload();
}
});
const TOSTER_OPTIONS = [
'swap_buttons', 'hide_sol_button', 'show_habr', 'hide_word_karma',
'show_name', 'show_nickname', 'hide_offered_services', 'use_ctrl_enter',
'top24_show_tags', 'top24_show_author', 'hide_solutions', 'save_form_to_storage',
'make_dark', 'enable_notifications', //'notify_if_inactive',
//'always_notify_my_questions', 'notify_about_likes', 'notify_about_solutions',
'datetime_replace', 'datetime_days', 'show_blue_circle', 'notify_all',
'aside_right_noads', 'aside_right_hide',
'show_my_questions', 'show_my_answers', 'minify_curator', 'remove_te_spam',
'is_widget', 'top24_show', 'check_online', 'is_search', 'is_options_button', 'is_debug',
'minify_names', 'read_q',
'show_my_comments','show_my_likes','show_my_tags',
'hidemenu_all_tags','hidemenu_all_users','hidemenu_all_notifications',
//'show_psycho', //local
'psycho_replace', 'psycho_not_myself', 'achiever_replace',
'psycho_hide_comments', 'psycho_hide_same_author','achiever_hide_comments','achiever_hide_same_author','achiever_hide_next','psycho_hide_next',
'show_cnt_questions','show_cnt_answers','show_perc_solutions','show_perc_sol_marks',
'show_ban_info','dont_ban_solutions',
'show_psycho','show_honor','sol_honor_replace','psycho_summary','psycho_tags',
'mark_anwers_by_color','mark_answers_count',
'add_comment_lines','change_user_background','show_respect',
'show_user_reg_date','short_tags','show_rules','show_user_reg_toster','show_user_reg_min',
'hide_tm_panel','move_logo_to_menu','resurrect_toster_logo','resurrect_toster_logo_height',
];
const HABR_OPTIONS = [
'move_posttime_down','move_stats_up', 'hide_comment_form_by_default',
];
if (localStorage.move_logo_to_menu === undefined) { //last added option --show_ban_info
const options_to_init = [
//Toster options
'show_habr','show_nickname','fixed_a_bug','always_notify_my_questions','show_blue_circle','notify_mention','notify_expert',
'is_widget','is_options_button','is_search','top24_show','read_q','show_psycho','show_status_achiever','show_cnt_questions',
'show_cnt_answers','show_perc_solutions','show_perc_sol_marks','show_ban_info','psycho_summary',
'show_rules',
'hide_tm_panel','move_logo_to_menu',
//Habr options
//move_posttime_down, move_stats_up, hide_comment_form_by_default, habr_fix_lines
];
options_to_init.forEach(c=>{if (localStorage[c] === undefined) localStorage[c]=1});
if (localStorage.all_conditions === undefined) localStorage.all_conditions="tag('JavaScript') = #ffc";
if (localStorage.datetime_days === undefined) localStorage.datetime_days=1;
}
//--------- DEBUG ---------
function count_obj(obj) {
let cnt = 0;
for(let k in obj) cnt++;
return cnt;
}
// -------------- BLACKLIST TAGS -----------
let db_tags_blacklist = {} // [tag] = true
function update_blacklist() {
db_tags_blacklist = {};
const lines = localStorage.tag_blacklist.split("\n");
for(let i=0;i<lines.length;i++) {
let line = lines[i];
if (line.indexOf('//') !== -1) line = line.substr(0,line.indexOf('//'));
line = line.trim();
if (!line) continue;
const tags = line.replace(';',',').split(',');
tags.forEach(tag=>{
tag=tag.trim();
if (!tag) return;
db_tags_blacklist[tag.toLowerCase()] = true;
});
}
}
if (localStorage.tag_blacklist) update_blacklist();
// ----------- USER BLACK LIST ---------
let db_user_blacklist = {}; // [user] = 'rule',
function update_user_blacklist() {
db_user_blacklist = {};
let j,err;
const lines = localStorage.user_blacklist.split("\n");
for(let i=0;i<lines.length;i++) {
let line = lines[i];
if (line.indexOf('//') !== -1) line = line.substr(0,line.indexOf('//'));
line = line.trim();
if (!line) continue;
let rule = -1; //hide - правило по умолчанию
if (false && (j=line.indexOf('=')) > -1) { //сложное правило. TODO: осмыслить, доделать
rule = line.substr(j+1).trim();
if (rule == 'hide') rule = -1;
else if (rule == 'ban') rule = -2;
else {
err = 'Line#'+(i+1)+' это не действие - '+rule;
continue;
}
line = line.substr(0,j).trim();
}
let phrases = line.split(',');
phrases.forEach(phrase=>{
phrase = phrase.trim().replace("\t",' ');
if (!phrase) return;
let arr = phrase.split(' ');
arr.forEach(nick=>{
nick = nick.trim();
if (nick[0] == '@') nick=nick.substr(1);
if (!nick) return;
db_user_blacklist[nick] = rule;
});
});
}
if(err)console.log('User Filter List:',err);
}
if (localStorage.user_blacklist) update_user_blacklist();
// ------------ Дополнительные условия показа вопросов -------------
const isValidAction_image = document.createElement("img");
function isValidAction(stringToTest) {
if (stringToTest === 'hide' || stringToTest === 'notify') return true;
if (!stringToTest || stringToTest === 'inherit' || stringToTest === 'transparent') return false;
let image = isValidAction_image;
image.style.color = "rgb(0, 0, 0)";
image.style.color = stringToTest;
if (image.style.color !== "rgb(0, 0, 0)") { return true; }
image.style.color = "rgb(255, 255, 255)";
image.style.color = stringToTest;
return image.style.color !== "rgb(255, 255, 255)";
}
let example_environment = {
questions: 1, q:1,
answers: 1, a:1,
solutions: 100, s:100,
karma: 0, k:0,
comments: 0, c:0,
articles: 0, publications:0, p:0,
nickname: 'admin', nick:'admin', n:'admin',
title: 'Toster?', t:'Toster?',
views: 0, v:0,
}
var cond_update_error_string;
let db_conditions = [];
let db_conditions_hide_cnt = 0;
let db_conditions_notify_cnt = 0;
function getDbCondLength() {
return db_conditions_notify_cnt;
}
function update_conditions() {
cond_update_error_string = '';
db_conditions = [];
db_conditions_hide_cnt = 0;
db_conditions_notify_cnt = 0;
const lines = localStorage.all_conditions.split("\n");
for(let i=0;i<lines.length;i++) {
let rule = lines[i].trim(); //Строка, содержащая отдельное правило
if (rule.indexOf('//') !== -1) rule = rule.split('//')[0].trim();
if (!rule) {
//Пустая строка или комментарий
continue;
}
let eq_idx = rule.lastIndexOf('='); //Последний знак "="
if (eq_idx < 1 || rule[eq_idx-1] == '=') {
if(!cond_update_error_string) cond_update_error_string = 'Line #'+(i+1)+': '+rule+' не является правилом.';
continue;
}
let rule_cond_str = rule.substring(0,eq_idx).trim(); //Условие правила - eval(rule_cond_str)
let rule_action_str = rule.substring(eq_idx+1).trim(); //Действие правила - #ff0 или hide
if (!rule_action_str) {
if(!cond_update_error_string) cond_update_error_string = 'Line #'+(i+1)+': не задано действие.';
continue;
}
if (rule_action_str.toLowerCase() == 'hide') {
rule_action_str = 'hide';
db_conditions_hide_cnt++;
}
if (rule_action_str.toLowerCase() == 'notify') {
rule_action_str = 'notify';
db_conditions_notify_cnt++;
}
if (!isValidAction(rule_action_str)) {
if(!cond_update_error_string) cond_update_error_string = 'Line #'+(i+1)+': '+rule_action_str+' не является валидным действием.';
continue;
}
db_conditions.push({
cond: rule_cond_str,
act: rule_action_str,
});
//test condition
checkCondition(rule_cond_str, example_environment);
if (condition_error_string && !cond_update_error_string) cond_update_error_string = 'Line #'+(i+1)+': '+condition_error_string;
}
if (!cond_update_error_string && db_conditions_notify_cnt && localStorage.enable_notify_action != 1) {
cond_update_error_string = 'Действите notify отключено в настройках выше.';
}
if (!cond_update_error_string) cond_update_error_string = ' ';
return db_conditions_notify_cnt;
}
if (localStorage.all_conditions) { //because eval_lite is not defined yet
let timer = setInterval(e=>{
if (eval_lite) {
update_conditions()
clearInterval(timer);
}
},500);
}
//---------------------Notofocations------------------------
function freeRegExp(){
/\s*/g.exec("");
}
let notify_feed_arr = {}; //Массив с id вопросов, по которым уже было уведомление
function clearNotifyFeedArr(now) {
Object.keys(notify_feed_arr).forEach(id=>{
let q = db.question[id];
if (!q || now - q.ut > 9900000) delete notify_feed_arr[id];