-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
160 lines (147 loc) · 3.82 KB
/
content.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
const body = document.body;
let bannedWord = "*Banned by StopSlogan*";
let parseWiki = true;
const slogans = {};
let slogansKeys = [];
const bannedSlogans = [];
const url =
"https://en.wikipedia.org/w/api.php?" +
new URLSearchParams({
origin: "*",
action: "parse",
page: "List_of_political_slogans",
format: "json"
});
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
function chromeStorage(sKey) {
return new Promise(function(resolve, reject) {
chrome.storage.sync.get(sKey, function(items) {
resolve(items[sKey]);
});
});
}
chromeStorage("parseWiki")
.then(result => {
if (result != undefined) {
parseWiki = result;
}
return chromeStorage("customBanWord");
})
.then(result => {
if (result) {
bannedWord = result;
}
return chromeStorage("savedCustomSlogans");
})
.then(result => {
if (result) {
result.forEach(slogan => {
slogansKeys.push(slogan);
slogans[slogan] = true;
});
}
startScan();
});
function startScan() {
function parseWikiNodes(startNode) {
if (startNode.tagName == "LI") {
const key = startNode.innerText.toLowerCase();
slogans[key] = true;
slogansKeys.push(key);
}
startNode.childNodes.forEach(node => {
parseWikiNodes(node);
});
}
const callback = function(mutationsList, observer) {
// Use traditional 'for loops' for IE 11
for (let mutation of mutationsList) {
if (mutation.type === "childList") {
const node = mutation.target;
checkNodeRecursively(node);
}
}
};
if (parseWiki) {
fetch(url)
.then(response => response.json())
.then(parsed => parsed.parse.text)
.then(text => {
const domParser = new DOMParser();
let doc = domParser.parseFromString(text["*"], "text/html");
parseWikiNodes(doc);
checkNodeRecursively(body);
});
} else {
checkNodeRecursively(body);
}
const observer = new MutationObserver(callback);
observer.observe(body, {
childList: true,
subtree: true
});
function checkNode(startNode) {
if (startNode.nodeType == Node.TEXT_NODE) {
const value = startNode.nodeValue.toLowerCase();
if (deepCheck(value)) {
startNode.nodeValue = bannedWord;
bannedSlogans.push(escapeHtml(value));
}
} else {
if (startNode.nodeType == Node.ELEMENT_NODE) {
if (startNode.tagName == "INPUT" || startNode.tagName == "TEXTAREA") {
const res = deepCheck(startNode.value);
if (res) {
startNode.value = res;
}
} else {
if (startNode.childNodes.length == 0) {
const res = deepCheck(startNode.value);
if (res) {
startNode.innerText = res;
}
}
}
}
}
}
function deepCheck(text) {
if (text == bannedWord || !text) {
return false;
}
text = text.toLowerCase();
if (slogans[text]) {
return bannedWord;
}
for (var i = 0; i < slogansKeys.length; i++) {
const key = slogansKeys[i];
if (text.includes(key)) {
let edited = text.replace(key, bannedWord);
return edited;
}
}
return false;
//TODO: Levenshtein Distance Algorithm
}
function checkNodeRecursively(startNode) {
checkNode(startNode);
startNode.childNodes.forEach(node => {
checkNodeRecursively(node);
});
}
// chrome.runtime.sendMessage(bannedSlogans, function (response) {
// console.log(response.farewell);
// });
}
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.action == "getBannedSlogans") {
sendResponse(bannedSlogans);
}
});