Repository navigation
Expand file tree
/
Copy pathoptions.js
More file actions
executable file
·68 lines (63 loc) · 2.36 KB
/
Copy pathoptions.js
File metadata and controls
executable file
·68 lines (63 loc) · 2.36 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
61
62
63
64
65
66
67
68
'use strict';
const getOptions = async isDefault => {
const message = isDefault ? "getDefaultOptions" : "getOptions"
const response = await chrome.runtime.sendMessage({ message });
return response.options;
}
const restoreOptions = async () => {
const options = await getOptions();
for (let i = 1; i <= options.maxCount; ++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];
}
document.getElementById('createSubmenusCheckbox').checked = options['createSubmenus'];
};
const saveOptions = async defaultFormatID => {
let options = await getOptions();
try {
if (defaultFormatID) {
options.defaultFormat = defaultFormatID;
}
for (let i = 1; i <= options.maxCount; ++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;
}
options['createSubmenus'] = document.getElementById('createSubmenusCheckbox').checked;
} catch (err) {
console.error("failed to get options", err);
}
try {
await chrome.storage.sync.set(options);
} catch (err) {
console.error("failed to save options", err);
}
try {
await chrome.runtime.sendMessage({
message: 'createContextMenus',
options,
});
} catch (err) {
console.error("failed to update context menu", err);
}
}
const restoreDefaults = async () => {
const options = await getOptions(true);
for (let i = 1; i <= options.maxCount; ++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];
}
document.getElementById('createSubmenusCheckbox').checked = options['createSubmenus'];
await saveOptions(options['defaultFormat']);
}
document.addEventListener('DOMContentLoaded', async () => {
await restoreOptions();
document.getElementById('saveButton').addEventListener('click', async e => {
await saveOptions();
});
document.getElementById('restoreDefaultsButton').addEventListener('click', async e => {
await restoreDefaults();
});
});