-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontent-script.js
651 lines (522 loc) · 15.8 KB
/
content-script.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
// 1. Get all objects of feed
// 2. Find the nested content
// 3. Check against the setting and hide.
// A list of post.
let feed;
// Hadle post type for image.
function hideImagePost(feed) {
if (!feed) return;
for (var i = 0; i < feed.length; i++) {
if (!feed[i].children || feed[i].children.length === 0) {
continue;
}
if (feed[i].attribs === 'nodisplay' || feed[i].attribs === 'imagefilterchecked') {
continue;
}
// TODO: Need to hide .artdeco-card for multiple image share.
if (feed[i].getElementsByClassName('feed-shared-image').length > 0) {
feed[i].style = 'display:none;';
feed[i].setAttribute('nodisplay', '');
} else {
feed[i].setAttribute('imagefilterchecked', '');
}
}
}
// Hadle post type for video.
function hideVideoPost(feed) {
if (!feed) return;
for (var i = 0; i < feed.length; i++) {
if (!feed[i].children || feed[i].children.length === 0) {
continue;
}
if (feed[i].attribs === 'nodisplay' || feed[i].attribs === 'videofilterchecked') {
continue;
}
if (feed[i].getElementsByClassName('feed-shared-linkedin-video').length > 0) {
feed[i].style = 'display:none;';
feed[i].setAttribute('nodisplay', '');
} else {
feed[i].setAttribute('videofilterchecked', '');
}
}
}
// Hadle post type for links.
function hideLinkPost(feed) {
if (!feed) return;
for (var i = 0; i < feed.length; i++) {
if (!feed[i].children || feed[i].children.length === 0) {
continue;
}
if (feed[i].attribs === 'nodisplay' || feed[i].attribs === 'linkfilterchecked') {
continue;
}
if (feed[i].getElementsByClassName('feed-shared-article').length > 0) {
feed[i].style = 'display:none;';
feed[i].setAttribute('nodisplay', '');
} else {
feed[i].setAttribute('linkfilterchecked', '');
}
}
}
// Hide document like a slide.
function hideDocumentPost(feed) {
if (!feed) return;
for (var i = 0; i < feed.length; i++) {
if (!feed[i].children || feed[i].children.length === 0) {
continue;
}
if (feed[i].attribs === 'nodisplay' || feed[i].attribs === 'documentfilterchecked') {
continue;
}
if (feed[i].getElementsByClassName('feed-shared-document').length > 0) {
feed[i].style = 'display:none;';
feed[i].setAttribute('nodisplay', '');
} else {
feed[i].setAttribute('documentfilterchecked', '');
}
}
}
// hide action. Q&A stuff.
function hideActionPost(feed) {
if (!feed) return;
for (var i = 0; i < feed.length; i++) {
if (!feed[i].children || feed[i].children.length === 0) {
continue;
}
if (feed[i].attribs === 'nodisplay' || feed[i].attribs === 'actionfilterchecked') {
continue;
}
if (feed[i].getElementsByClassName('feed-shared-poll').length > 0) {
feed[i].style = 'display:none;';
feed[i].nodisplay('nodisplay', '');
} else {
feed[i].setAttribute('actionfilterchecked', '');
}
}
}
// Hadle post with text content.
function hideTextPost(feed) {
if (!feed) return;
for (var i = 0; i < feed.length; i++) {
if (!feed[i].children || feed[i].children.length === 0) {
continue;
}
if (feed[i].attribs === 'nodisplay' || feed[i].attribs === 'textfilterchecked') {
continue;
}
if (feed[i].getElementsByClassName('feed-shared-text').length > 0) {
feed[i].style = 'display:none;';
feed[i].setAttribute('nodisplay', '');
} else {
feed[i].setAttribute('textfilterchecked', '');
}
}
}
// Handle post with specific text keyword.
function hideKeywordPost(feed, keywords) {
if (!feed) return;
// Make it case insensitive.
const regex = new RegExp(keywords.join('|'), "i");
for (var i = 0; i < feed.length; i++) {
if (!feed[i].children || feed[i].children.length === 0) {
continue;
}
if (feed[i].attribs === 'nodisplay' || feed[i].attribs === 'keywordfilterchecked') {
continue;
}
const description = feed[i].getElementsByClassName('feed-shared-update-v2__description-wrapper')[0];
if (!description) {
feed[i].setAttribute('keywordfilterchecked', '');
continue;
}
const sharedKeyword = regex.test(description.innerText);
if (sharedKeyword) {
feed[i].style = 'display:none;';
feed[i].setAttribute('nodisplay', '');
} else {
feed[i].setAttribute('keywordfilterchecked', '');
}
}
}
// Hadle post with companies. feed-shared-actor__description contains 'followers'
// Handle post with promotion.
function hidePromotionPost(feed) {
if (!feed) return;
for (var i = 0; i < feed.length; i++) {
if (!feed[i].children || feed[i].children.length === 0) {
continue;
}
if (feed[i].attribs === 'nodisplay' || feed[i].attribs === 'promotionfilterchecked') {
continue;
}
const subDescription = feed[i].getElementsByClassName('feed-shared-actor__sub-description')[0];
if (!subDescription) {
feed[i].setAttribute('promotionfilterchecked', '');
continue;
}
if (subDescription.innerHTML.includes('Promoted')) {
feed[i].style = 'display:none;';
feed[i].setAttribute('nodisplay', '');
} else {
feed[i].setAttribute('promotionfilterchecked', '');
}
}
}
// Handle activity post that contains header.
// Note: This will hide LinkedIn's suggestions such as job recommended and popular courses.
function hideActivityPost(feed) {
if (!feed) return;
for (var i = 0; i < feed.length; i++) {
if (!feed[i].children || feed[i].children.length === 0) {
continue;
}
if (feed[i].attribs === 'nodisplay' || feed[i].attribs === 'activityfilterchecked') {
continue;
}
const sharedHeader = feed[i].getElementsByClassName('feed-shared-header')[0];
if (!sharedHeader) {
feed[i].setAttribute('activityfilterchecked', '');
continue;
}
if (!sharedHeader.innerText.includes('job update')) {
feed[i].style = 'display:none;';
feed[i].setAttribute('nodisplay', '');
} else {
feed[i].setAttribute('activityfilterchecked', '');
}
}
}
// Handle all posts in feed.
function hideAll(feed) {
if (!feed) return;
for (var i = 0; i < feed.length; i++) {
if (!feed[i].children || feed[i].children.length === 0) {
continue;
}
if (feed[i].attribs === 'nodisplay') {
continue;
}
// NOTE: Use visibility hidden instead of display none here. Hiding all posts trigger load more posts easily. That continuous page update will slow down the site.
feed[i].style = 'visibility:hidden;';
feed[i].setAttribute('nodisplay', '');
}
}
function hidePromotions() {
const promotions = document.getElementsByClassName('ad-banner-container');
if (promotions.length === 0) return;
for (var i = 0; i < promotions.length; i++) {
if (!promotions[i].children || promotions[i].children.length === 0) {
continue;
}
if (promotions[i].attribs === 'nodisplay') {
continue;
}
// NOTE: Hide instead of display none. It's more aesthetically pleasant to keep the layout.
promotions[i].style = 'visibility:hidden;';
promotions[i].setAttribute('nodisplay', '');
}
}
function hideNews() {
const news = document.getElementsByClassName('news-module');
if (news.length === 0) return;
for (var i = 0; i < news.length; i++) {
if (!news[i].children || news[i].children.length === 0) {
continue;
}
if (news[i].attribs === 'nodisplay') {
continue;
}
// NOTE: Hide instead of display none. It's more aesthetically pleasant to keep the layout.
news[i].style = 'visibility:hidden;';
news[i].setAttribute('nodisplay', '');
// NOTE: News has a parent wrapper that would show white. Hide that as well.
news[i].parentElement.style = 'visibility:hidden;';
}
}
function hideLearning() {
const courses = document.getElementsByClassName('learning-top-courses');
if (courses.length === 0) return;
for (var i = 0; i < courses.length; i++) {
if (!courses[i].children || courses[i].children.length === 0) {
continue;
}
if (courses[i].attribs === 'nodisplay') {
continue;
}
// NOTE: Hide instead of display none. It's more aesthetically pleasant to keep the layout.
courses[i].style = 'visibility:hidden;';
courses[i].setAttribute('nodisplay', '');
// NOTE: courses has a parent wrapper that would show white. Hide that as well.
courses[i].parentElement.style = 'visibility:hidden;';
}
}
function hideFollowSuggestions() {
const followSuggestions = document.getElementsByClassName('feed-follows-module');
if (followSuggestions.length === 0) return;
for (var i = 0; i < followSuggestions.length; i++) {
if (!followSuggestions[i].children || followSuggestions[i].children.length === 0) {
continue;
}
if (followSuggestions[i].attribs === 'nodisplay') {
continue;
}
// NOTE: Hide instead of display none. It's more aesthetically pleasant to keep the layout.
followSuggestions[i].style = 'visibility:hidden;';
followSuggestions[i].setAttribute('nodisplay', '');
// NOTE: followSuggestions has a parent wrapper that would show white. Hide that as well.
followSuggestions[i].parentElement.style = 'visibility:hidden;';
}
}
function isDisabled() {
const data = {
type: 'IS_DISABLED',
};
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage(data, resp => {
const disabled = resp.disabled;
resolve(disabled);
});
});
}
function isDisabledImage() {
const data = {
type: 'IS_DISABLED_IMAGE_HIDE',
};
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage(data, resp => {
const disabled = resp.disabled;
resolve(disabled);
});
});
}
function isDisabledVideo() {
const data = {
type: 'IS_DISABLED_VIDEO_HIDE',
};
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage(data, resp => {
const disabled = resp.disabled;
resolve(disabled);
});
});
}
function isDisabledLink() {
const data = {
type: 'IS_DISABLED_LINK_HIDE',
};
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage(data, resp => {
const disabled = resp.disabled;
resolve(disabled);
});
});
}
function isDisabledDocument() {
const data = {
type: 'IS_DISABLED_DOCUMENT_HIDE',
};
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage(data, resp => {
const disabled = resp.disabled;
resolve(disabled);
});
});
}
function isDisabledPromotion() {
const data = {
type: 'IS_DISABLED_PROMOTION_HIDE',
};
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage(data, resp => {
const disabled = resp.disabled;
resolve(disabled);
});
});
}
function isDisabledDistractions() {
const data = {
type: 'IS_DISABLED_DISTRACTIONS',
};
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage(data, resp => {
const disabled = resp.disabled;
resolve(disabled);
});
});
}
function isDisabledActivities() {
const data = {
type: 'IS_DISABLED_ACTIVITIY_HIDE',
};
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage(data, resp => {
const disabled = resp.disabled;
resolve(disabled);
});
});
}
function loadKeywords() {
const data = {
type: 'GET_KEYWORDS_HIDE'
};
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage(data, resp => {
const keywords = resp.keywords;
resolve(keywords);
});
});
}
function handleImagePost(feed) {
return isDisabledImage()
.then(disabled => {
if (disabled) return;
hideImagePost(feed);
});
}
function handleVideoPost(feed) {
return isDisabledVideo()
.then(disabled => {
if (disabled) return;
hideVideoPost(feed);
});
}
function handleLinkPost(feed) {
return isDisabledLink()
.then(disabled => {
if (disabled) return;
hideLinkPost(feed);
});
}
function handleDocumentPost(feed) {
return isDisabledDocument()
.then(disabled => {
if (disabled) return;
hideDocumentPost(feed);
});
}
function handlePromotionPost(feed) {
return isDisabledPromotion()
.then(disabled => {
if (disabled) return;
hidePromotionPost(feed);
});
}
let keywordsToHide;
function handleKeywords(feed) {
let p;
if (!keywordsToHide) {
p = loadKeywords();
} else {
p = Promise.resolve(keywordsToHide);
}
p.then(keywords => {
keywordsToHide = keywords;
if (keywordsToHide.length === 0) return;
hideKeywordPost(feed, keywords);
});
}
function handleDistractions(feed) {
return isDisabledDistractions()
.then(disabled => {
if (disabled) return;
hideAll(feed);
hidePromotions();
hideNews();
hideLearning();
hideFollowSuggestions();
});
}
function handleActivities(feed) {
return isDisabledActivities()
.then(disabled => {
if (disabled) return;
hideActivityPost(feed);
});
}
function addObserver() {
// Add listener for the wrapper and listen for a chnage.
const allH1 = document.getElementsByTagName('h1');
// It will contain logo as h1 in navbar. Remove.
let feedH1;
for (var i = 0; i < allH1.length; i++) {
if (!allH1[i].className.includes('global-nav__branding')) {
feedH1 = allH1[i];
}
}
let parent;
if (feedH1) {
parent = feedH1.parentElement;
} else {
// if no header is there, check if this is the search result feed.
parent = document.getElementsByClassName('search-results__list')[0];
}
if (!parent) return;
let timer;
// feed pointer gets updated automatically along with the element. Keep the previous length in cache.
let feedPrevLength;
const mutationObserver = new MutationObserver(e => {
// e is a list of mutation record. Each contains
// Save computation. This observation with subtree will get called often.
if (feed && e.length < feed.length) {
return;
}
timer = new Date();
const gapTime = 500;
const setFeed = () => {
// Wait for some time. This mutation observer might get called repeatedly.
if ((new Date().getTime() - timer.getTime()) < gapTime) {
setTimeout(setFeed, gapTime);
return;
}
feed = document.getElementsByClassName('feed-shared-update-v2');
if (feedPrevLength !== feed.length) {
handleImagePost(feed);
handleVideoPost(feed);
handleLinkPost(feed);
handleDocumentPost(feed);
handlePromotionPost(feed);
handleActivities(feed);
handleKeywords(feed);
// hideActionPost(feed);
handleDistractions(feed);
feedPrevLength = feed.length;
}
}
setTimeout(setFeed, gapTime);
});
// LinkedIn insert the about 10-20 div placeholders first even though inside the element (the part about feed-shared-update-v2) is never inserted unless you scroll enough.
// Structure:
// ember-view > relative ember-view > feed-shared-update-v2
// That is why feed-shared-update-v2 does not catch all div afterwards and the shallow mutation observer does not catch them either.
mutationObserver.observe(parent, { childList: true, subtree: true });
}
function init() {
// Check for disabled status
return isDisabled()
.then(disabled => {
if (disabled) return;
feed = document.getElementsByClassName('feed-shared-update-v2');
if (feed.length === 0) {
return;
}
// Handle posts according to each settings.
handleImagePost(feed);
handleVideoPost(feed);
handleLinkPost(feed);
handleDocumentPost(feed);
handlePromotionPost(feed);
handleActivities(feed);
handleKeywords(feed);
// NOTE: Action not working.
// hideActionPost(feed);
handleDistractions(feed)
addObserver();
});
}
init();
// Linkedin is a single app and content script does not get reloaded when navigating pages.
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.type === 'PAGE_RELOADED') {
init();
}
});