-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.ts
42 lines (39 loc) · 1.16 KB
/
options.ts
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
// Salvar as alterações
const inputs = Array.from(
document.querySelectorAll<HTMLInputElement | HTMLSelectElement>('input, select')
);
inputs.forEach(input => {
input.addEventListener('change', () => {
const promise =
input.value.trim() === ''
? browser.storage.local.remove(input.id)
: browser.storage.local.set({ [input.id]: input.value });
promise.catch(err => console.error(err));
});
});
const inputsById = new Map(
inputs.map(input => [input.id, input] as [string, HTMLInputElement | HTMLSelectElement])
);
// Observar mudanças feitas em outras páginas
browser.storage.onChanged.addListener((changes, areaName) => {
if (areaName !== 'local') return;
const changed = Object.keys(changes);
changed.forEach(key => {
const input = inputsById.get(key);
if (input) {
input.value = ((changes as any)[key] as browser.storage.StorageChange).newValue || '';
}
});
});
// Carregar os valores salvos
browser.storage.local
.get(Array.from(inputsById.keys()))
.then(prefs => {
Object.keys(prefs).forEach(key => {
const input = inputsById.get(key);
if (input) {
input.value = prefs[key];
}
});
})
.catch(err => console.error(err));