blob: 16294ab8d79361dcdc8a8e2d5903c4d71d15d78b [file] [log] [blame]
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +02001import optionsPrototype from './optionsPrototype.json5';
2import specialOptions from './specialOptions.json5';
3
4export {optionsPrototype, specialOptions};
5
6// Adds missing options with their default value. If |dryRun| is set to false,
7// they are also saved to the sync storage area.
8export function cleanUpOptions(options, dryRun = false) {
9 console.log('[cleanUpOptions] Previous options', JSON.stringify(options));
10
11 if (typeof options !== 'object' || options === null) options = {};
12
13 var ok = true;
14 for (const [opt, optMeta] of Object.entries(optionsPrototype)) {
15 if (!(opt in options)) {
16 ok = false;
17 options[opt] = optMeta['defaultValue'];
18 }
19 }
20
21 console.log('[cleanUpOptions] New options', JSON.stringify(options));
22
23 if (!ok && !dryRun) {
24 chrome.storage.sync.set(options);
25 }
26
27 return options;
28}
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020029
30// Returns a promise which returns the values of options |options| which are
31// stored in the sync storage area.
32export function getOptions(options) {
33 // Once we only target MV3, this can be greatly simplified.
34 return new Promise(
35 (resolve, reject) => {chrome.storage.sync.get(options, items => {
36 if (chrome.runtime.lastError) return reject(chrome.runtime.lastError);
37
38 resolve(items);
39 })});
40}
41
42// Returns a promise which returns whether the |option| option/feature is
43// currently enabled.
44export function isOptionEnabled(option) {
45 return getOptions(option).then(options => {
46 return options?.[option] === true;
47 });
48}