-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpopup.js
More file actions
60 lines (51 loc) · 1.79 KB
/
popup.js
File metadata and controls
60 lines (51 loc) · 1.79 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
const MODE_LIGHT = 'light';
const MODE_DARK = 'dark';
document.addEventListener('DOMContentLoaded', async () => {
const modeToggle = document.getElementById('mode-toggle');
const colorButtons = Array.from(document.querySelectorAll('.color-button'));
let activeMode = MODE_LIGHT;
try {
const response =
await browser.runtime.sendMessage({action: 'getColorMode'});
activeMode = response?.mode === MODE_DARK ? MODE_DARK : MODE_LIGHT;
} catch (_) {
activeMode = MODE_LIGHT;
}
modeToggle.checked = activeMode === MODE_DARK;
applyModeVisibility(colorButtons, activeMode);
applyPopupTheme(activeMode);
modeToggle.addEventListener('change', async () => {
const requestedMode = modeToggle.checked ? MODE_DARK : MODE_LIGHT;
let nextMode = requestedMode;
try {
const response = await browser.runtime.sendMessage({
action: 'setColorMode',
mode: requestedMode,
});
nextMode = response?.mode === MODE_DARK ? MODE_DARK : MODE_LIGHT;
} catch (_) {
nextMode = requestedMode;
}
modeToggle.checked = nextMode === MODE_DARK;
applyModeVisibility(colorButtons, nextMode);
applyPopupTheme(nextMode);
});
colorButtons.forEach((button) => {
button.addEventListener('click', async () => {
await browser.runtime.sendMessage({
action: 'setWindowColor',
color: button.dataset.color,
});
});
});
});
function applyModeVisibility(buttons, mode) {
buttons.forEach((button) => {
const isDarkSwatch = button.classList.contains('dark-swatch');
const isVisibleInMode = mode === MODE_DARK ? isDarkSwatch : !isDarkSwatch;
button.classList.toggle('mode-hidden', !isVisibleInMode);
});
}
function applyPopupTheme(mode) {
document.body.classList.toggle('popup-dark', mode === MODE_DARK);
}