-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
57 lines (52 loc) · 1.32 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
function genAmogus() {
const img = document.createElement('img');
img.crossOrigin = 'Anonymous';
img.setAttribute('style', 'height: 1em !important; width: auto !important; display: inline-block !important');
img.setAttribute('alt', 'among');
img.src = browser.runtime.getURL('amogus.png');
return img;
}
function recur(node) {
const tag = node.tagName?.toLowerCase();
if(tag == 'script' || tag == 'style') return;
for(let i = 0; i < node.childNodes.length; ++i) {
const c = node.childNodes[i];
if(c.nodeType == Node.TEXT_NODE) {
const content = c.textContent
.replace(/among/gi, 'among')
.split('among')
.map(v => [document.createTextNode(v), genAmogus()])
.flat();
content.pop();
if(content.length > 1) {
c.replaceWith(...content);
}
}
else if(c.nodeType == Node.ELEMENT_NODE) {
recur(c);
}
}
}
function onMutate(mutations) {
for(const m of mutations) {
try {
if(m.type == 'childList') {
for(const n of m.addedNodes) {
recur(n);
}
}
} catch(e) {
console.log('amogusify onMutate:', m, e);
}
}
}
const config = { childList: true, subtree: true };
const observer = new MutationObserver(onMutate);
addEventListener('load', function() {
try {
recur(document.body);
observer.observe(document.body, config);
} catch(e) {
console.log('amogusify init:', e);
}
});