-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcontent.js
More file actions
42 lines (34 loc) · 1.36 KB
/
content.js
File metadata and controls
42 lines (34 loc) · 1.36 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
'use strict';
let _selectionTimeout;
function updateSelection() {
const selectedText = document.getSelection().toString().trim();
try {
chrome.storage.local.set({ selectedText });
} catch {
document.removeEventListener('selectionchange', onSelectionChange);
document.removeEventListener('visibilitychange', onVisibilityChange);
}
}
function onSelectionChange() {
clearTimeout(_selectionTimeout);
_selectionTimeout = setTimeout(() => updateSelection(), 100);
}
function onVisibilityChange() {
clearTimeout(_selectionTimeout);
if (document.visibilityState === 'visible') {
_selectionTimeout = setTimeout(() => updateSelection(), 300);
}
}
document.addEventListener('selectionchange', onSelectionChange, { passive: true });
document.addEventListener('visibilitychange', onVisibilityChange, { passive: true });
// Workaround for https://bugs.chromium.org/p/chromium/issues/detail?id=893175
const prefersSchemeMedia = window.matchMedia('(prefers-color-scheme: dark)');
prefersSchemeMedia.addEventListener('change', updatePrefersScheme, { passive: true });
function updatePrefersScheme() {
try {
chrome.storage.local.set({ prefersScheme: prefersSchemeMedia.matches ? 'dark' : 'light' });
} catch {
prefersSchemeMedia.removeEventListener('change', updatePrefersScheme);
}
}
updatePrefersScheme();