-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
utils.js
43 lines (35 loc) · 1.11 KB
/
utils.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
/**
* require: migration function
*/
;(() => {
window.utils = {
loadPreference: async () => {
let results = await browser.storage.local.get()
let syncResults = await browser.storage.sync.get()
results.list = results.list && results.list.sort((a, b) => b.time - a.time)
if ((typeof results.length === 'number') && (results.length > 0)) {
results = results[0]
}
if (!results.version && !syncResults.version) {
await browser.storage.local.set(defaultPreference)
return defaultPreference
}
if (results.version === defaultPreference.version) {
return results
}
else {
if (window.migration) {
await window.migration(results, syncResults)
}
}
let updateKeys = Object.keys(defaultPreference).filter( key => results[key] === undefined)
if (updateKeys.length === 0) { return results; }
let update = updateKeys.reduce( (obj, key) => ({
...obj,
key: defaultPreference[key]
}), results)
await browser.storage.local.set(update)
return update
}
}
})()