Skip to content

Commit

Permalink
Allowing to import from previous version #961
Browse files Browse the repository at this point in the history
  • Loading branch information
ManeraKai committed Aug 18, 2024
1 parent 7883432 commit a94ad51
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 98 deletions.
134 changes: 60 additions & 74 deletions src/assets/javascripts/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})
}
Expand Down Expand Up @@ -973,7 +960,6 @@ export default {
computeService,
reverse,
initDefaults,
upgradeOptions,
processUpdate,
copyRaw,
switchInstance,
Expand Down
8 changes: 1 addition & 7 deletions src/pages/background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,8 @@ browser.runtime.onInstalled.addListener(async details => {
if (!(await utils.getOptions())) {
await servicesHelper.initDefaults()
}
browser.runtime.openOptionsPage()
} else if (details.reason == "update") {
if (details.previousVersion == "2.5.2") {
await servicesHelper.upgradeOptions()
await servicesHelper.processUpdate()
} else {
await servicesHelper.processUpdate()
}
await servicesHelper.processUpdate()
}
}
})
Expand Down
28 changes: 13 additions & 15 deletions src/pages/options_src/General/SettingsButtons.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@
const reader = new FileReader()
reader.readAsText(importSettingsFiles[0])
reader.onload = async () => {
const data = JSON.parse(reader.result)
if ("theme" in data && data.version == browser.runtime.getManifest().version) {
browser.storage.local.clear(async () => {
options.set(data)
})
} else {
alert("Incompatible settings")
let data = JSON.parse(reader.result)
if (data.version != browser.runtime.getManifest().version) {
alert("Importing from a previous version. Be careful")
}
data = await servicesHelper.processUpdate(data)
options.set(data)
}
reader.onerror = error => {
console.log("error", error)
Expand All @@ -51,20 +49,20 @@
}
async function importSettingsSync() {
browser.storage.sync.get({ options }, r => {
const optionsSync = r.options
if (optionsSync.version == browser.runtime.getManifest().version) {
options.set(optionsSync)
} else {
alert("Error")
browser.storage.sync.get({ options }, async r => {
let data = r.options
if (data.version != browser.runtime.getManifest().version) {
alert("Importing from a previous version. Be careful")
}
data = await servicesHelper.processUpdate(data)
options.set(data)
})
}
async function resetSettings() {
browser.storage.local.clear(async () => {
await servicesHelper.initDefaults()
options.set(await utils.getOptions())
const data = await servicesHelper.initDefaults()
options.set(data)
})
}
</script>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/options_src/Services/Services.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
<ServiceIcon details={selection} />
{selection.label}
{:else}
{browser.i18n.getMessage("search_service") || "Search Service"}
{browser.i18n.getMessage("searchService") || "Search Service"}
{/if}
</div>
<div style="font-size: 10px;" slot="chevron-icon">🮦</div>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/popup_src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@

<style>
:global(html, body) {
width: 300px;
width: 280px;
height: min-content;
min-height: auto;
margin: 0;
Expand Down

0 comments on commit a94ad51

Please sign in to comment.