-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththeme.js
38 lines (33 loc) · 1.04 KB
/
theme.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
const cssPath = chrome.runtime.getURL("override.css");
let cssPromise = fetch(cssPath).then(response => response.text());
const applyDarkMode = () => {
cssPromise.then(css => {
let s = document.createElement("style");
s.type = "text/css";
s.innerHTML = css;
s.setAttribute("isSlackDarkMode", "true");
document.head.appendChild(s);
});
}
const togglePageState = () => {
const element = document.querySelector("style[isSlackDarkMode='true']");
if (element) {
element.parentNode.removeChild(element);
} else {
applyDarkMode();
}
return !element;
}
chrome.extension.onMessage.addListener(function (msg, sender, sendResponse) {
const currentState = togglePageState();
sendResponse(currentState);
chrome.storage.sync.set({
isSlackDarkMode: currentState
});
});
chrome.extension.sendMessage({
type: "newTabOpened"
});
chrome.storage.sync.get(["isSlackDarkMode"], storage => storage.isSlackDarkMode ? (applyDarkMode(), chrome.extension.sendMessage({
type: "applyDarkMode"
})) : null);