-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
549 lines (367 loc) · 16.5 KB
/
client.js
File metadata and controls
549 lines (367 loc) · 16.5 KB
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
var sttngsarr = {'hcc_rndmsrt': '0', 'hcc_cllpsbrnchs': '0', 'hcc_cllpsbrnchs_all':'0', 'hcc_cllpsbrnchs_rev':'0','hcc_revcll_bttn':'0', 'hcc_enbldvdr': '0', 'hcc_hiderate': '0', 'hcc_hiderate_u':'0', 'hcc_hideshdwng': '0', 'hcc_shwusrnt': '0', 'hcc_hidelng': '0', 'hcc_shrt2bttm': '0', 'hcc_hidelng_lngth': '420', 'hcc_shrt2bttm_lngth': '50', 'hcc_hidertngs': '0', 'hcc_hideorigbrn': '0'};
var scrltoparr = [];
var unrdcmmnts;
//tried hard to codegrab Habr's original branch icon but completely failed (((
//var comtreesvg =" url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAASCAYAAABvqT8MAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAYdEVYdFNvZnR3YXJlAHBhaW50Lm5ldCA0LjAuOWwzfk4AAAFNSURBVDhPfZLLSsNQEIbTpLnYtA2CWhAFH8rncudLFJcirl2JEEuLt1bUKG0aSZcudGOcL2bCqRADX6Y9Z/5/Zk6OVRRFy6AthMJGFVlzBLuipQJiE6aRxwJKX+gJOGs1k66wJ9jqEgiIIsEVcON3X+hZ8kgkx9EKQK9q0AgvTT7Qxf/ghYChaMU6PBoWED+nnvzHCDpVdBHQG4MhrAXjJMOkTBIQQDk0wxJxrAWTJFN3To49jNsk4kSFTaEWnFzehtfS1nSRe4/pyn+Y5+7N23sHAcnENUETpmCtpQa++Sb1g+B+nm8JodAVImH7NJ7uqkgrMFx5SsfnV8HZaGZ/fH6xXu5J/85fAXeF4euWLu6SoKrqT16zvinAmbOmiingHrHvxk+LyBQwrN79WjBOlpx/ea3lmwxMwY4wIBl0Y5auaBOi0cty/3d9WPwAjqhbUBHo+v0AAAAASUVORK5CYII=')";
String.prototype.trim = function () {
return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
};
String.prototype.rmnlns = function () {
return this.replace(/(\r\n|\n|\r)/gm, " ");
};
function addcss(css){
var head = document.getElementsByTagName('head')[0];
var s = document.createElement('style');
s.setAttribute('type', 'text/css');
if (s.styleSheet) { // IE
s.styleSheet.cssText = css;
} else { // the world
s.appendChild(document.createTextNode(css));
}
head.appendChild(s);
}
function addcss_hide(tag){
addcss(tag+'{visibility: hidden; display: none;}');
}
function hcc_start(){
chrome.storage.local.get('hcc_glblswtch', function (result) {
if (result.hcc_glblswtch == null) {
chrome.storage.local.set({'hcc_glblswtch': '1'}, function(){
chrome.storage.local.set({'hcc_strdsttngs': sttngsarr}, function(){
applyformat();
});
});
}
if (result.hcc_glblswtch == '1') {
chrome.storage.local.get({'hcc_strdsttngs': {}}, function (result) {
sttngsarr = result.hcc_strdsttngs;
applyformat();
});
}
});
}
function applyformat(){
var unrd;
unrd = document.querySelector("#xpanel");
if (unrd){
unrd = unrd.querySelector("span.new");
if (unrd.style.display != "none"){
unrdcmmnts = document.querySelectorAll("li.js-comment_new");
}
}
if (sttngsarr.hcc_hideorigbrn == '1') {
addcss_hide('span.comment__collapse');
}
if (sttngsarr.hcc_hidertngs == '1') {
addcss_hide('.stacked-counter');
addcss_hide('.dropdown__user-stats');
}
var rootcmnts = document.getElementById('comments-list').children;
if (sttngsarr.hcc_hiderate == '1'){
addcss_hide('span.voting-wjt__counter');
//unhide if voted
//in half cases unhides current first comment's rate instead of voted, but only "live", after page reload works correctly
if (sttngsarr.hcc_hiderate_u == '1'){
let tmp = document.querySelectorAll("div.voting-wjt_comments");
for (let item of tmp){
if (item.querySelector("button.voting-wjt__button").title.includes("Вы")){
let tmpspan = item.querySelector("span.voting-wjt__counter");
tmpspan.style.visibility = "visible";
tmpspan.style.display = "flex";
}
}
}
}
if (sttngsarr.hcc_hideshdwng == '1'){ addcss('.comment__message_downgrade {opacity: 1 !important;}');}
if (sttngsarr.hcc_enbldvdr == '1') {addcss('hr.btwncmnts {display: block; margin-top: 2em; margin-bottom: 2em; width: 90%; height: 1px; border: none; color:#e3e3e3;background-color:#e3e3e3;}');}
if (sttngsarr.hcc_cllpsbrnchs == '1'){
addcss_hide('.brtggl');
addcss('label.cmmtggl {display: block; color: #666;}');
if (sttngsarr.hcc_cllpsbrnchs_rev == '0'){
//addcss('label.cmmtggl::before {font-weight: normal; font-size: 16px; content: "\\1f4ac "'+comtreesvg+'; vertical-align: middle; display: inline-block; width: 2em; background: #f7f7f7; text-align: center;}');
addcss('label.cmmtggl::before {font-weight: normal; font-size: 16px; content: "\\1f4ac h"; vertical-align: middle; display: inline-block; width: 2em; background: #f7f7f7; text-align: center;}');
addcss('.brtggl:checked ~ label.cmmtggl::before { content: "–";}');
} else {
addcss('label.cmmtggl::before {font-weight: normal; font-size: 16px; content: "–"; vertical-align: middle; display: inline-block; width: 2em; background: #f7f7f7; text-align: center;}');
addcss('.brtggl:checked ~ label.cmmtggl::before { content: "\\1f4ac h";}');
}
//css styles for bottom collapse buttons
addcss('label.cmmtggl_frombttm::before { content: "–";}');
for (let i=0; i < rootcmnts.length; i++ ){
let brnchs = rootcmnts[i].querySelectorAll('ul.content-list_nested-comments');
for (let brnch of brnchs){
let amnt = brnch.children.length;
//let bttmbttn = 1;
//if this is last comment don't do anything unless we need to create collapse from bottom button
if (brnch.parentNode.id == "comments-list") {continue;}
//Habr's html code contains empty <ul> at the end of comments branch
if (amnt == 0) {
//dat sheat took a lot of me and this code is smokin but not hot pile
if (sttngsarr.hcc_revcll_bttn == '1'){
let tmpbr = brnch.parentNode;
let tmp_id = tmpbr.querySelector("span.parent_id").dataset.parent_id;
while( tmpbr.id != "comments-list" ){
if (tmpbr.tagName == "UL" && tmpbr.children.length > 1) {
tmp_id = tmpbr.querySelector("span.parent_id").dataset.parent_id;
break;
}
tmpbr = tmpbr.parentNode;
if (tmpbr.id != "comments-list" &&
tmpbr.querySelector("span.parent_id").dataset.parent_id != "0") {
tmp_id = tmpbr.querySelector("span.parent_id").dataset.parent_id;
}
}
if (tmp_id == 0) {continue;}
let tmplblem = null;
if (tmpbr.id == "comments-list"){
tmplblem = tmpbr.querySelectorAll("label.cmmtggl");
}
else {
//thats ok, just 3 nodes up (ノ°Д°)ノ︵ ┻━┻
tmplblem = tmpbr.parentNode.parentNode.parentNode.querySelectorAll("label.cmmtggl");
}
let tmpble = null;
for (let i=0; i < tmplblem.length; i++){
//console.log(tmplblem[i].id); console.log("ltggl_reply_comments_" + tmp_id);
if (tmplblem[i].id == "ltggl_reply_comments_" + tmp_id){
tmplble = tmplblem[i];
break;
}
}
if (tmplble == null) {continue;}
let tmpfcs = tmplble.parentNode;
/**** CREATING ELEMENT WITH DUPLICATE ID SURE WILL GO TO XHTML HELL FOR THAT ****/
tmplblbttm = tmplble.cloneNode(true);
tmplblbttm.classList.add("cmmtggl_frombttm");
tmplblbttm.onclick = function() { tmpfcs.scrollIntoView(); };
brnch.parentNode.insertBefore(tmplblbttm,brnch.nextSibling);
}
//just continue
continue;
}
if (sttngsarr.hcc_cllpsbrnchs_all == '0' &&
brnch.parentNode.parentNode.id != "comments-list")
{continue;}
let tmpid = brnch.id;
let tmpipt = document.createElement("input");
tmpipt.className = "brtggl"; tmpipt.type = "checkbox"; tmpipt.id = "tggl_"+tmpid;
let tmplbl = document.createElement("label");
tmplbl.className = "cmmtggl"; tmplbl.htmlFor = "tggl_"+tmpid; tmplbl.id = "ltggl_"+tmpid;
tmplbl.onclick = function(){document.getElementById("ltggl_"+tmpid).focus();};
//create button to collapse/expand branch from start
brnch.parentNode.insertBefore(tmpipt,brnch); brnch.parentNode.insertBefore(tmplbl,brnch);
//create button to collapse/expand branch from end
/*
if ( amnt > 1 || brnch.parentNode.parentNode.id == "comments-list" ){
let tmpfcs = document.getElementById("ltggl_"+tmpid).parentNode; //
}
*/
if (sttngsarr.hcc_cllpsbrnchs_rev == '0'){
addcss_hide('#'+tmpid);
addcss('#tggl_'+tmpid+':checked ~ #'+tmpid+'{visibility: visible; display: block;}');
}
else {
addcss('#'+tmpid+' {visibility: visible; display: block;}');
addcss('#tggl_'+tmpid+':checked ~ #'+tmpid+'{visibility: hidden; display: none;}');
}
let tmpamnt = brnch.querySelectorAll('li.content-list__item_comment').length;
addcss('#ltggl_'+tmpid+'::after {content: \"\\0020 '+tmpamnt+'\"}');
}
}
}
if (sttngsarr.hcc_hidelng == '1'){
addcss('label.cmmtggl {display: block; color: #666;}');
addcss('div.lngcmmnt { width: 100%; max-height: 8em; overflow: hidden; -webkit-mask-image: linear-gradient(to bottom, rgba(0,0,0,1) 50%, rgba(0,0,0,0)); -webkit-mask-size: 100%; -webkit-mask-repeat: no-repeat; -webkit-mask-position: 100% 50%;}');
addcss('label.lngcmmtggl {vertical-align: middle; font-style: italic; display: block; text-align: center; width: 100%; color: #bbb; }');
for (let i=0; i < rootcmnts.length; i++){
let tmp = rootcmnts[i].querySelector("div.comment__message");
if (tmp.innerHTML.length > Number(sttngsarr.hcc_hidelng_lngth)) {
let tmpid = rootcmnts[i].querySelector('div.comment').id;
tmp.classList.add("lngcmmnt"); tmp.id = ("lngcmmnt_"+tmpid);
tmp.outerHTML = "<input class=\"brtggl\" type=\"checkbox\" id=\"lngcmnttggl_"+tmpid+"\">"+tmp.outerHTML+"<label class=\"lngcmmtggl\" for=\"lngcmnttggl_"+tmpid+"\" id=\"lntggl_"+tmpid+"\">развернуть</label>";
addcss('#lngcmnttggl_'+tmpid+':checked ~ #lngcmmnt_'+tmpid+' {max-height: none; -webkit-mask-image:none}');
addcss('#lngcmnttggl_'+tmpid+':checked ~ #lntggl_'+tmpid+'{visibility: hidden; display:none;}');
}
}
}
var rootdiv = document.getElementById('comments-list');
if (sttngsarr.hcc_rndmsrt == '1'){
var i = 0;
while (rootcmnts.length > i){
let tmp = Math.floor(Math.random() * (rootcmnts.length-i));
tmp += i;
rootdiv.insertBefore(rootcmnts[tmp], rootcmnts[i]);
i++;
}
}
if (sttngsarr.hcc_shrt2bttm == '1'){
var shtlng = Number(sttngsarr.hcc_shrt2bttm_lngth);
var iend = 0;
for (let i=0; i < rootcmnts.length-iend; i++){
let tmp = rootcmnts[i].querySelector("div.comment__message");
if (tmp.innerHTML.length < shtlng) {
rootdiv.appendChild(rootcmnts[i]);
iend++; i--;
}
}
}
/**********************EXPERIMENTAL*************************/
var doExprmnt = 0;
if (doExprmnt == 1){
var tcmntsnms = [];
var tcmntsrts = [];
let z = -1;
let tmpamnt = "";
for (let i=0; i < rootcmnts.length; i++ ){
let ttls = rootcmnts[i].querySelector('span.voting-wjt__counter');
//Всего голосов –8: ↑7 и ↓15
tmpamnt = (ttls.innerText);
if (tmpamnt == "") {continue;}
if (tmpamnt.indexOf("–") != -1) {
//wut?..
tmpamnt = tmpamnt.replace(/–/g,"-");
}
tmpamnt = parseInt(tmpamnt);
let tmpstr = ttls.title;
let plss = parseInt(tmpstr.substring((tmpstr.indexOf("↑")+1),(tmpstr.indexOf("↓")-3)));
let mnss = parseInt(tmpstr.substring((tmpstr.indexOf("↓")+1),tmpstr.Length));
if (plss == 0 || mnss == 0 || (plss+mnss) < 8) {continue;}
z++;
//this is index of root comment. It's not working, dunno why
tcmntsnms[z] = i;
//We need max rating and mix plus-to-minus amount to count comment as interesting.
//So we need max amount of votes minus min plus-to-minus to be highest to count comment discussable
tcmntsrts[z] = Math.abs(plss-mnss);
console.log(tcmntsrts[z]);
}
console.log(tcmntsnms);
let tmpln = z;
var endcmnts = [];
for (i=0; i < z+1; i++){
let mn = Math.min.apply(null, tcmntsrts);
//can be more than one min
let tmpi = tcmntsrts.indexOf(mn);
endcmnts[i] = tcmntsnms[tmpi];
console.log(tcmntsrts[tmpi]);
tcmntsrts[tmpi] = 1000000;
}
endcmnts = endcmnts.reverse();
console.log(endcmnts);
i = 0;
while (endcmnts.length > i){
//It works but not shows. Hmmm..
//if its rootcmnts[i], not rootcmnts[0], only first goes up
rootdiv.insertBefore(rootcmnts[(endcmnts[i])],rootcmnts[i]);
i++;
}
}
if (sttngsarr.hcc_enbldvdr == '1') {
var hr = document.createElement("hr");
hr.className = "btwncmnts";
var rt = document.getElementById('comments-list');
for (let i = 0; i < rootcmnts.length; i+=2) {
//if not clone, we got one element moving to bottom of list
let cln = hr.cloneNode(true);
rt.insertBefore(cln, rt.children[i+1]);
}
}
if (sttngsarr.hcc_shwusrnt == '1') {
getntsdata();
}
document.getElementById("xpanel").addEventListener("click", fixtracker, false);
//expand branch if url is for direct comment sttngsarr.hcc_cllpsbrnchs == '1' &&
if ( sttngsarr.hcc_cllpsbrnchs == '1' && sttngsarr.hcc_cllpsbrnchs_rev == '0' && (window.location.href).includes("#comment_") ){
let cmnt = window.location.href;
cmnt = cmnt.substring(cmnt.indexOf("#"),cmnt.Length);
let tmp = document.querySelector(cmnt);
while (tmp.parentNode){
tmp = tmp.parentNode;
if (tmp.className == "comments-list") break;
let tmpinpt = tmp.querySelector('input[class=brtggl]');
if (tmpinpt) {
tmpinpt.checked = true;
}
}
document.getElementById(cmnt).focus();
}
}
function getntsdata(){
var theUrl = "https://habr.com/ru/users/"; //notes
var user = "";
user = document.querySelector("span.user-info__nickname").innerText;
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", theUrl+user+"/notes/", true); // true for asynchronous
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
addusrnts(xmlHttp.responseText);
}
}
xmlHttp.send(null);
}
function addusrnts(dcmnt){
var parser = new DOMParser();
var tmp = parser.parseFromString(dcmnt.toString(), "text/html");
var usrnms = tmp.querySelectorAll("td.notes-table__username");
var usrnts = tmp.querySelectorAll("td.notes-table__desc");
addcss("div.usernotes{font-size: 0.8em; margin: 0.5em;padding: 0.5em;background-color:#fff7d7;}");
var usrsincmmnts = document.getElementsByClassName("user-info_inline");
for (let i=0; i< usrsincmmnts.length; i++){
var tmpusrnm = usrsincmmnts[i].dataset.userLogin;
for (let ii=0; ii<usrnms.length; ii++){
if (tmpusrnm == usrnms[ii].innerText.trim()){
let tmpparent = usrsincmmnts[i].parentNode;
let tmpref = tmpparent.querySelector("time");
let tmphtml = document.createElement("div");
let tmptext = usrnts[ii].innerText.rmnlns();
tmptext = tmptext.substring(0, 60);
if (usrnts[ii].innerText.length > 60) tmptext += "...";
tmphtml.className = "usernotes"; tmphtml.innerText = tmptext;
tmphtml.setAttribute('title', usrnts[ii].innerText);
tmpparent.insertBefore(tmphtml, tmpref);
}
}
}
}
function fixtracker(){
if (!unrdcmmnts || sttngsarr.hcc_cllpsbrnchs == '0') return;
for (let i=0; i< unrdcmmnts.length; i++){
let tmpinpt = unrdcmmnts[i].parentNode;
tmpinpt = tmpinpt.querySelectorAll("input.brtggl");
for (let z=0; z < tmpinpt.length; z++){
tmpinpt[z].checked = true;
}
let tmp = unrdcmmnts[i];
while (tmp.parentNode){
tmp = tmp.parentNode;
if (tmp.tagName == "LI"){
let tmp2 = tmp.querySelector("input.brtggl");
if (tmp2) tmp2.checked = true;
}
if (tmp.id == "comments-list") break;
}
}
}
//window.addEventListener('DOMContentLoaded', addlst());
window.addEventListener('load', hcc_start());
document.getElementById("comments").addEventListener('click', function(event){
if (event == null || sttngsarr.hcc_hiderate_u == '0') return;
var clckdlmnt = event.target;
var i = 0;
while (clckdlmnt.parentNode) {
if (i > 5) break;
clckdlmnt = clckdlmnt.parentNode;
if (clckdlmnt.classList.contains("voting-wjt_comments")) {
let tmpspan = clckdlmnt.querySelector("span.voting-wjt__counter");
var observer = new MutationObserver(function() {
let tmpspan = clckdlmnt.querySelector("span.voting-wjt__counter");
tmpspan.setAttribute("style", "visibility: visible; display: flex;");
observer.disconnect();
});
var config = { attributes: true, childList: false, characterData: false, subtree: false };
// Start observing the target node for configured mutations
observer.observe(tmpspan.parentNode, config);
}
i++;
}
});
document.getElementById("xpanel").addEventListener("click", fixtracker, false);