-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
64 lines (57 loc) · 1.74 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// background.js
let darkModeEnabled = true;
chrome.storage.sync.get({
darkModeEnabled: true,
backgroundColor: "#222222",
textColor: "#eeeeee",
linkColor: "#88ccff",
}, function (items) {
darkModeEnabled = items.darkModeEnabled;
updateActionTitle();
});
chrome.action.onClicked.addListener(function (tab) {
if (tab.url && tab.url.startsWith("chrome://")) return;
darkModeEnabled = !darkModeEnabled;
updateActionTitle();
chrome.storage.sync.set({ darkModeEnabled: darkModeEnabled }, function () {
applyDarkModeToTab(tab, darkModeEnabled);
});
});
function updateActionTitle() {
const title = darkModeEnabled ? "Disable Dark Mode" : "Enable Dark Mode";
chrome.action.setTitle({ title: title });
}
function applyDarkModeToTab(tab, darkModeEnabled) {
if (tab.url && !tab.url.startsWith("chrome://")) {
chrome.storage.sync.get({
backgroundColor: "#222222",
textColor: "#eeeeee",
linkColor: "#88ccff",
}, function (items) {
chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ["content.js"],
});
});
}
}
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (changeInfo.status === "complete") {
applyDarkModeToTab(tab, darkModeEnabled);
}
});
chrome.storage.onChanged.addListener(function (changes, namespace) {
if (namespace === "sync") {
if (changes.darkModeEnabled) {
darkModeEnabled = changes.darkModeEnabled.newValue;
updateActionTitle();
}
if (changes.darkModeEnabled || changes.backgroundColor || changes.textColor || changes.linkColor) {
chrome.tabs.query({}, function (tabs) {
tabs.forEach(function (tab) {
applyDarkModeToTab(tab, darkModeEnabled);
});
});
}
}
});