Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions ImageBlockX/background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@

/**
* On startup, check whether we have stored settings.
* If we don't, then store the default settings.
* If we don't, then store and return the default settings.
*
* @param {ImageBlockXState} storedSettings - the settings as found in the browser local storage
* @return {ImageBlockXState} storedSettings if valid, the default settings otherwise.
*/
const checkStoredSettings = storedSettings => {
/** @type {ImageBlockXState} */
Expand All @@ -40,14 +41,20 @@ const checkStoredSettings = storedSettings => {
};

// TODO: Find way to merge settings if incomplete (how could that happen however?)
if (!storedSettings.pattern ||
!storedSettings.isBlocking ||
!storedSettings.isReloadingOnToggle ||
!storedSettings.ui) {
if (!("pattern" in storedSettings) ||
!("isBlocking" in storedSettings) ||
!("isReloadingOnToggle" in storedSettings) ||
!("ui" in storedSettings)) {
console.debug("Stored settings not found, loading default");
browser.storage.local.set(defaultState).then(
() => console.debug("Default settings loaded")
);

return defaultState;
} else {
console.debug("Valid stored settings found.");

return storedSettings;
}
};

Expand Down Expand Up @@ -166,9 +173,13 @@ const toggleBlocking = settings => {
(() => {
browser.storage.local.get().then(
settings => {
checkStoredSettings(settings);
settings = checkStoredSettings(settings);
updateState(settings);
updateUI(settings);

// TODO: a hack to register the blocking handler.
toggleBlocking(settings);
toggleBlocking(settings);
},
e => console.error(e));

Expand Down