-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
42 lines (23 loc) · 1.3 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
function changeKievToKyiv(from, to){ //We create a function that manages to obtain every reference to Kiev on the web page.
getEveryTextNode().forEach(function(node){ //The getEveryTextNode() function is defined further down below in our code.
node.nodeValue = node.nodeValue.replace(new RegExp(changeMaker(from), 'g'), to);
});
function getEveryTextNode(){
var result = [];
(function nodeScanner(node){
if(node.childNodes.length){
for(var i = 0; i < node.childNodes.length; i++){
nodeScanner(node.childNodes[i]);
}
}else if(node.nodeType == Node.TEXT_NODE){
result.push(node);
}
})(document);
return result;
}
function changeMaker(str){ //We use this regular expression to catch instances of "Kiev" so that we can change it to Kyiv.
return (str+'').replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1"); //This is the exact regular expression we use. We use the "g" flag to ensure that we catch every instance of Kiev on the webpage.
}
}
// We use this segment to inject the content script into the particular web page.
window.onload = changeKievToKyiv("Kiev", "Kyiv"); //This particular line means that as soon as the window loads, then it injects the content script.