-
Notifications
You must be signed in to change notification settings - Fork 0
/
content-script.js
81 lines (68 loc) · 2.48 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
69
70
71
72
73
74
75
76
77
78
79
80
minWordLength = 1;
minTextLength = 50;
boldRatio = 0.4;
flashBorder =false;
function insertTextBefore(text, node, bold) {
if (bold) {
var span = document.createElement("span");
span.style.fontWeight = "bolder";
span.appendChild(document.createTextNode(text));
node.parentNode.insertBefore(span, node);
}
else {
node.parentNode.insertBefore(document.createTextNode(text), node);
}
}
function startProcess(){
chrome.storage.sync.get("flashColor", function (result) {
if (result.flashColor!=null){
flashBorder=result.flashColor;
}
else{
flashBorder=false;
}
processNode(document.body);
});
}
function processNode(node) {
var walker = document.createTreeWalker(node, NodeFilter.SHOW_TEXT, {
acceptNode: (node) => {
return (
node.parentNode.nodeName !== 'SCRIPT' &&
node.parentNode.nodeName !== 'NOSCRIPT' &&
node.parentNode.nodeName !== 'STYLE' &&
node.nodeValue.length >= minTextLength) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT;
}
});
var node;
while (node = walker.nextNode()) {
var text = node.nodeValue;
var wStart = -1, wLen = 0, eng = false;
// English letters only
for (var i = 0; i <= text.length; i++) { // We use <= here because we want to include the last character in the loop
var cEng = i < text.length ? /[a-zA-Z]/.test(text[i]) : false;
if (i == text.length || eng !== cEng) {
// State flipped or end of string
if (eng && wLen >= minWordLength) {
var word = text.substring(wStart, wStart + wLen);
var numBold = Math.ceil(word.length * boldRatio);
var bt = word.substring(0, numBold), nt = word.substring(numBold);
insertTextBefore(bt, node, true);
insertTextBefore(nt, node, false);
} else if (wLen > 0) {
var word = text.substring(wStart, wStart + wLen);
insertTextBefore(word, node, false);
}
wStart = i;
wLen = 1;
eng = cEng;
} else {
wLen++;
}
}
node.nodeValue = ""; // Can't remove the node (otherwise the tree walker will break) so just set it to empty
}
if (flashBorder)
document.body.style.border = "2px solid red";;
}
startProcess();