From a26115154a492bd66ca5ea004eb10342f3dcbf2d Mon Sep 17 00:00:00 2001 From: tophf Date: Wed, 11 Nov 2020 13:26:26 +0300 Subject: [PATCH] fix getPrefs error on browser startup in the active tab --- js/prefs.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/js/prefs.js b/js/prefs.js index dc7e812a..4ecdafc3 100644 --- a/js/prefs.js +++ b/js/prefs.js @@ -119,11 +119,9 @@ window.INJECTED !== 1 && (() => { 'storage.sync': ['get', 'set'], }); } - const initializing = ( - msg.isBg - ? browser.storage.sync.get(STORAGE_KEY).then(res => res[STORAGE_KEY]) - : API.getPrefs() - ).then(setAll); + // getPrefs may fail on browser startup in the active tab as it loads before the background script + const initializing = (msg.isBg ? readStorage() : API.getPrefs().catch(readStorage)) + .then(setAll); chrome.storage.onChanged.addListener(async (changes, area) => { 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() { return browser.storage.sync.set({[STORAGE_KEY]: values}); }