-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
47 lines (42 loc) · 1.34 KB
/
background.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
async function setForcedColors(debugee, color, scheme) {
return chrome.debugger.sendCommand(debugee, "Emulation.setEmulatedMedia", {
features: [
{ name: "forced-colors", value: color },
{ name: "prefers-color-scheme", value: scheme },
],
});
}
async function forcedColorEnabled(tabId) {
const [{ result }] = await chrome.scripting.executeScript({
target: { tabId: tabId },
func: () => window.matchMedia("(forced-colors: active)").matches,
});
return result;
}
async function prefersDarkEnabled(tabId) {
const [{ result }] = await chrome.scripting.executeScript({
target: { tabId: tabId },
func: () => window.matchMedia("(prefers-color-scheme: dark)").matches,
});
return result;
}
chrome.action.onClicked.addListener(async (tab) => {
const tabId = tab.id;
const debugee = { tabId: tabId };
try {
await chrome.debugger.attach(debugee, "1.3");
} catch {
/* If it's already attached */
}
// Cycle between:
// - forced color with dark mode
// - forced color with light mode
// - nothing
if ((await forcedColorEnabled(tabId)) && (await prefersDarkEnabled(tabId))) {
await setForcedColors(debugee, "active", "light");
} else if (await forcedColorEnabled(tabId)) {
await setForcedColors(debugee, "none", "");
} else {
await setForcedColors(debugee, "active", "dark");
}
});