-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent_script.js
146 lines (134 loc) · 4.01 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
// Avoid duplicate insertions: only replace "corona" or "virus" strings
// after "corona virus" strings are replaced => put one word strings at
// the end of the list.
var TARGET_STRINGS = [
"the corona virus",
"the covid-19 virus",
"the covid virus",
"the corona-virus",
"the covid-19",
"the Covid-19",
"the COVID-19",
"the coronavirus",
"the Coronavirus",
"the covid",
"the Covid",
"the COVID",
"the virus",
"the Virus",
"the corona",
"the Corona",
"corona virus",
"covid-19 virus",
"covid virus",
"corona-virus",
"covid-19",
"Covid-19",
"COVID-19",
"coronavirus",
"Coronavirus",
"covid",
"Covid",
"COVID",
"virus",
"Virus",
"corona",
"Corona",
];
replaceCorona();
function replaceCorona() {
chrome.storage.sync.get(["cbp-replacement", "cbp-enabled"], (storage) => {
const isEnabled = storage["cbp-enabled"];
if (isEnabled === undefined) {
return;
}
const replacement = storage["cbp-replacement"];
if (
isEnabled &&
replacement.trim() !== "" &&
TARGET_STRINGS.indexOf(replacement) === -1
) {
traverseAndObserveDocument(replacement);
TARGET_STRINGS.push(replacement);
}
chrome.storage.onChanged.addListener((changes) => {
for (key in changes) {
if (key === "cbp-replacement" || !isEnabled) {
return replaceCorona();
}
}
});
});
}
function walk(rootNode, value) {
// Find all the text nodes in rootNode
const walker = document.createTreeWalker(
rootNode,
NodeFilter.SHOW_TEXT,
null,
false
);
let node;
// Modify each text node's value
while ((node = walker.nextNode())) {
handleText(node, value);
}
}
function handleText(textNode, value) {
textNode.nodeValue = replaceText(textNode.nodeValue, value);
}
function replaceText(v, value) {
TARGET_STRINGS.forEach((targetString) => {
v = v.replace(new RegExp(targetString, "g"), value);
});
return v;
}
// Returns true if a node should *not* be altered in any way
function isForbiddenNode(node) {
return (
node.isContentEditable || // DraftJS and many others
(node.parentNode && node.parentNode.isContentEditable) || // Special case for Gmail
(node.tagName &&
(node.tagName.toLowerCase() == "textarea" || // Some catch-alls
node.tagName.toLowerCase() == "input"))
);
}
function traverseAndObserveDocument(value) {
document.title = replaceText(document.title, value);
walk(document.body, value);
window.addEventListener("message", (e) => {
if (e.data.type === "REPLACE_COVID") {
window.__replacementStringCovid = e.data.value;
document.title = replaceText(document.title, value);
walk(document.body, value);
}
});
const title = document.getElementsByTagName("title")[0];
const observer = (mutations) => {
for (const mutation of mutations) {
for (const node of mutation.addedNodes) {
if (isForbiddenNode(node)) {
// Should never operate on user-editable content
continue;
} else if (node.nodeType === 3) {
// Replace the text for text nodes
handleText(node, value);
} else {
// Otherwise, find text nodes within the given node and replace text
walk(node, value);
}
}
}
};
const observerConfig = {
characterData: true,
childList: true,
subtree: true,
};
const bodyObserver = new MutationObserver(observer);
const titleObserver = new MutationObserver(observer);
bodyObserver.observe(document.body, observerConfig);
if (title) {
titleObserver.observe(title, observerConfig);
}
}