-
Notifications
You must be signed in to change notification settings - Fork 1
/
messenger.js
61 lines (57 loc) · 1.8 KB
/
messenger.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
"use strict";
{
const m = matchMedia("(prefers-color-scheme:dark)");
const returnAuto = () => m.matches;
const returnFalse = () => false;
const returnTrue = () => true;
let isDark;
async function w(s, d = document) {
let e = d.querySelector(s);
while (!e) {
await new Promise((d) => requestIdleCallback(d));
e = d.querySelector(s);
}
return e;
}
async function u() {
if (
document.documentElement.classList.contains("__fb-dark-mode") !== isDark()
) {
const banner = await w('div[role="banner"]');
const menuOpener =
banner.lastElementChild.firstElementChild.firstElementChild
.firstElementChild.firstElementChild;
const menuAlreadyOpen = banner.querySelector(
'[data-pagelet="root"] hr ~ hr + div > * >:nth-child(3)'
);
if (!menuAlreadyOpen) menuOpener.click();
const menu = await w(
'[data-pagelet="root"] hr ~ hr + div > * >:nth-child(3)',
banner
);
menu.firstElementChild.click();
const toggle = isDark() ? "on" : "off";
const radio = await w(
`input[name="dark-mode"][value="${toggle}"]`,
menu.parentElement.parentElement.parentElement.parentElement
.nextElementSibling
);
radio.click();
if (!menuAlreadyOpen) menuOpener.click();
}
}
function c(options) {
if (typeof options.theme === "string")
options.theme = { newValue: options.theme };
if (options?.theme == null || options.theme.newValue === "auto") {
m.addEventListener("change", u);
isDark = returnAuto;
} else {
m.removeEventListener("change", u);
isDark = options.theme.newValue === "dark" ? returnTrue : returnFalse;
}
u();
}
chrome.storage.onChanged.addListener(c);
chrome.storage.sync.get("theme", c);
}