-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
40 lines (33 loc) · 1.2 KB
/
popup.js
File metadata and controls
40 lines (33 loc) · 1.2 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
document.addEventListener("DOMContentLoaded", async function () {
const toggle = document.getElementById("darkModeToggle");
const status = document.getElementById("status");
// Get current tab
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
// Get current dark mode state
const result = await chrome.storage.sync.get([`darkMode_${tab.url}`]);
const isDarkMode = result[`darkMode_${tab.url}`] || false;
// Set toggle state
toggle.checked = isDarkMode;
updateStatus(isDarkMode);
// Handle toggle change
toggle.addEventListener("change", async function () {
const isEnabled = toggle.checked;
// Save state
await chrome.storage.sync.set({ [`darkMode_${tab.url}`]: isEnabled });
// Send message to content script
try {
await chrome.tabs.sendMessage(tab.id, {
action: "toggleDarkMode",
enabled: isEnabled,
});
updateStatus(isEnabled);
} catch (error) {
console.log("Error sending message:", error);
// Reload the page if content script isn't loaded
chrome.tabs.reload(tab.id);
}
});
function updateStatus(isDark) {
status.textContent = isDark ? "Dark mode ON" : "Dark mode OFF";
}
});