Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
345 lines (320 loc) · 12.1 KB
/
Copy pathscript.js
File metadata and controls
345 lines (320 loc) · 12.1 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
(async() => {
const {t} = LexiBridgeI18n;
let {words = [], blocked = [], pack, highlightStyle = 'underline'} =
await chrome.storage.local.get(['words', 'blocked', 'pack', 'highlightStyle']);
let dict = LexiBridge.buildDictionary(words);
let defLang = LexiBridgePacks.getPack(pack).defLang;
words = null;
let enabled = !blocked.includes(location.hostname);
// Each style is a separately named highlight, styled in app.css.
const highlight = new Highlight();
let highlightName = null;
function applyStyle(style) {
if(highlightName) CSS.highlights.delete(highlightName);
highlightName = `lexibridge-${style}`;
CSS.highlights.set(highlightName, highlight);
}
applyStyle(highlightStyle);
let entries = []; // [{word, range}]
let seen = new WeakSet();
const excluded = 'meta, style, script, noscript, head, input, textarea, select, pre, code, kbd, svg, .lexibridge-ui';
function acceptText(node) {
const parent = node.parentElement;
if(!parent || !node.data.trim()) {
return false;
}
return !parent.closest(excluded) && !parent.isContentEditable;
}
// only: highlight just these words (newly added ones) in already seen text.
function highlightText(node, only) {
seen.add(node);
for(const m of LexiBridge.findMatches(node.data, dict)) {
if(only && !only.has(m.word)) continue;
const range = document.createRange();
range.setStart(node, m.start);
range.setEnd(node, m.end);
entries.push({word: m.word, range});
highlight.add(range);
}
}
function scan(root, only) {
// Unseen nodes with `only` are left to the full scan of pending mutations.
const accept = (n) => acceptText(n) && (only ? seen.has(n) : !seen.has(n));
if(root.nodeType === Node.TEXT_NODE) {
if(accept(root)) highlightText(root, only);
return;
}
if(root.nodeType !== Node.ELEMENT_NODE || root.closest(excluded)) {
return;
}
const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT, {
acceptNode: (n) => accept(n) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT
});
while(walker.nextNode()) {
highlightText(walker.currentNode, only);
}
report();
}
function removeEntries(shouldRemove) {
entries = entries.filter(e => {
if(shouldRemove(e)) {
highlight.delete(e.range);
return false;
}
return true;
});
report();
}
// The toolbar badge shows how many distinct words are highlighted here.
// Sent once things settle, and only when the number changes.
let reported = null;
let reportTimer = null;
function report() {
clearTimeout(reportTimer);
reportTimer = setTimeout(() => {
const count = enabled ? new Set(entries.map(e => e.word)).size : 0;
if(count === reported) return;
reported = count;
try {
chrome.runtime.sendMessage({action: 'badge', data: {count}}).catch(() => {});
} catch {} // extension reloaded: this page keeps an orphaned script
}, 500);
}
// Pages restored from the back/forward cache lost their badge.
addEventListener('pageshow', (e) => {
if(e.persisted) {
reported = null;
report();
}
});
function setEnabled(on) {
enabled = on;
highlight.clear();
entries = [];
seen = new WeakSet();
LexiBridgeUI.closeAll();
if(on) scan(document.body);
report();
}
if(enabled) scan(document.body);
// Content added later (SPA navigation, infinite scroll, lazy loading).
let pending = [];
let timer = null;
new MutationObserver((mutations) => {
if(!enabled) return;
for(const m of mutations) {
pending.push(...m.addedNodes);
}
if(!timer) {
timer = setTimeout(() => {
const nodes = pending;
pending = [];
timer = null;
// Ranges in removed text are moved out of it by the browser and collapse.
removeEntries(e => e.range.collapsed);
nodes.forEach(n => n.isConnected && scan(n));
}, 300);
}
}).observe(document.body, {childList: true, subtree: true});
// Settings changed here, in another tab, the popup or the options page.
chrome.storage.onChanged.addListener((changes, area) => {
if(area !== 'local') {
return;
}
if(changes.highlightStyle) {
applyStyle(changes.highlightStyle.newValue ?? 'underline');
}
if(changes.pack) {
defLang = LexiBridgePacks.getPack(changes.pack.newValue).defLang;
}
if(changes.blocked) {
const on = !(changes.blocked.newValue ?? []).includes(location.hostname);
if(on !== enabled) setEnabled(on);
}
if(changes.words) {
const old = dict;
dict = LexiBridge.buildDictionary(changes.words.newValue ?? []);
if(!enabled) return;
removeEntries(e => !dict.has(e.word));
const added = new Set([...dict.keys()].filter(k => !old.has(k)));
if(added.size) {
scan(document.body, added);
}
}
});
// The popup asks how many distinct words are highlighted on this page.
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if(request.action === 'page-stats') {
sendResponse({enabled, count: new Set(entries.map(e => e.word)).size});
}
});
async function getWords() {
return (await chrome.storage.local.get(['words'])).words ?? [];
}
async function addWord(word, definition) {
const rest = (await getWords()).filter(i => LexiBridge.normalizeWord(i[0]) !== word);
await chrome.storage.local.set({'words': [[word, definition], ...rest]});
}
// Returns a function that puts the word back where it was.
async function removeWord(word) {
const words = await getWords();
const index = words.findIndex(i => LexiBridge.normalizeWord(i[0]) === word);
if(index < 0) return async() => {};
const row = words[index];
await chrome.storage.local.set({'words': words.filter((_, i) => i !== index)});
return async() => {
const now = await getWords();
if(now.some(i => LexiBridge.normalizeWord(i[0]) === word)) return;
now.splice(Math.min(index, now.length), 0, row);
await chrome.storage.local.set({'words': now});
};
}
const speak = (word) => chrome.runtime.sendMessage({action: 'speak', data: {word}});
const band = (word) => chrome.runtime.sendMessage({action: 'band', data: {word}});
// The highlighted word under the pointer, if any.
function entryAt(x, y) {
const target = document.caretRangeFromPoint(x, y);
if(!target) {
return null;
}
// caretRangeFromPoint also answers for blank space next to a word.
const inside = (r) => x >= r.left && x <= r.right && y >= r.top && y <= r.bottom;
return entries.find(i => i.range.isPointInRange(target.startContainer, target.startOffset) &&
[...i.range.getClientRects()].some(inside)) ?? null;
}
// Words inside links and buttons keep their click; their card opens on hover.
const interactive = 'a[href], button, [role="button"], [role="link"]';
const inInteractive = (entry) => !!entry.range.startContainer.parentElement?.closest(interactive);
function showWordCard(entry) {
const {word, range} = entry;
return LexiBridgeUI.showCard({
word,
surface: range.toString(),
definition: dict.get(word),
lang: defLang,
band: band(word),
anchor: () => range.getBoundingClientRect(),
state: 'known',
onSpeak: () => speak(word),
// The pass over the word hides its highlights going, here and wherever
// else it is in view; other tabs follow through storage.onChanged.
onKnown: async() => {
// Only where the text shows: clipped, off-canvas or covered text (screen
// reader headings, closed menus) has rects too.
const shown = (r, parent) => {
if(!r.width || r.bottom <= 0 || r.top >= window.innerHeight) return false;
const hit = document.elementFromPoint(r.left + r.width / 2, r.top + r.height / 2);
return !!hit && (hit === parent || parent.contains(hit));
};
const rects = (e) => [...e.range.getClientRects()].filter(r => shown(r, e.range.startContainer.parentElement));
const also = entries.filter(e => e.word === word && e !== entry).flatMap(rects).slice(0, 40);
await LexiBridgeUI.erase(rects(entry), also);
removeEntries(e => e.word === word);
const undo = await removeWord(word);
LexiBridgeUI.chip(() => range.getBoundingClientRect(), t('card_knownDone'), t('card_removed', word), t('common_undo'), undo);
},
});
}
// Click a highlighted word: show its card.
document.body.addEventListener("click", (e) => {
if(!enabled || !window.getSelection().isCollapsed || e.target.closest?.('.lexibridge-ui')) {
return;
}
const entry = entryAt(e.clientX, e.clientY);
if(!entry || inInteractive(entry)) {
return;
}
e.preventDefault();
showWordCard(entry);
});
// Hover a highlighted word in a link: show its card after a pause, and
// close it once the pointer has left both the word and the card.
const HOVER_OPEN_MS = 500, HOVER_CLOSE_MS = 300;
let hoverEntry = null, hoverCard = null, openTimer = null, closeTimer = null;
function onPointer(e) {
const overCard = !!e.target.closest?.('.lexibridge-ui');
const entry = overCard || !e.target.closest?.(interactive) ? null : entryAt(e.clientX, e.clientY);
const candidate = entry && inInteractive(entry) ? entry : null;
if(hoverCard?.isConnected) {
const stay = overCard || candidate === hoverEntry;
if(stay) {
clearTimeout(closeTimer);
closeTimer = null;
} else if(!closeTimer) {
closeTimer = setTimeout(() => {
hoverCard?.hidePopover();
hoverCard = hoverEntry = closeTimer = null;
}, HOVER_CLOSE_MS);
}
return;
}
if(candidate === hoverEntry) {
return;
}
clearTimeout(openTimer);
hoverEntry = candidate;
if(candidate) {
openTimer = setTimeout(() => {
if(enabled && hoverEntry === candidate) hoverCard = showWordCard(candidate);
}, HOVER_OPEN_MS);
}
}
let pointerFrame = null;
document.addEventListener("mousemove", (e) => {
if(!enabled || pointerFrame) return;
pointerFrame = requestAnimationFrame(() => {
pointerFrame = null;
onPointer(e);
});
}, {passive: true});
// Select a word that is not in the list yet: offer to add it.
document.addEventListener("mouseup", (e) => {
if(!enabled || e.target.closest?.('.lexibridge-ui')) {
return;
}
// Let the browser finish updating the selection first.
setTimeout(() => {
LexiBridgeUI.hidePill();
const selection = window.getSelection();
const text = selection.toString().trim();
if(selection.isCollapsed || !LexiBridge.isWord(text) || LexiBridge.lookup(dict, LexiBridge.normalizeWord(text))) {
return;
}
const node = selection.anchorNode;
const parent = node?.nodeType === Node.TEXT_NODE ? node.parentElement : node;
if(!parent || parent.closest('input, textarea, .lexibridge-ui') || parent.isContentEditable) {
return;
}
const range = selection.getRangeAt(0).cloneRange();
LexiBridgeUI.showPill(range.getBoundingClientRect(), () => addFromSelection(text, range));
});
});
document.addEventListener("mousedown", (e) => {
if(!e.target.closest?.('.lexibridge-ui')) LexiBridgeUI.hidePill();
});
window.addEventListener("scroll", () => LexiBridgeUI.hidePill(), {passive: true});
async function addFromSelection(text, range) {
// Base form and definition from the bundled word lists, if known.
const {word, definition} = await chrome.runtime.sendMessage({action: 'lookup', data: {word: text}});
const card = {
word,
surface: text,
lang: defLang,
band: band(word),
anchor: () => range.getBoundingClientRect(),
onSpeak: () => speak(word),
};
if(definition) {
// One click: add it and show what it means, with a way back. The
// selection goes so the new highlight shows.
await addWord(word, definition);
window.getSelection().removeAllRanges();
LexiBridgeUI.showCard({...card, definition, state: 'added', onUndoAdd: () => removeWord(word)});
} else {
LexiBridgeUI.showCard({...card, definition: '', state: 'new', onSave: async(value) => {
await addWord(word, value);
window.getSelection().removeAllRanges();
}});
}
}
})();