-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontentScript.js
53 lines (48 loc) · 1.8 KB
/
contentScript.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
var totalCount = 0;
var zwMatches = new RegExp("[\u200B-\u200D\uFEFF]","g");
let walker = document.createTreeWalker(
document,
NodeFilter.SHOW_TEXT,
{ acceptNode:
function(node) {
if( node.nodeType == Node.TEXT_NODE
&& node.parentNode.nodeName !== "SCRIPT"
&& node.nodeValue.match(zwMatches)
){
totalCount++;
return NodeFilter.FILTER_ACCEPT;
}
else{
//FILTER_SKIP should keep the children of this node in consideration.
return NodeFilter.FILTER_SKIP;
}
}
},
false);
while(walker.nextNode());
//send message to background page to update the badge text
chrome.runtime.sendMessage({message: "updateBadgeNumber", value:totalCount}, function(response) {
});
//catch selected text and send message to background page
document.onselectionchange = function() {
var selection = document.getSelection();
if(testForZeroWidthCharacters(selection.toString())){
chrome.storage.sync.set({
selectedText: selection.toString(),
selectedTextClean:removeZeroWidthCharacters(selection.toString()),
selectedTextEmoji:replaceZeroWidthCharacters(selection.toString()),
},function(){});
/* show message*/
chrome.runtime.sendMessage({message: "warnUserOfZeroWidthChars", value:selection.toString()}, function(response) {
});
}
};
function testForZeroWidthCharacters(text){
return text.match(zwMatches);
}
function removeZeroWidthCharacters(text){
return text.replace(zwMatches, "");
}
function replaceZeroWidthCharacters(text){
return text.replace(zwMatches, "💀");
}