Optimized code

このコミットが含まれているのは:
ManeraKai 2023-01-23 12:54:45 +03:00
コミット 7c2e8f08c7
この署名に対応する既知のキーがデータベースに存在しません
GPGキーID: 5ABC31FFD562E337
4個のファイルの変更87行の追加106行の削除

ファイルの表示

@ -49,7 +49,6 @@ function redirect(url, type, initiator, forceRedirection) {
let frontend
for (const service in config.services) {
if (!forceRedirection && !options[service].enabled) continue
if (config.services[service].embeddable && type != options[service].redirectType && options[service].redirectType != "both") continue
if (!config.services[service].embeddable && type != "main_frame") continue
@ -57,21 +56,20 @@ function redirect(url, type, initiator, forceRedirection) {
if (!regexArray(service, url, config, frontend)) continue
if (initiator && all(service, null, options, config).includes(initiator.origin)) return "BYPASSTAB"
if (
initiator
&&
options[frontend].includes(initiator.origin)
) return "BYPASSTAB"
let instanceList = []
for (const network in options[frontend]) {
instanceList.push(...options[frontend])
}
console.log(frontend, instanceList)
let instanceList = options[frontend]
if (instanceList.length === 0) return
randomInstance = utils.getRandomInstance(instanceList)
break
}
if (!frontend || !randomInstance) return
if (!randomInstance) return
// Here is a (temperory) space for defining constants required in 2 or more switch cases.
const mapCentreRegex = /@(-?\d[0-9.]*),(-?\d[0-9.]*),(\d{1,2})[.z]/
@ -399,7 +397,7 @@ function computeService(url, returnFrontend) {
return
} else {
for (const frontend in config.services[service].frontends) {
if (all(service, frontend, options, config).some(val => val.includes(utils.protocolHost(url)))) {
if (all(service, frontend, options, config).includes(utils.protocolHost(url))) {
if (returnFrontend)
resolve([service, frontend, utils.protocolHost(url)])
else
@ -418,27 +416,16 @@ function _switchInstance(url) {
await init()
const protocolHost = utils.protocolHost(url)
for (const service in config.services) {
if (!all(service, undefined, options, config).some(val => val.includes(protocolHost))) continue
let instancesList = []
let frontend
if (!options[service].frontend)
frontend = Object.keys(config.services[service].frontends)[0]
else
frontend = options[service].frontend
instancesList = [...options[frontend]]
let oldInstance
const i = instancesList.indexOf(protocolHost)
if (i > -1) {
instancesList.splice(i, 1)
}
let frontend = options[service].frontend ?? Object.keys(config.services[service].frontends)[0]
let instancesList = [...options[frontend]]
if (!instancesList.includes(protocolHost)) continue
instancesList.splice(instancesList.indexOf(protocolHost), 1)
if (instancesList.length === 0) {
resolve()
return
}
console.log(instancesList)
const randomInstance = utils.getRandomInstance(instancesList)
const newUrl = `${randomInstance}${url.pathname}${url.search}`
resolve(newUrl)
@ -452,11 +439,10 @@ function reverse(url, urlString) {
return new Promise(async resolve => {
await init()
url = new URL(url)
let protocolHost
protocolHost = utils.protocolHost(url)
let protocolHost = utils.protocolHost(url)
for (const service in config.services) {
if (!all(service, undefined, options, config).some(val => val.includes(protocolHost))) continue
let frontend = options[service].frontend ?? Object.keys(config.services[service].frontends)[0]
if (options[frontend].includes(protocolHost)) continue
switch (service) {
case "youtube":
@ -511,7 +497,7 @@ function initDefaults() {
options['theme'] = "detect"
options['popupServices'] = ["youtube", "twitter", "tiktok", "imgur", "reddit", "quora", "translate", "maps"]
options['invidious'] = ['https://inv.vern.cc/']
options['invidious'] = ['https://inv.vern.cc']
options['piped'] = ['https://piped.video']
options['pipedMaterial'] = ['https://piped-material.xn--17b.net']
options['cloudtube'] = ['https://tube.cadence.moe']
@ -525,7 +511,7 @@ function initDefaults() {
options['libremdb'] = ['https://libremdb.iket.me']
options['simplytranslate'] = ['https://simplytranslate.org']
options['linvgatranslate'] = ['https://lingva.ml']
options['searxng'] = ['https://sx.vern.cc/']
options['searxng'] = ['https://sx.vern.cc']
options['rimgo'] = ['https://rimgo.vern.cc']
options['librarian'] = ['https://lbry.vern.cc']
options['beatbump'] = ['https://beatbump.ml']

ファイルの表示

@ -1,7 +1,7 @@
{
"name": "__MSG_extensionName__",
"description": "__MSG_extensionDescription__",
"version": "3.0.0",
"version": "2.5.0",
"manifest_version": 2,
"browser_specific_settings": {
"gecko": {

ファイルの表示

@ -15,7 +15,9 @@ browser.runtime.onInstalled.addListener(async details => {
}
}
else if (details.reason == "update") {
await servicesHelper.upgradeOptions()
if (details.previousVersion == '2.3.4') {
await servicesHelper.upgradeOptions()
}
// await servicesHelper.processUpdate()
}
browser.runtime.openOptionsPage()

ファイルの表示

@ -4,20 +4,18 @@ window.browser = window.browser || window.chrome
import servicesHelper from "../../assets/javascripts/services.js"
import utils from "../../assets/javascripts/utils.js"
let config,
divs = {}
config = await utils.getConfig()
servicesHelper.switchInstance().then(r => {
if (!r) document.getElementById("change_instance_div").style.display = "none"
else document.getElementById("change_instance").addEventListener("click", async () => {
browser.tabs.update({ url: await servicesHelper.switchInstance() })
})
if (!r)
document.getElementById("change_instance_div").style.display = "none"
else
document.getElementById("change_instance").addEventListener("click", async () => {
browser.tabs.update({ url: await servicesHelper.switchInstance() })
})
})
servicesHelper.copyRaw(true).then(r => {
if (!r) document.getElementById("copy_raw_div").style.display = "none"
if (!r)
document.getElementById("copy_raw_div").style.display = "none"
else {
const copy_raw = document.getElementById("copy_raw")
copy_raw.addEventListener("click", () => servicesHelper.copyRaw(false, copy_raw))
@ -28,78 +26,73 @@ document.getElementById("more-options").addEventListener("click", () => browser.
const allSites = document.getElementsByClassName("all_sites")[0]
const currSite = document.getElementsByClassName("current_site")[0]
function setDivs() {
return new Promise(resolve => {
for (const service in config.services) {
divs[service] = {}
divs[service].toggle = {}
divs[service].current = currSite.getElementsByClassName(service)[0]
divs[service].all = allSites.getElementsByClassName(service)[0]
divs[service].toggle.current = currSite.getElementsByClassName(service + "-enabled")[0]
divs[service].toggle.all = allSites.getElementsByClassName(service + "-enabled")[0]
}
resolve()
})
}
const config = await utils.getConfig()
await setDivs()
let divs = {}
for (const service in config.services) {
divs[service] = {}
divs[service].toggle = {}
divs[service].current = currSite.getElementsByClassName(service)[0]
divs[service].all = allSites.getElementsByClassName(service)[0]
divs[service].toggle.current = currSite.getElementsByClassName(service + "-enabled")[0]
divs[service].toggle.all = allSites.getElementsByClassName(service + "-enabled")[0]
}
const currentSiteIsFrontend = document.getElementById("current_site_divider")
browser.storage.local.get(["options"], r => {
browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
for (const service in config.services) {
if (!r.options.popupServices.includes(service)) allSites.getElementsByClassName(service)[0].classList.add("hide")
else allSites.getElementsByClassName(service)[0].classList.remove("hide")
currSite.getElementsByClassName(service)[0].classList.add("hide")
}
browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
let options = await utils.getOptions()
for (const service in config.services) {
if (!options.popupServices.includes(service))
allSites.getElementsByClassName(service)[0].classList.add("hide")
else
allSites.getElementsByClassName(service)[0].classList.remove("hide")
currSite.getElementsByClassName(service)[0].classList.add("hide")
}
for (const service in config.services) {
divs[service].toggle.all.checked = r.options[service].enabled
divs[service].toggle.current.checked = r.options[service].enabled
}
for (const service in config.services) {
divs[service].toggle.all.checked = options[service].enabled
divs[service].toggle.current.checked = options[service].enabled
}
let url
try {
url = new URL(tabs[0].url)
} catch {
currentSiteIsFrontend.classList.add("hide")
return
}
let url
try {
url = new URL(tabs[0].url)
} catch {
currentSiteIsFrontend.classList.add("hide")
return
}
let service = await servicesHelper.computeService(url, true)
let frontend
let instance
if (service) {
if (typeof service != "string") {
instance = service[2]
frontend = service[1]
service = service[0]
let isCustom = false
for (const network in config.networks) if (r.options[frontend].indexOf(instance) > -1) isCustom = true
}
divs[service].current.classList.remove("hide")
divs[service].all.classList.add("hide")
} else {
currentSiteIsFrontend.classList.add("hide")
let service = await servicesHelper.computeService(url, true)
let frontend
let instance
if (service) {
if (typeof service != "string") {
instance = service[2]
frontend = service[1]
service = service[0]
let isCustom = false
for (const network in config.networks)
if (options[frontend].indexOf(instance) > -1)
isCustom = true
}
})
divs[service].current.classList.remove("hide")
divs[service].all.classList.add("hide")
} else {
currentSiteIsFrontend.classList.add("hide")
}
})
for (const service in config.services) {
divs[service].toggle.all.addEventListener("change", () => {
browser.storage.local.get("options", r => {
let options = r.options
options[service].enabled = divs[service].toggle.all.checked
browser.storage.local.set({ options })
})
divs[service].toggle.all.addEventListener("change", async () => {
let options = await utils.getOptions()
options[service].enabled = divs[service].toggle.all.checked
browser.storage.local.set({ options })
})
divs[service].toggle.current.addEventListener("change", () => {
browser.storage.local.get("options", r => {
let options = r.options
options[service].enabled = divs[service].toggle.current.checked
browser.storage.local.set({ options })
})
divs[service].toggle.current.addEventListener("change", async () => {
let options = await utils.getOptions()
options[service].enabled = divs[service].toggle.current.checked
browser.storage.local.set({ options })
})
}
@ -110,4 +103,4 @@ for (const a of document.getElementsByTagName("a")) {
e.preventDefault()
}
})
}
}