-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent_script.js
68 lines (57 loc) · 2.22 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
// Wait for document ready before executing main function
var readyStateCheckInterval = setInterval(function() {
if (document.readyState === "interactive") {
clearInterval(readyStateCheckInterval);
main();
}
}, 10);
function main() {
// Replace page title
document.title = generateReplacment(document.title);
// Replace all initial text on page
replaceNodeText(document.body);
// Create a mutation observer to monitor the DOM for changes
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
Array.prototype.slice.call(mutation.addedNodes).forEach(replaceNodeText);
});
});
// Configure and start the observer
observer.observe(document.body, {
childList: true,
subtree: true,
attributes: false,
characterData: false
});
}
function replaceNodeText(node) {
// Do nothing for nodes contained in a Google Docs editor (sorry I.E. no support for you)
if(node.closest && node.closest('.kix-appview')) return;
// Create a tree walker to traverse all text nodes
var walker = document.createTreeWalker(
node,
NodeFilter.SHOW_TEXT,
{
acceptNode: function(node) {
// Reject contentEditable nodes
return (node.parentElement && node.parentElement.isContentEditable) ?
NodeFilter.FILTER_SKIP :
NodeFilter.FILTER_ACCEPT;
}
},
false
);
// Replace all text nodes
var textNode;
while(textNode = walker.nextNode()) {
textNode.nodeValue = generateReplacment(textNode.nodeValue);
}
}
function generateReplacment(text) {
// TODO: perform with a single replace command
return text
.replace(/\bFlorida Man\b/g, "Man Likely Suffering From Mental Illness and/or Drug Addiction")
.replace(/\bFlorida man\b/g, "man likely suffering from mental illness and/or drug addiction")
.replace(/\bFlorida Woman\b/g, "Woman Likely Suffering From Mental Illness and/or Drug Addiction")
.replace(/\bFlorida woman\b/g, "woman likely suffering from mental illness and/or drug addiction");
}