-
Notifications
You must be signed in to change notification settings - Fork 1
/
content.js
61 lines (48 loc) · 1.5 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
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
// Content Script listener responds on messages dispatched by main.js (main extension file)
// takes in a command input distructured to {action,color,phrase}
chrome.extension.onMessage.addListener(function ({action,color,phrase}, sender, sendResponse) {
if (action == 'erase') {
console.log(phrase)
erase(phrase)
sendResponse({});
}
if (action == 'mark') {
highlighte({color,phrase})
sendResponse({});
}
if (action == 'erase all'){
location.reload(true);
}
});
/**
* Erase the previously highlighted word/phrase/
* @param {} phrase} - word/phrase to highlite
*/
erase = (phrase) =>{
if (window.find && window.getSelection) {
document.designMode = "on";
var sel = window.getSelection();
sel.collapse(document.body, 0);
while (window.find(phrase)) {
document.execCommand("RemoveFormat", false, null);
}
document.designMode = "off";
}
return sel;
}
/**
* Highlighte specific keyword and set highliter color
* @param {} {color - color of highlighter
* @param {} phrase} - word/phrase to highlite
*/
highlighte = ({color,phrase}) =>{
if (window.find && window.getSelection) {
document.designMode = "on";
var sel = window.getSelection();
sel.collapse(document.body, 0);
while (window.find(phrase)) {
document.execCommand("HiliteColor", false, color);
}
document.designMode = "off";
}
}