Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 1 | import optionsPrototype from './optionsPrototype.json5'; |
| 2 | import specialOptions from './specialOptions.json5'; |
| 3 | |
| 4 | export {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. |
| 8 | export function cleanUpOptions(options, dryRun = false) { |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 9 | console.debug('[cleanUpOptions] Previous options', JSON.stringify(options)); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 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 | |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 21 | console.debug('[cleanUpOptions] New options', JSON.stringify(options)); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 22 | |
| 23 | if (!ok && !dryRun) { |
| 24 | chrome.storage.sync.set(options); |
| 25 | } |
| 26 | |
| 27 | return options; |
| 28 | } |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 29 | |
| 30 | // Returns a promise which returns the values of options |options| which are |
| 31 | // stored in the sync storage area. |
| 32 | export function getOptions(options) { |
| 33 | // Once we only target MV3, this can be greatly simplified. |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 34 | return new Promise((resolve, reject) => { |
| 35 | if (typeof options === 'string') |
| 36 | options = [options, '_forceDisabledFeatures']; |
Adrià Vilanova Martínez | 3dbbb45 | 2021-09-06 19:13:43 +0200 | [diff] [blame] | 37 | else if (Array.isArray(options)) |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 38 | options = [...options, '_forceDisabledFeatures']; |
| 39 | else if (options !== null) |
Adrià Vilanova Martínez | 3dbbb45 | 2021-09-06 19:13:43 +0200 | [diff] [blame] | 40 | console.error( |
| 41 | 'Unexpected |options| parameter of type ' + (typeof options) + |
| 42 | ' (expected: string, array, or null).'); |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 43 | |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 44 | chrome.storage.sync.get(options, items => { |
| 45 | if (chrome.runtime.lastError) return reject(chrome.runtime.lastError); |
| 46 | |
| 47 | // Handle applicable kill switches which force disable features |
| 48 | if (items?._forceDisabledFeatures) { |
| 49 | for (let feature of items?._forceDisabledFeatures) { |
| 50 | items[feature] = false; |
| 51 | } |
| 52 | |
| 53 | delete items._forceDisabledFeatures; |
| 54 | } |
| 55 | |
| 56 | resolve(items); |
| 57 | }); |
| 58 | }); |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | // Returns a promise which returns whether the |option| option/feature is |
| 62 | // currently enabled. |
| 63 | export function isOptionEnabled(option) { |
| 64 | return getOptions(option).then(options => { |
| 65 | return options?.[option] === true; |
| 66 | }); |
| 67 | } |