-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfb_adblock.user.js
81 lines (67 loc) · 2.26 KB
/
fb_adblock.user.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
// ==UserScript==
// @name Facebook Adblocker
// @name:vi Facebook Adblocker
// @namespace https://lelinhtinh.github.io
// @description Block all ads in Facebook News Feed.
// @description:vi Chặn quảng cáo được tài trợ trên trang chủ Facebook.
// @version 1.1.3
// @icon https://i.imgur.com/F8ai0jB.png
// @author lelinhtinh
// @oujs:author baivong
// @license MIT; https://baivong.mit-license.org/license.txt
// @match https://facebook.com/*
// @match https://*.facebook.com/*
// @noframes
// @supportURL https://github.com/lelinhtinh/Userscript/issues
// @run-at document-idle
// @grant none
// ==/UserScript==
(function() {
'use strict';
/**
* Logging level
* @type {Number}
*/
const DEBUG = 0;
/* === DO NOT CHANGE === */
let countAds = 0;
const config = {
attributes: false,
childList: true,
subtree: true,
};
const removeAds = wrap => {
if (DEBUG >= 2) console.log(wrap, 'wrapNode');
const subtiltes = wrap.querySelectorAll('[data-testid*="story"]:not([role]) a');
if (!subtiltes.length) return;
Array.from(subtiltes).forEach(v => {
if (v.textContent.trim().search(/Được tài trợ|Bài viết được đề xuất|Sponsor|Suggest|Recommend/i) !== -1) {
v = v.closest('[data-testid="fbfeed_story"]');
if (DEBUG) console.log(++countAds, 'countAds');
if (DEBUG >= 3) console.log(v.innerHTML, 'htmlAds');
v.remove();
}
});
};
let observerStory;
let observerHead;
const init = () => {
if (DEBUG) console.log('Facebook Adblocker');
countAds = 0;
const newsFeed = document.querySelector('[data-testid="newsFeedStream"]');
if (DEBUG >= 2) console.log(newsFeed, 'newsFeedNode');
if (!newsFeed) return;
if (observerStory) observerStory.disconnect();
observerStory = new MutationObserver(mutationsList => {
for (let mutation of mutationsList) {
removeAds(mutation.target);
}
});
observerStory.observe(newsFeed, config);
removeAds(document);
};
init();
if (observerHead) observerHead.disconnect();
observerHead = new MutationObserver(init);
observerHead.observe(document.head, config);
})();