-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
44 lines (37 loc) · 1.12 KB
/
popup.js
File metadata and controls
44 lines (37 loc) · 1.12 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
;(() => {
const STORAGE_KEY = 'enabled'
const DEFAULT_ENABLED = true
const toggle = document.getElementById('enabled-toggle')
const statusText = document.getElementById('status-text')
function render(enabled) {
toggle.checked = enabled
statusText.textContent = enabled
? 'Auto-focus is currently ON.'
: 'Auto-focus is currently OFF.'
}
async function loadEnabledState() {
try {
const result = await browser.storage.local.get(STORAGE_KEY)
const value = result[STORAGE_KEY]
return typeof value === 'boolean' ? value : DEFAULT_ENABLED
} catch {
return DEFAULT_ENABLED
}
}
async function saveEnabledState(enabled) {
await browser.storage.local.set({ [STORAGE_KEY]: enabled })
}
browser.storage.onChanged.addListener((changes, areaName) => {
if (areaName !== 'local' || !changes[STORAGE_KEY]) return
const nextValue = changes[STORAGE_KEY].newValue
if (typeof nextValue !== 'boolean') return
render(nextValue)
})
toggle.addEventListener('change', () => {
void saveEnabledState(toggle.checked)
})
void (async () => {
const enabled = await loadEnabledState()
render(enabled)
})()
})()