fix getPrefs error on browser startup in the active tab

This commit is contained in:
tophf 2020-11-11 13:26:26 +03:00
parent da6361637d
commit a26115154a

View File

@ -119,11 +119,9 @@ window.INJECTED !== 1 && (() => {
'storage.sync': ['get', 'set'], 'storage.sync': ['get', 'set'],
}); });
} }
const initializing = ( // getPrefs may fail on browser startup in the active tab as it loads before the background script
msg.isBg const initializing = (msg.isBg ? readStorage() : API.getPrefs().catch(readStorage))
? browser.storage.sync.get(STORAGE_KEY).then(res => res[STORAGE_KEY]) .then(setAll);
: API.getPrefs()
).then(setAll);
chrome.storage.onChanged.addListener(async (changes, area) => { chrome.storage.onChanged.addListener(async (changes, area) => {
const data = area === 'sync' && changes[STORAGE_KEY]; const data = area === 'sync' && changes[STORAGE_KEY];
@ -237,6 +235,14 @@ window.INJECTED !== 1 && (() => {
} }
} }
function readStorage() {
/* Using a non-promisified call since this code may also run in a content script
when API.getPrefs occasionally fails during browser startup in the active tab */
return new Promise(resolve =>
chrome.storage.sync.get(STORAGE_KEY, data =>
resolve(data[STORAGE_KEY])));
}
function updateStorage() { function updateStorage() {
return browser.storage.sync.set({[STORAGE_KEY]: values}); return browser.storage.sync.set({[STORAGE_KEY]: values});
} }