-
Notifications
You must be signed in to change notification settings - Fork 8
/
options.js
77 lines (74 loc) · 2.99 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
async function restoreOptions() {
const options = await gettingOptions();
for (let i = 1; i <= 9; ++i) {
document.getElementById('title'+i).value = options['title'+i] || '';
document.getElementById('format'+i).value = options['format'+i] || '';
document.getElementById('html'+i).checked = !!options['html'+i];
}
for (let name of VARIABLE_NAMES) {
document.getElementById(name+'_format').value = options[name+'_format'] || '';
}
document.getElementById('enableContextMenusCheckbox').checked = options['enableContextMenus'];
document.getElementById('createSubmenusCheckbox').checked = options['createSubmenus'];
}
async function saveOptions(defaultFormatID) {
let options;
try {
options = defaultFormatID ?
{'defaultFormat': defaultFormatID} : await gettingOptions();
for (let i = 1; i <= 9; ++i) {
options['title'+i] = document.getElementById('title'+i).value;
options['format'+i] = document.getElementById('format'+i).value;
options['html'+i] = document.getElementById('html'+i).checked ? 1 : 0;
}
for (let name of VARIABLE_NAMES) {
options[name+'_format'] = document.getElementById(name+'_format').value;
}
options['enableContextMenus'] = document.getElementById('enableContextMenusCheckbox').checked;
options['createSubmenus'] = document.getElementById('createSubmenusCheckbox').checked;
} catch (err) {
console.error("failed to get options", err);
}
try {
await browser.storage.sync.set(options);
} catch (err) {
console.error("failed to save options", err);
}
try {
await createContextMenus(options);
} catch (err) {
console.error("failed to update context menu", err);
}
}
async function restoreDefaults() {
for (let i = 1; i <= 9; ++i) {
document.getElementById('title'+i).value = DEFAULT_OPTIONS['title'+i] || '';
document.getElementById('format'+i).value = DEFAULT_OPTIONS['format'+i] || '';
document.getElementById('html'+i).checked = DEFAULT_OPTIONS['html'+i] || 0;
}
for (let name of VARIABLE_NAMES) {
document.getElementById(name+'_format').value = DEFAULT_OPTIONS[name+'_format'] || '';
}
document.getElementById('enableContextMenusCheckbox').checked = DEFAULT_OPTIONS['enableContextMenus'];
document.getElementById('createSubmenusCheckbox').checked = DEFAULT_OPTIONS['createSubmenus'];
return saveOptions(DEFAULT_OPTIONS['defaultFormat']);
}
async function init() {
await restoreOptions();
document.getElementById('enableContextMenusCheckbox').
addEventListener('click', function(e) {
document.getElementById('createSubmenusCheckbox').disabled =
!document.getElementById('enableContextMenusCheckbox').checked;
});
document.getElementById('saveButton').
addEventListener('click', function(e) {
e.preventDefault();
saveOptions();
});
document.getElementById('restoreDefaultsButton').
addEventListener('click', function(e) {
e.preventDefault();
restoreDefaults();
});
}
document.addEventListener('DOMContentLoaded', init);