forked from domdomegg/hideytthumbnails-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.js
57 lines (50 loc) · 2.22 KB
/
options.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
/** @typedef {import("./common")} */
document.addEventListener('DOMContentLoaded', async () => {
// Localize page
document.querySelectorAll('[data-i18n]').forEach(elem => {
const translated = browser.i18n.getMessage(elem.dataset.i18n);
if (translated) {
elem.innerText = translated;
}
})
// Load existing settings
const options = await loadOptions();
document.forms[0].syncSettings.checked = options.syncSettings;
document.forms[0].thumbnailMode.value = options.thumbnailMode || 'blurred-except-hover';
document.forms[0].disableSearchResultPage.checked = options.disabledOnPages.results;
document.forms[0].disableChannelPage.checked = options.disabledOnPages.channel;
document.forms[0].disablePlaylistPage.checked = options.disabledOnPages.playlist;
document.forms[0].disableWatchPage.checked = options.disabledOnPages.watch;
document.forms[0].disableSubscriptionsPage.checked = options.disabledOnPages.subscriptions;
document.forms[0].disableEverywhere.checked = options.disabledOnPages.everywhere;
});
// Save on change
document.forms[0].addEventListener('change', async () => {
const status = document.getElementById('status');
status.textContent = `⏳ ${browser.i18n.getMessage('options_saving')}`
await saveOptions({
syncSettings: document.forms[0].syncSettings.checked,
thumbnailMode: document.forms[0].thumbnailMode.value,
disabledOnPages: {
results: document.forms[0].disableSearchResultPage.checked,
channel: document.forms[0].disableChannelPage.checked,
playlist: document.forms[0].disablePlaylistPage.checked,
watch: document.forms[0].disableWatchPage.checked,
subscriptions: document.forms[0].disableSubscriptionsPage.checked,
everywhere: document.forms[0].disableEverywhere.checked,
},
})
// Artificial delay, so the 'saving' message actually appears
await new Promise(resolve => setTimeout(resolve, 200))
status.textContent = `✅ ${browser.i18n.getMessage('options_saved')}`;
});
/**
* @param {Options} options
* @returns {Promise<void>}
*/
const saveOptions = async (options) => new Promise((resolve) => {
browser.storage.local.set(options, resolve);
if (options.syncSettings) {
browser.storage.sync.set(options, resolve);
}
})