From 1ce6a16686b9dc7c0624a9f586ec77ff8267a04b Mon Sep 17 00:00:00 2001 From: Hygna Date: Thu, 20 Oct 2022 16:25:17 +0100 Subject: [PATCH] Fixed all settings changed except the last one set not saving (for real this time) Closes https://github.com/libredirect/libredirect/issues/473 --- src/assets/javascripts/utils.js | 72 ++++++++++++++++----------- src/pages/options/widgets/services.js | 11 ++-- 2 files changed, 49 insertions(+), 34 deletions(-) diff --git a/src/assets/javascripts/utils.js b/src/assets/javascripts/utils.js index 66a439b..9a2d490 100644 --- a/src/assets/javascripts/utils.js +++ b/src/assets/javascripts/utils.js @@ -134,26 +134,32 @@ async function processDefaultCustomInstances(service, frontend, network, documen calcFrontendCheckBoxes() frontendNetworkElement.getElementsByClassName("toggle-all")[0].addEventListener("change", async event => { - if (event.target.checked) frontendDefaultRedirects = [...redirects[frontend][network]] - else frontendDefaultRedirects = [] + browser.storage.local.get("options", r => { + let options = r.options + if (event.target.checked) frontendDefaultRedirects = [...redirects[frontend][network]] + else frontendDefaultRedirects = [] - options[frontend][network].enabled = frontendDefaultRedirects - browser.storage.local.set({ options }) - calcFrontendCheckBoxes() + options[frontend][network].enabled = frontendDefaultRedirects + browser.storage.local.set({ options }) + calcFrontendCheckBoxes() + }) }) for (let element of frontendCheckListElement.getElementsByTagName("input")) { if (element.className != "toggle-all") frontendNetworkElement.getElementsByClassName(element.className)[0].addEventListener("change", async event => { - if (event.target.checked) frontendDefaultRedirects.push(element.className) - else { - let index = frontendDefaultRedirects.indexOf(element.className) - if (index > -1) frontendDefaultRedirects.splice(index, 1) - } + browser.storage.local.get("options", r => { + let options = r.options + if (event.target.checked) frontendDefaultRedirects.push(element.className) + else { + let index = frontendDefaultRedirects.indexOf(element.className) + if (index > -1) frontendDefaultRedirects.splice(index, 1) + } - options[frontend][network].enabled = frontendDefaultRedirects - browser.storage.local.set({ options }) - calcFrontendCheckBoxes() + options[frontend][network].enabled = frontendDefaultRedirects + browser.storage.local.set({ options }) + calcFrontendCheckBoxes() + }) }) } @@ -174,29 +180,35 @@ async function processDefaultCustomInstances(service, frontend, network, documen for (const item of frontendCustomInstances) { frontendNetworkElement.getElementsByClassName(`clear-${item}`)[0].addEventListener("click", async () => { - let index = frontendCustomInstances.indexOf(item) - if (index > -1) frontendCustomInstances.splice(index, 1) - options[frontend][network].custom = frontendCustomInstances - browser.storage.local.set({ options }) - calcFrontendCustomInstances() + browser.storage.local.get("options", r => { + let options = r.options + let index = frontendCustomInstances.indexOf(item) + if (index > -1) frontendCustomInstances.splice(index, 1) + options[frontend][network].custom = frontendCustomInstances + browser.storage.local.set({ options }) + calcFrontendCustomInstances() + }) }) } } calcFrontendCustomInstances() frontendNetworkElement.getElementsByClassName("custom-instance-form")[0].addEventListener("submit", async event => { - event.preventDefault() - let frontendCustomInstanceInput = frontendNetworkElement.getElementsByClassName("custom-instance")[0] - let url = new URL(frontendCustomInstanceInput.value) - let protocolHostVar = protocolHost(url) - if (frontendCustomInstanceInput.validity.valid && !redirects[frontend][network].includes(protocolHostVar)) { - if (!frontendCustomInstances.includes(protocolHostVar)) { - frontendCustomInstances.push(protocolHostVar) - options[frontend][network].custom = frontendCustomInstances - browser.storage.local.set({ options }) - frontendCustomInstanceInput.value = "" + browser.storage.local.get("options", r => { + let options = r.options + event.preventDefault() + let frontendCustomInstanceInput = frontendNetworkElement.getElementsByClassName("custom-instance")[0] + let url = new URL(frontendCustomInstanceInput.value) + let protocolHostVar = protocolHost(url) + if (frontendCustomInstanceInput.validity.valid && !redirects[frontend][network].includes(protocolHostVar)) { + if (!frontendCustomInstances.includes(protocolHostVar)) { + frontendCustomInstances.push(protocolHostVar) + options[frontend][network].custom = frontendCustomInstances + browser.storage.local.set({ options }) + frontendCustomInstanceInput.value = "" + } + calcFrontendCustomInstances() } - calcFrontendCustomInstances() - } + }) }) } diff --git a/src/pages/options/widgets/services.js b/src/pages/options/widgets/services.js index eb7f1ba..37385fa 100644 --- a/src/pages/options/widgets/services.js +++ b/src/pages/options/widgets/services.js @@ -85,10 +85,13 @@ for (const service in config.services) { else divs[service][option].value = options[service][option] divs[service][option].addEventListener("change", () => { - if (typeof config.services[service].options[option] == "boolean") options[service][option] = divs[service][option].checked - else options[service][option] = divs[service][option].value - browser.storage.local.set({ options }) - changeFrontendsSettings(service) + browser.storage.local.get("options", r => { + let options = r.options + if (typeof config.services[service].options[option] == "boolean") options[service][option] = divs[service][option].checked + else options[service][option] = divs[service][option].value + browser.storage.local.set({ options }) + changeFrontendsSettings(service) + }) }) }