-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
53 lines (48 loc) · 1.48 KB
/
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
var format = function(word, colors) {
var string = "";
for (var i = 0; i < word.length; i++) {
string += "<span style='color: " + colors[i%colors.length] + "'>" + word.charAt(i) + "</span>";
}
return string;
};
var image = function(word, image) {
return word +
"<span style='position:relative;top:-5px'>" +
"<img src='" + image + "' width='20em'>" +
"</span>";
}
chrome.storage.sync.get('words', function(data) {
var words = data.words;
if (words === undefined)
words = Array();
var queue = [document.body];
var curr, word, replace, color;
String.prototype.replaceAll = function(find, replace) {
var target = this;
return target.replace(new RegExp(find, 'g'), replace);
};
while (curr = queue.pop()) {
words.forEach(function(wordPair) {
word = wordPair[0];
if (curr.textContent.match(word)) {
color = wordPair[2];
replace = wordPair[1];
if (color !== null && color !== undefined) {
replace = format(replace, color);
}
for (var i = 0; i < curr.childNodes.length; ++i) {
switch (curr.childNodes[i].nodeType) {
case Node.TEXT_NODE : // 3
if (curr.childNodes[i].textContent.match(word)) {
curr.innerHTML = curr.innerHTML.replaceAll(word, replace);
}
break;
case Node.ELEMENT_NODE : // 1
queue.push(curr.childNodes[i]);
break;
}
}
}
});
}
});