about summary refs log tree commit diff stats
path: root/src/assets
diff options
context:
space:
mode:
authorManeraKai <manerakai@protonmail.com>2024-08-18 13:00:57 +0300
committerManeraKai <manerakai@protonmail.com>2024-08-18 13:00:57 +0300
commita94ad51a346d58f090e4d9a26fe5e78f15474acd (patch)
treed4afb1271ab03e0a6147fc4e2894a08509e1f14c /src/assets
parentMerge pull request #973 from monstorix/master (diff)
downloadlibredirect-a94ad51a346d58f090e4d9a26fe5e78f15474acd.zip
Allowing to import from previous version https://github.com/libredirect/browser_extension/issues/961
Diffstat (limited to 'src/assets')
-rw-r--r--src/assets/javascripts/services.js134
1 files changed, 60 insertions, 74 deletions
diff --git a/src/assets/javascripts/services.js b/src/assets/javascripts/services.js
index 417f66c3..9b6ef325 100644
--- a/src/assets/javascripts/services.js
+++ b/src/assets/javascripts/services.js
@@ -831,99 +831,86 @@ const defaultInstances = {
   ytify: ["https://ytify.netlify.app"],
 }
 
+async function getDefaults() {
+  let config = await utils.getConfig()
+  let options = {}
+  for (const service in config.services) {
+    options[service] = {}
+    for (const defaultOption in config.services[service].options) {
+      options[service][defaultOption] = config.services[service].options[defaultOption]
+    }
+    for (const frontend in config.services[service].frontends) {
+      if (config.services[service].frontends[frontend].instanceList) {
+        options[frontend] = []
+      }
+    }
+  }
+  options.exceptions = {
+    url: [],
+    regex: [],
+  }
+  options.theme = "detect"
+  options.popupServices = ["youtube", "tiktok", "imgur", "reddit", "quora", "translate", "maps"]
+  options.fetchInstances = "github"
+  options.redirectOnlyInIncognito = false
+  options = { ...options, ...defaultInstances }
+  return options
+}
+
 function initDefaults() {
   return new Promise(resolve => {
     browser.storage.local.clear(async () => {
-      let config = await utils.getConfig()
-      let options = {}
-      for (const service in config.services) {
-        options[service] = {}
-        for (const defaultOption in config.services[service].options) {
-          options[service][defaultOption] = config.services[service].options[defaultOption]
-        }
-        for (const frontend in config.services[service].frontends) {
-          if (config.services[service].frontends[frontend].instanceList) {
-            options[frontend] = []
-          }
-        }
-      }
-      options.exceptions = {
-        url: [],
-        regex: [],
-      }
-      options.theme = "detect"
-      options.popupServices = ["youtube", "tiktok", "imgur", "reddit", "quora", "translate", "maps"]
-      options.fetchInstances = "github"
-      options.redirectOnlyInIncognito = false
-
-      options = { ...options, ...defaultInstances }
-
+      options = await getDefaults()
       browser.storage.local.set({ options }, () => resolve())
     })
   })
 }
 
-function upgradeOptions() {
-  return new Promise(async resolve => {
-    let options = await utils.getOptions()
-
-    browser.storage.local.clear(() => {
-      browser.storage.local.set({ options }, () => {
-        resolve()
-      })
-    })
-  })
-}
-
-function processUpdate() {
+function processUpdate(_options) {
   return new Promise(async resolve => {
-    let frontends = []
     const config = await utils.getConfig()
-    let options = await utils.getOptions()
-    for (const service in config.services) {
-      if (!options[service]) options[service] = {}
+    let options = _options ?? await utils.getOptions()
 
-      if (!(options[service].frontend in config.services[service].frontends)) {
-        options[service] = config.services[service].options // Reset settings for service
-        delete options[options[service].frontend] // Remove deprecated frontend
-      }
+    const defaults = await getDefaults()
 
-      for (const defaultOption in config.services[service].options) {
-        if (!(defaultOption in options[service])) {
-          options[service][defaultOption] = config.services[service].options[defaultOption]
+    // Remove any unknown option or subOption
+    for (const optionName in options) {
+      if (!(optionName in defaults)) delete options[optionName]
+      else if (typeof optionName === 'object' && optionName !== null) {
+        for (const subOptionName in options[optionName]) {
+          if (!(subOptionName in defaults[optionName])) delete options[optionName][subOptionName]
         }
       }
+    }
+
+    // Remove any unknwon popupService
+    options.popupServices = options.popupServices.filter(service => service in config.services)
+
+    // Add missing options
+    for (const [defaultName, defaultValue] of Object.entries(defaults)) {
+      if (!(defaultName in options)) {
+        options[defaultName] = defaultValue
+      }
+    }
 
-      for (const frontend in config.services[service].frontends) {
-        frontends.push(frontend)
-        if (!(frontend in options) && config.services[service].frontends[frontend].instanceList) {
-          options[frontend] = defaultInstances[frontend] || []
-        }
+    for (const [serviceName, serviceValue] of Object.entries(config.services)) {
+      // Reset service options if selected frontend is deprecated
+      if (!(options[serviceName].frontend in serviceValue.frontends)) {
+        options[serviceName] = serviceValue.options
       }
 
-      for (const frontend of options.popupServices) {
-        if (!Object.keys(config.services).includes(frontend)) {
-          const i = options.popupServices.indexOf(frontend)
-          if (i > -1) options.popupServices.splice(i, 1)
+      // Add a default service option if it's not present
+      for (const optionName in serviceValue.options) {
+        if (!(optionName in options[serviceName])) {
+          options[serviceName][optionName] = serviceValue.options[optionName]
         }
       }
     }
-    const general = ["theme", "popupServices", "fetchInstances", "redirectOnlyInIncognito"]
-    const combined = [
-      ...Object.keys(config.services),
-      ...frontends,
-      ...general,
-      "exceptions",
-      "popupServices",
-      "version",
-    ]
-    for (const key in options) {
-      if (combined.indexOf(key) < 0) {
-        delete options[key] // Remove any unknown settings in options
-      }
-    }
-    browser.storage.local.set({ options }, () => {
-      resolve()
+
+    browser.storage.local.clear(() => {
+      browser.storage.local.set({ options }, () => {
+        resolve(options)
+      })
     })
   })
 }
@@ -973,7 +960,6 @@ export default {
   computeService,
   reverse,
   initDefaults,
-  upgradeOptions,
   processUpdate,
   copyRaw,
   switchInstance,