forked from aCosmicWave/blocktherich
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblocktherich.js
107 lines (99 loc) · 3.26 KB
/
blocktherich.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
const blackList = {
ballmer: ["steve ballmer", "steven anthony ballmer"],
bezos: ["bezos", "jeff bezos"],
bloomberg: ["michael bloomberg", "michael rubens bloomberg"],
brin: ["sergey brin", "sergey mikhailovich brin"],
buffett: ["buffett", "warren buffett", "warren edward buffett"],
ellison: ["larry ellison", "lawrence joseph ellison"],
gates: ["bill gates", "william henry gates iii"],
kanye: ["kanye", "kanye west", "ye west", "kanye omari west"],
musk: ["musk", "elon musk", "elon reeve musk"],
page: ["larry page", "lawrence edward page"],
trump: ["trump", "donald trump", "donald j. trump", "donald john trump"],
zuck: ["zuck", "zuckerberg", "mark zuckerberg", "mark elliot zuckerberg"]
}
createObserver();
if (isRich(document.body.textContent)) {
findRichNode(document.body);
}
function findRichNode(node) {
if (node.hasChildNodes()) {
node.childNodes.forEach(element => {
if (node.nodeName = "A") {
if (node.href != undefined && node.href != '') {
if (isRichUrl(node.href)) {
blurNode(node);
return;
}
}
}
findRichNode(element)
})
} else if (node.nodeType === Node.TEXT_NODE) {
if (node.parentNode && node.parentNode.nodeName === 'TEXTAREA') {
return;
} else if (isRich(node.textContent)) {
blurNode(node.parentNode);
}
} else if (node.nodeName = "IMG") {
if (node.src != undefined && node.src != '') {
if (isRichUrl(node.src)) {
blurNode(node);
}
}
if (node.alt != undefined && node.alt != '') {
if (isRich(node.alt)) {
blurNode(node);
}
}
if (node.title != undefined && node.title != '') {
if (isRich(node.title)) {
blurNode(node);
}
}
}
}
function isRich(element) {
let hasMatch = false;
Object.keys(blackList).forEach(person => {
blackList[person].forEach(alias => {
if (new RegExp("(^|(\\.|,|\\s|\“)+)" + alias + "((,|\\.|\\s|s|\\’s|\\?|\\'|\“)+|$)", "ig").test(element)) {
hasMatch = true;
return;
}
});
});
return hasMatch;
}
function isRichUrl(url) {
let hasMatch = false;
let urlPath = url.split("?")[0].toLowerCase();
Object.keys(blackList).forEach(person => {
blackList[person].forEach(alias => {
if (urlPath.includes(alias)) {
hasMatch = true;
return;
}
});
});
return hasMatch;
}
function blurNode(node) {
node.style.filter = "blur(1.5rem)";
}
function createObserver() {
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.addedNodes && mutation.addedNodes.length > 0) {
for (let i = 0; i < mutation.addedNodes.length; i++) {
const newNode = mutation.addedNodes[i];
findRichNode(newNode);
}
}
});
});
observer.observe(document.body, {
childList: true,
subtree: true
});
}