-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontent_script.js
91 lines (80 loc) · 2.16 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
81
82
83
84
85
86
87
88
89
90
let dict;
const GERMAN = "de";
const SPANISH = "es";
const supported_languages = [GERMAN, SPANISH];
const dictionaries = {"de": "german_dictionary.json", "es": "spanish_dictionary.json"};
const regexes = {
"de": new RegExp('[A-ZÄÖÜ][a-zäöüß]+|(<[^>]*>)','g'), // German nouns
"es": new RegExp('[A-ZÑÁÉÍÓÚÜa-zñáéíóúü]+|(<[^>]*>)', 'g'), // Spanish words (not just nouns)
};
let fcolor = "blue";
let mcolor = "red";
let ncolor = "green";
let acolor = "purple";
chrome.storage.sync.get(['femColor', 'mascColor', 'neutColor', 'ambColor'], function(result) {
fcolor = result.femColor;
mcolor = result.mascColor;
ncolor = result.neutColor;
acolor = result.ambColor;
});
chrome.storage.sync.get(['ext_active'], function(result) {
if (result.ext_active == 'on') {
chrome.i18n.detectLanguage(document.body.innerText, function(result) {
if (result.languages.length > 0) {
colorize(result.languages[0].language);
}
})
}
});
function colorize(language) {
if (supported_languages.indexOf(language) < 0) {
return;
}
url = chrome.runtime.getURL('lexicon/' + dictionaries[language]);
fetch(url).then(f => f.json()).then(j => {dict = j; walkAndReplace(language)}).catch(e => console.log(e));
}
function walkAndReplace(language) {
const re = regexes[language];
const walker = document.createTreeWalker(
document.body,
NodeFilter.SHOW_TEXT,
node => node.textContent.match(re) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP,
false
);
const nodes = [];
while (walker.nextNode()) {
nodes.push(walker.currentNode);
}
nodes.forEach(function(node) {
if (node.parentNode) {
node.parentNode.innerHTML = (node.parentNode.innerHTML.replace(re, wrapper))
}
});
}
function wrapper(match) {
let color = "black";
let gender = null;
if (dict.hasOwnProperty(match)) {
gender = dict[match];
switch (gender) {
case "Masc":
color = mcolor;
break;
case "Fem":
case "Plur":
color = fcolor;
break;
case "MascFem":
case "Ambiguous":
color = acolor;
break;
case "Neut":
color = ncolor;
break
default:
color = "black";
}
return `<span style="color: ${color}">${match}</span>`;
}
return match
}