-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
31 lines (28 loc) · 1.39 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
chrome.storage.sync.get(["englishDictionary", "translatedDictionary"], function(result) {
var theAPIKey = "AIzaSyBttL3_rUfMaP8vZQazT8bCd5XhHkmR4lA";
var englishDictionary = result.englishDictionary;
var translatedDictionary = result.translatedDictionary;
var elements = document.getElementsByTagName('*');
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
for (var k = 0; k < englishDictionary.length; k++) {
for (var j = 0; j < element.childNodes.length; j++) {
var node = element.childNodes[j];
if (node.nodeType === 3) {
var text = node.nodeValue;
var searchWord = new RegExp('\\b' + englishDictionary[k] + '\\b', "gi");
var replacedText = text.replace(searchWord, translatedDictionary[k]);
// if (replacedText == null || replacedText == "") {
// element.replaceChild(document.createTextNode(replacedText), englishDictionary[k]);
// }
// translatedDictionary[k] = englishDictionary[k];
if (replacedText !== text) {
element.replaceChild(document.createTextNode(replacedText), node);
}
}
}
}
}
console.log(translatedDictionary);
console.log(englishDictionary);
});