-
Notifications
You must be signed in to change notification settings - Fork 0
/
highlightKanji.js
66 lines (58 loc) · 1.79 KB
/
highlightKanji.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
var innerText = document.body.innerText;
function replaceText(node, oldChar, newChar)
{
var current = node.nodeValue;
var replaced = current.replace(oldChar, newChar);
node.nodeValue = replaced;
}
function traverse(node, oldChar, newChar)
{
var children = node.childNodes;
var childLen = children.length;
for(var i = 0; i < childLen; i++)
{
var child = children.item(i);
if(child.nodeType == 3)//or if(child instanceof Text)
replaceText(child, oldChar, newChar);
else
traverse(child, oldChar, newChar);
}
}
chrome.storage.sync.get("api_key", function(data) {
var api_key = data.api_key;
console.log(api_key);
var xhr = new XMLHttpRequest();
xhr.onload = function () {
var json = xhr.responseText;
json = JSON.parse(json);
if (json.error) {
console.log("error processing API request");
console.log(json.error.message);
} else {
// Build up a list of all known kanji
var knownKanji = {};
console.log("Known kanji: " + json.requested_information.length);
for (var i = 0; i < json.requested_information.length; ++i)
{
//console.log(json.requested_information[i].character);
//knownKanji.push(json.requested_information[i].character);
knownKanji[json.requested_information[i].character] = true;
traverse(document.body, json.requested_information[i].character, "Z");
}
/*
for (var i = 0; i < innerText.length; ++i)
{
if (knownKanji[innerText[i]] != undefined)
{
innerText[i] = 'Z';
console.log("contains " + innerText[i]);
}
}
*/
document.body.innerText = innerText;
}
};
var url = "http://www.wanikani.com/api/v1.1/user/" + encodeURIComponent(api_key) + "/kanji";
xhr.open("GET", url);
xhr.send();
});