-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
129 lines (124 loc) · 3.69 KB
/
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
(async() => {
const isBlocked = await chrome.runtime.sendMessage({
action: 'is-blocked'
});
if(isBlocked) {
return;
}
const words = await chrome.runtime.sendMessage({
action: 'get-words',
});
if(words.length <= 0) {
return;
}
let nodes = [];
const excludeNodes = ['META', 'STYLE', 'SCRIPT', 'HEAD', 'INPUT', 'TEXTAREA', 'PRE', 'CODE', 'KBD']
function setChildren(node) {
for (const child of node.childNodes) {
if(child.nodeType === Node.ELEMENT_NODE && !excludeNodes.includes(child.nodeName)) {
setChildren(child);
} else if (child.nodeType === Node.TEXT_NODE && child.data !== "\n") {
nodes.push(child);
}
}
}
setChildren(document.body);
const keys = words.map(i => i[0])
var highlight = new Highlight();
var ranges = [];
nodes.map(node => {
let content = node.textContent.toLowerCase();
let textList = content.split(' ');
textList.map((text, index) => {
if(keys.includes(text)) {
const range = document.createRange();
const startOffset = content.indexOf(text);
range.setStart(node, startOffset);
range.setEnd(node, startOffset + text.length);
ranges.push({'word': text, 'range': range, 'value': words[keys.indexOf(text)][1]});
highlight.add(range);
}
});
// keys.map((text, index) => {
// const matches = content.match(new RegExp(text, "i"));
// if(matches) {
// for(const match of matches) {
// const range = document.createRange();
// ranges.push({'word': text, 'range': range, 'value': words[index][1]});
// const startOffset = content.indexOf(text);
// range.setStart(node, startOffset);
// range.setEnd(node, startOffset + text.length);
// highlight.add(range);
// }
// }
// });
});
// apply highlight css
CSS.highlights.set('lexibridge', highlight);
document.body.addEventListener("click", (e) => {
const target = document.caretRangeFromPoint(e.clientX, e.clientY);
let has_range = false;
for(const [range] of highlight.entries()) {
if(range.isPointInRange(target.startContainer, target.startOffset)) {
e.preventDefault();
const rect = range.getBoundingClientRect();
has_range = true;
const value = ranges.find(i => i.range === range)
// create popover
const popover = document.createElement("div");
popover.popover = "auto";
popover.classList.add("lexibridge-popover");
const title = document.createElement("h5");
title.textContent = value.word;
popover.appendChild(title);
const speakBtn = document.createElement("span");
speakBtn.innerHTML = '🔈';
speakBtn.addEventListener("click", () => {
chrome.runtime.sendMessage({
action: 'speak',
data: {
word: value.word,
}
});
});
title.appendChild(speakBtn);
const p = document.createElement("p");
p.textContent = value.value;
popover.appendChild(p);
const buttons = document.createElement("div");
const btn = document.createElement("button");
btn.textContent = "关闭(ESC)";
btn.addEventListener("click", () => {
popover.remove();
});
buttons.appendChild(btn);
const delBtn = document.createElement("button");
delBtn.textContent = "从词库删除";
delBtn.addEventListener("click", async() => {
await chrome.runtime.sendMessage({
action: 'delete-word',
data: {
word: value.word,
}
});
popover.remove();
ranges.map(j => {
if(j.word === value.word) {
highlight.delete(j.range);
}
});
});
buttons.appendChild(delBtn);
popover.appendChild(buttons);
popover.addEventListener("toggle", (e) => {
if(e.newState === "closed") {
popover.remove();
}
});
document.body.appendChild(popover);
popover.showPopover();
return;
}
}
});
})();