-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathca3rav.js
54 lines (50 loc) · 1.52 KB
/
ca3rav.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
var walker =document.createTreeWalker(
document.body,
NodeFilter.SHOW_TEXT,
{ acceptNode: function(node) { return NodeFilter.FILTER_ACCEPT; } },
false);
while(walker.nextNode()){
var cur = walker.currentNode;
var txt = cur.textContent;
if(isWrong(txt))
continue;
translate(cur);
}
function getSavedCats(callback) {
chrome.storage.sync.get("cats", function (type) {
callback(type["cats"]);
});
}
getSavedCats(function(cats) {
if(cats === true){
var ilist = document.images;
for(var i = 0; i < ilist.length; i++){
ilist[i].src = "https://thecatapi.com/api/images/get?format=src&type=gif&rand=" + Math.random();
ilist[i].srcset = "";
}
}
});
function isWrong(txt){
return isEmpty(txt) || isCode(txt) || isTooShort(txt);
}
function isCode(txt){
return count(txt, "{") + count(txt, "}") + count(txt, "<") + count(txt, ">") >= 2
}
function isEmpty(txt){
return txt == null || txt.trim().length == 0;
}
function isTooShort(txt){
return txt.trim().length < 3;
}
function count(s1, s2) {
return (s1.length - s1.replace(new RegExp(s2,"g"), '').length) / s2.length;
}
function translate(element){
// string extensionId, any message, object options, function responseCallback
chrome.runtime.sendMessage(element.textContent, function(response) {
// if(element.parentElement !== null)
element.parentElement.innerHTML = response;
// else
// element.textContent = response;
});
}