Embrace object usage for user-set options
This commit is contained in:
parent
25de09a935
commit
e04c79d4ae
|
@ -5,7 +5,6 @@ import utils from "./utils.js"
|
||||||
let config = {},
|
let config = {},
|
||||||
redirects = {},
|
redirects = {},
|
||||||
options = {}
|
options = {}
|
||||||
let disabled, curNetwork, networkFallback, redirectType
|
|
||||||
|
|
||||||
async function getConfig() {
|
async function getConfig() {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
|
@ -25,25 +24,29 @@ function camelCase(str) {
|
||||||
function init() {
|
function init() {
|
||||||
return new Promise(async resolve => {
|
return new Promise(async resolve => {
|
||||||
browser.storage.local.get(["network", "networkFallback"], r => {
|
browser.storage.local.get(["network", "networkFallback"], r => {
|
||||||
curNetwork = r.network
|
options.network = r.network
|
||||||
networkFallback = r.networkFallback
|
options.networkFallback = r.networkFallback
|
||||||
|
options.redirects = r.redirects
|
||||||
})
|
})
|
||||||
//cur = current
|
//cur = current
|
||||||
for (const service in config.services) {
|
for (const service in config.services) {
|
||||||
options[service] = {}
|
options[service] = {}
|
||||||
browser.storage.local.get([`disable${camelCase(service)}`, `${service}Redirects`, `${service}RedirectType,`, `${service}Frontend`], r => {
|
browser.storage.local.get([`disable${camelCase(service)}`, `${service}RedirectType`, `${service}Frontend`], r => {
|
||||||
options[service].disabled = r["disable" + camelCase(service)]
|
options[service].disabled = r["disable" + camelCase(service)]
|
||||||
options[service].redirects = r[service + "Redirects"]
|
|
||||||
options[service].frontend = r[service + "Frontend"]
|
options[service].frontend = r[service + "Frontend"]
|
||||||
|
options[service].redirectType = r[service + "RedirectType"]
|
||||||
// console.log(r)
|
// console.log(r)
|
||||||
})
|
})
|
||||||
for (const frontend in config.services[service].frontends) {
|
for (const frontend in config.services[service].frontends) {
|
||||||
redirects[frontend] = {}
|
options[frontend] = {}
|
||||||
|
options[frontend].checks = []
|
||||||
|
options[frontend].custom = []
|
||||||
for (const network in config.networks) {
|
for (const network in config.networks) {
|
||||||
browser.storage.local.get([`${frontend}${camelCase(network)}RedirectsChecks`, `${frontend}${camelCase(network)}CustomRedirects`], r => {
|
browser.storage.local.get([`${frontend}${camelCase(network)}RedirectsChecks`, `${frontend}${camelCase(network)}CustomRedirects`], r => {
|
||||||
console.log(r)
|
// console.log(r)
|
||||||
// console.log(`${frontend}${camelCase(network)}RedirectsChecks`)
|
// console.log(`${frontend}${camelCase(network)}RedirectsChecks`)
|
||||||
redirects[frontend][network] = [...r[frontend + camelCase(network) + "RedirectsChecks"], ...r[frontend + camelCase(network) + "CustomRedirects"]]
|
options[frontend].checks = r[frontend + camelCase(network) + "RedirectsChecks"]
|
||||||
|
options[frontend].custom = r[frontend + camelCase(network) + "CustomRedirects"]
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -57,17 +60,21 @@ function all(service) {
|
||||||
for (frontend in config.services[service].frontends) {
|
for (frontend in config.services[service].frontends) {
|
||||||
for (network in config.networks) tmp.push([...redirects[frontend][network]])
|
for (network in config.networks) tmp.push([...redirects[frontend][network]])
|
||||||
}
|
}
|
||||||
|
return tmp
|
||||||
}
|
}
|
||||||
|
|
||||||
await getConfig()
|
getConfig()
|
||||||
await init()
|
init()
|
||||||
browser.storage.onChanged.addListener(init)
|
browser.storage.onChanged.addListener(init)
|
||||||
|
|
||||||
function redirect(url, type, initiator) {
|
function redirect(url, type, initiator) {
|
||||||
let randomInstance, frontend
|
let randomInstance
|
||||||
|
let frontend = options[service].frontend
|
||||||
|
let network = options.network
|
||||||
|
let networkFallback = options.networkFallback
|
||||||
if (url.pathname == "/") return
|
if (url.pathname == "/") return
|
||||||
for (const service in config.services) {
|
for (const service in config.services) {
|
||||||
if (disabled && !disableOverride) continue
|
if (options[service].disabled && !disableOverride) continue
|
||||||
let targets = service.targets
|
let targets = service.targets
|
||||||
if (targets == "datajson") {
|
if (targets == "datajson") {
|
||||||
browser.storage.local.get(`${service}Targets`, (targets = r[service + "Targets"]))
|
browser.storage.local.get(`${service}Targets`, (targets = r[service + "Targets"]))
|
||||||
|
@ -75,10 +82,10 @@ function redirect(url, type, initiator) {
|
||||||
|
|
||||||
if (initiator && (all(service).includes(initiator.origin) || targets.includes(initiator.host))) continue
|
if (initiator && (all(service).includes(initiator.origin) || targets.includes(initiator.host))) continue
|
||||||
if (!targets.some(rx => rx.test(url.href))) continue
|
if (!targets.some(rx => rx.test(url.href))) continue
|
||||||
if (type != redirectType && type != "both") continue
|
if (type != options[service].redirectType && type != "both") continue
|
||||||
browser.storage.local.get(`${service}Frontend`, (frontend = r[service + "Frontend"]))
|
// browser.storage.local.get(`${service}Frontend`, (frontend = r[service + "Frontend"]))
|
||||||
let instanceList = redirects[frontend][curNetwork]
|
let instanceList = [...[service + camelCase(network) + "RedirectsChecks"], ...[service + camelCase(network) + "CustomRedirects"]]
|
||||||
if (instanceList.length === 0 && networkFallback) instanceList = redirects[frontend].clearnet
|
if (instanceList.length === 0 && networkFallback) instanceList = [...[service + "ClearnetRedirectsChecks"], ...[service + "ClearnetCustomRedirects"]]
|
||||||
if (instanceList.length === 0 && redirects.indexOf(frontend) != -1) return
|
if (instanceList.length === 0 && redirects.indexOf(frontend) != -1) return
|
||||||
randomInstance = utils.getRandomInstance(instanceList)
|
randomInstance = utils.getRandomInstance(instanceList)
|
||||||
}
|
}
|
||||||
|
@ -378,27 +385,41 @@ function initDefaults() {
|
||||||
.then(async data => {
|
.then(async data => {
|
||||||
let dataJson = JSON.parse(data)
|
let dataJson = JSON.parse(data)
|
||||||
redirects = dataJson
|
redirects = dataJson
|
||||||
console.log(redirects)
|
// console.log(redirects)
|
||||||
console.log(config)
|
// console.log(config)
|
||||||
|
browser.storage.local.set({
|
||||||
|
redirects,
|
||||||
|
})
|
||||||
browser.storage.local.get(["cloudflareBlackList", "authenticateBlackList", "offlineBlackList"], async r => {
|
browser.storage.local.get(["cloudflareBlackList", "authenticateBlackList", "offlineBlackList"], async r => {
|
||||||
|
// console.log(r)
|
||||||
for (const service in config.services) {
|
for (const service in config.services) {
|
||||||
for (const defaultOption in service.defaults) {
|
if (config.services[service].targets == "datajson") {
|
||||||
browser.storage.local.set({ [defaultOption]: service.defaults[defaultOption] })
|
browser.storage.local.set({ [service + "Targets"]: redirects[service] })
|
||||||
|
}
|
||||||
|
for (const defaultOption in config.services[service].defaults) {
|
||||||
|
browser.storage.local.set({ [defaultOption]: config.services[service].defaults[defaultOption] })
|
||||||
}
|
}
|
||||||
for (const frontend in config.services[service].frontends) {
|
for (const frontend in config.services[service].frontends) {
|
||||||
if ((config.services[service].targets = "datajson")) {
|
let clearnetChecks = redirects[frontend][clearnet]
|
||||||
browser.storage.local.set({ [service + "Targets"]: redirects[service] })
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
for (const instance of [...r.cloudflareBlackList, ...r.authenticateBlackList, ...r.offlineBlackList]) {
|
for (const instance of [...r.cloudflareBlackList, ...r.authenticateBlackList, ...r.offlineBlackList]) {
|
||||||
let i = redirects[frontend]["clearnet"].indexOf(instance)
|
let i = clearnetChecks.indexOf(instance)
|
||||||
if (i > -1) redirects[frontend]["clearnet"].splice(i, 1)
|
if (i > -1) clearnetChecks.splice(i, 1)
|
||||||
}
|
}
|
||||||
for (const network in config.networks) {
|
for (const network in config.networks) {
|
||||||
browser.storage.local.set({
|
console.log(redirects[frontend][network])
|
||||||
[frontend + camelCase(network) + "RedirectsChecks"]: [...redirects[frontend][network]],
|
switch (network) {
|
||||||
[frontend + camelCase(network) + "CustomRedirects"]: [],
|
case "clearnet":
|
||||||
})
|
browser.storage.local.set({
|
||||||
|
[frontend + "ClearnetRedirectsChecks"]: [clearnetChecks],
|
||||||
|
[frontend + "ClearnetCustomRedirects"]: [],
|
||||||
|
})
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
browser.storage.local.set({
|
||||||
|
[frontend + camelCase(network) + "RedirectsChecks"]: [...redirects[frontend][network]],
|
||||||
|
[frontend + camelCase(network) + "CustomRedirects"]: [],
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -293,7 +293,7 @@
|
||||||
"targets": "datajson",
|
"targets": "datajson",
|
||||||
"name": "PeerTube",
|
"name": "PeerTube",
|
||||||
"defaults": {
|
"defaults": {
|
||||||
"disablePeertubeTargets": true
|
"disablePeertube": true
|
||||||
},
|
},
|
||||||
"imageType": "svg"
|
"imageType": "svg"
|
||||||
},
|
},
|
||||||
|
@ -310,7 +310,7 @@
|
||||||
"targets": ["^https?:\\/{2}odysee\\.com", "^https?:\\/{2}lbry\\.tv"],
|
"targets": ["^https?:\\/{2}odysee\\.com", "^https?:\\/{2}lbry\\.tv"],
|
||||||
"name": "LBRY",
|
"name": "LBRY",
|
||||||
"defaults": {
|
"defaults": {
|
||||||
"disableLbryTargets": true,
|
"disableLbry": true,
|
||||||
"lbryFrontend": "librarian",
|
"lbryFrontend": "librarian",
|
||||||
"lbryRedirectType": "both"
|
"lbryRedirectType": "both"
|
||||||
},
|
},
|
||||||
|
@ -429,7 +429,7 @@
|
||||||
"targets": ["^https?:\\/{2}send\\.libredirect\\.invalid\\/$", "^https?:\\/{2}send\\.firefox\\.com\\/$", "^https?:\\/{2}sendfiles\\.online\\/$"],
|
"targets": ["^https?:\\/{2}send\\.libredirect\\.invalid\\/$", "^https?:\\/{2}send\\.firefox\\.com\\/$", "^https?:\\/{2}sendfiles\\.online\\/$"],
|
||||||
"name": "Send Files",
|
"name": "Send Files",
|
||||||
"defaults": {
|
"defaults": {
|
||||||
"disableSendTarget": false
|
"disableSendTargets": false
|
||||||
},
|
},
|
||||||
"imageType": "svg"
|
"imageType": "svg"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue