-
Notifications
You must be signed in to change notification settings - Fork 0
/
tpe.js
90 lines (76 loc) · 2.65 KB
/
tpe.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
console.log("Load tpe.js");
let replacements = [];
let updateReplacements = (replacementTable) => {
for(keyword in replacementTable) {
let replacement = replacementTable[keyword];
replacements.push({
"replace": new RegExp(keyword, 'g'),
"to": replacement
});
}
}
let replace = (node) => {
let needReplceFlag = false;
if (node.nodeName == "#text" && node.textContent.length > 0) {
let text = node.textContent;
replacements.forEach((replacement) => {
text = text.replace(replacement.replace, replacement.to);
});
if (text != node.textContent) {
//console.log(node.textContent + ' -> ' + text);
node.textContent = text;
}
if (text.indexOf("🇹🇼") >= 0) {
return true;
} else {
return false;
}
} else if (node.nodeName == "IMG") {
// Special case for replacing emoji for Facebook and Twitter
if (node.alt && node.alt == "🇹🇼" && node.src && node.src.indexOf("emoji") > 0) {
node.src = chrome.runtime.getURL("tpe.png");
} else if (node.src && node.src.indexOf("1f1f9_1f1fc") > 0 && node.src.indexOf("emoji.php") > 0) {
node.src = chrome.runtime.getURL("tpe.png");
}
} else {
node.childNodes.forEach((childNode) => {
needReplceFlag = needReplceFlag || replace(childNode);
});
if (needReplceFlag) {
if (!node.classList.contains("tpe-flag-font")) {
node.classList.add("tpe-flag-font");
}
// Special case for replacing emoji background for Facebook
if (node.style.backgroundImage.indexOf("emoji.php") > 0) {
node.style.backgroundImage = 'url("' + chrome.runtime.getURL("tpe.png") + '")';
}
}
}
return false;
};
let taiwanObserver = new window.MutationObserver((mutationList, observer) => {
mutationList.forEach((mutation) => {
let type = mutation.type;
if (type != "childList") {
return;
}
let addedNodes = mutation.addedNodes;
addedNodes.forEach((node) => {
if (["SCRIPT", "STYLE"].indexOf(node.nodeName) >= 0) {
return;
}
replace(node);
});
});
});
fetch(chrome.runtime.getURL("replacement.json")).then((response) => {
return response.json();
}).then((jsonObject => {
let replacementTable = jsonObject;
updateReplacements(replacementTable);
replace(document.body);
taiwanObserver.observe(document, {
subtree: true,
childList: true
});
}));