-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadaptiveTheme.js
More file actions
168 lines (154 loc) · 5.05 KB
/
Copy pathadaptiveTheme.js
File metadata and controls
168 lines (154 loc) · 5.05 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
let query = null;
browser.runtime.onMessage.addListener((message, _, sendResponse) => {
switch (message.header) {
case "GET_color":
query = message.query;
message.dynamic ? enableDynamic() : disableDynamic();
sendResponse(getcolor());
break;
case "SET_THEME_color":
setThemecolor(message.color);
break;
default:
break;
}
});
function getcolor() {
return {
theme: getThemecolor(),
page: getPagecolor(),
query: getQuerycolor(),
};
}
function getThemecolor() {
const metaThemecolor = document.querySelector(`meta[name="theme-color"]:not([media])`,);
const metaThemecolorLight = document.querySelector(`meta[name="theme-color"][media="(prefers-color-scheme: light)"]`,) ?? metaThemecolor;
const metaThemecolorDark = document.querySelector(`meta[name="theme-color"][media="(prefers-color-scheme: dark)"]`,) ?? metaThemecolor;
return {
light: metaThemecolorLight?.content,
dark: metaThemecolorDark?.content,
};
}
function getPagecolor() {
return document.elementsFromPoint(window.innerWidth / 2, 3).filter((element) => element.offsetWidth >= window.innerWidth * 0.9 && element.offsetHeight >= 20, ).map((element) => getElementcolor(element)).concat( getElementcolor(document.body), getElementcolor(document.documentElement),).filter((color) => color !== undefined);
}
function getQuerycolor() {
try {
return query ? getElementcolor(document.querySelector(query)) : undefined;
} catch (error) {
return undefined;
}
}
function getElementcolor(element) {
if (element instanceof Element) {
const style = getComputedStyle(element);
return {
color: style.backgroundColor,
opacity: style.opacity,
filter: style.filter,
};
}
}
const darkReaderObserver = new MutationObserver(sendcolor);
const metaThemecolorObserver = new MutationObserver(sendcolor);
const metaTagObserver = new MutationObserver((mutationList) =>
mutationList.forEach((mutation) => {
mutation.addedNodes.forEach((node) => {
if (node.nodeName === "META" && node.name === "theme-color") {
sendcolor();
metaThemecolorObserver.observe(node, {
attributes: true,
});
}
});
}),
);
const styleTagObserver = new MutationObserver((mutationList) => {
if (
mutationList.some((mutation) => [...mutation.addedNodes, ...mutation.removedNodes].some(
(node) => node.nodeName === "STYLE",),
)
) {
sendcolor();
}
});
function enableDynamic() {
["click", "resize", "scroll", "visibilitychange"].forEach((event) => document.addEventListener(event, sendcolor),
);
[
"transitionend",
"transitioncancel",
"animationend",
"animationcancel",
].forEach((transitionEvent) => document.addEventListener(transitionEvent, sendcolorRequiresFocus),
);
darkReaderObserver.observe(document.documentElement, {
attributes: true,
attributeFilter: ["data-darkreader-mode"],
});
document.querySelectorAll("meta[name=theme-color]").forEach((metaTag) =>
metaThemecolorObserver.observe(metaTag, { attributes: true }),
);
if (document.head)
metaTagObserver.observe(document.head, { childList: true });
styleTagObserver.observe(document.documentElement, { childList: true });
if (document.head)
styleTagObserver.observe(document.head, { childList: true });
}
function disableDynamic() {
["click", "resize", "scroll", "visibilitychange"].forEach((event) => document.removeEventListener(event, sendcolor),
);
[
"transitionend",
"transitioncancel",
"animationend",
"animationcancel",
].forEach((transitionEvent) =>
document.removeEventListener(transitionEvent, sendcolorRequiresFocus),
);
darkReaderObserver.disconnect();
metaThemecolorObserver.disconnect();
metaTagObserver.disconnect();
styleTagObserver.disconnect();
}
function setThemecolor(color) {
const metaThemecolorList = document.querySelectorAll(
`meta[name="theme-color"]`,
);
const newMetaThemecolor = document.createElement("meta");
newMetaThemecolor.name = "theme-color";
newMetaThemecolor.content = color;
(document.head || document.documentElement).appendChild(newMetaThemecolor);
metaThemecolorList.forEach((metaThemecolor) => metaThemecolor.remove());
}
let dispatchTimeout;
let lastSentAt = 0;
const throttleIntervalMs = 250;
async function sendcolor() {
clearTimeout(dispatchTimeout);
const remaining = throttleIntervalMs + lastSentAt - Date.now();
const dispatch = async () => {
if (document.visibilityState !== "visible") return;
lastSentAt = Date.now();
try {
await browser.runtime.sendMessage({
header: "UPDATE_color",
color: getcolor(),
});
} catch (error) {
console.warn("Failed to send color to Adaptive Theme background.");
}
};
remaining <= 0 ? await dispatch() : (dispatchTimeout = setTimeout(dispatch, remaining));
}
async function sendcolorRequiresFocus() {
if (document.hasFocus()) await sendcolor();
}
(async function sendMessageOnLoad(attempt = 0) {
try {
await browser.runtime.sendMessage({ header: "SCRIPT_READY" });
} catch {
attempt >= 3 ? console.error("Could not connect to Adaptive Theme background.") : console.warn("Failed to connect to Adaptive Theme background.");
if (attempt < 60) setTimeout(() => sendMessageOnLoad(++attempt), 1000);
}
})();