Allowing to import from previous version https://github.com/libredirect/browser_extension/issues/961
This commit is contained in:
parent
7883432c19
commit
a94ad51a34
@ -831,103 +831,90 @@ const defaultInstances = {
|
|||||||
ytify: ["https://ytify.netlify.app"],
|
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() {
|
function initDefaults() {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
browser.storage.local.clear(async () => {
|
browser.storage.local.clear(async () => {
|
||||||
let config = await utils.getConfig()
|
options = await getDefaults()
|
||||||
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 }
|
|
||||||
|
|
||||||
browser.storage.local.set({ options }, () => resolve())
|
browser.storage.local.set({ options }, () => resolve())
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function upgradeOptions() {
|
function processUpdate(_options) {
|
||||||
return new Promise(async resolve => {
|
return new Promise(async resolve => {
|
||||||
let options = await utils.getOptions()
|
const config = await utils.getConfig()
|
||||||
|
let options = _options ?? await utils.getOptions()
|
||||||
|
|
||||||
|
const defaults = await getDefaults()
|
||||||
|
|
||||||
|
// 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 [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
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
browser.storage.local.clear(() => {
|
browser.storage.local.clear(() => {
|
||||||
browser.storage.local.set({ options }, () => {
|
browser.storage.local.set({ options }, () => {
|
||||||
resolve()
|
resolve(options)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function processUpdate() {
|
|
||||||
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] = {}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const defaultOption in config.services[service].options) {
|
|
||||||
if (!(defaultOption in options[service])) {
|
|
||||||
options[service][defaultOption] = config.services[service].options[defaultOption]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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 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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {URL} url
|
* @param {URL} url
|
||||||
*/
|
*/
|
||||||
@ -973,7 +960,6 @@ export default {
|
|||||||
computeService,
|
computeService,
|
||||||
reverse,
|
reverse,
|
||||||
initDefaults,
|
initDefaults,
|
||||||
upgradeOptions,
|
|
||||||
processUpdate,
|
processUpdate,
|
||||||
copyRaw,
|
copyRaw,
|
||||||
switchInstance,
|
switchInstance,
|
||||||
|
@ -13,14 +13,8 @@ browser.runtime.onInstalled.addListener(async details => {
|
|||||||
if (!(await utils.getOptions())) {
|
if (!(await utils.getOptions())) {
|
||||||
await servicesHelper.initDefaults()
|
await servicesHelper.initDefaults()
|
||||||
}
|
}
|
||||||
browser.runtime.openOptionsPage()
|
|
||||||
} else if (details.reason == "update") {
|
} else if (details.reason == "update") {
|
||||||
if (details.previousVersion == "2.5.2") {
|
await servicesHelper.processUpdate()
|
||||||
await servicesHelper.upgradeOptions()
|
|
||||||
await servicesHelper.processUpdate()
|
|
||||||
} else {
|
|
||||||
await servicesHelper.processUpdate()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -20,14 +20,12 @@
|
|||||||
const reader = new FileReader()
|
const reader = new FileReader()
|
||||||
reader.readAsText(importSettingsFiles[0])
|
reader.readAsText(importSettingsFiles[0])
|
||||||
reader.onload = async () => {
|
reader.onload = async () => {
|
||||||
const data = JSON.parse(reader.result)
|
let data = JSON.parse(reader.result)
|
||||||
if ("theme" in data && data.version == browser.runtime.getManifest().version) {
|
if (data.version != browser.runtime.getManifest().version) {
|
||||||
browser.storage.local.clear(async () => {
|
alert("Importing from a previous version. Be careful")
|
||||||
options.set(data)
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
alert("Incompatible settings")
|
|
||||||
}
|
}
|
||||||
|
data = await servicesHelper.processUpdate(data)
|
||||||
|
options.set(data)
|
||||||
}
|
}
|
||||||
reader.onerror = error => {
|
reader.onerror = error => {
|
||||||
console.log("error", error)
|
console.log("error", error)
|
||||||
@ -51,20 +49,20 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function importSettingsSync() {
|
async function importSettingsSync() {
|
||||||
browser.storage.sync.get({ options }, r => {
|
browser.storage.sync.get({ options }, async r => {
|
||||||
const optionsSync = r.options
|
let data = r.options
|
||||||
if (optionsSync.version == browser.runtime.getManifest().version) {
|
if (data.version != browser.runtime.getManifest().version) {
|
||||||
options.set(optionsSync)
|
alert("Importing from a previous version. Be careful")
|
||||||
} else {
|
|
||||||
alert("Error")
|
|
||||||
}
|
}
|
||||||
|
data = await servicesHelper.processUpdate(data)
|
||||||
|
options.set(data)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async function resetSettings() {
|
async function resetSettings() {
|
||||||
browser.storage.local.clear(async () => {
|
browser.storage.local.clear(async () => {
|
||||||
await servicesHelper.initDefaults()
|
const data = await servicesHelper.initDefaults()
|
||||||
options.set(await utils.getOptions())
|
options.set(data)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
@ -72,7 +72,7 @@
|
|||||||
<ServiceIcon details={selection} />
|
<ServiceIcon details={selection} />
|
||||||
{selection.label}
|
{selection.label}
|
||||||
{:else}
|
{:else}
|
||||||
{browser.i18n.getMessage("search_service") || "Search Service"}
|
{browser.i18n.getMessage("searchService") || "Search Service"}
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
<div style="font-size: 10px;" slot="chevron-icon">🮦</div>
|
<div style="font-size: 10px;" slot="chevron-icon">🮦</div>
|
||||||
|
@ -88,7 +88,7 @@
|
|||||||
|
|
||||||
<style>
|
<style>
|
||||||
:global(html, body) {
|
:global(html, body) {
|
||||||
width: 300px;
|
width: 280px;
|
||||||
height: min-content;
|
height: min-content;
|
||||||
min-height: auto;
|
min-height: auto;
|
||||||
margin: 0;
|
margin: 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user