aboutsummaryrefslogtreecommitdiffstats
path: root/src/pages/options
diff options
context:
space:
mode:
authorManeraKai <manerakai@protonmail.com>2024-05-21 02:05:37 +0300
committerManeraKai <manerakai@protonmail.com>2024-05-21 02:05:37 +0300
commit462011d2c30b1e0b6d60295c85216545c41a4a22 (patch)
tree8332736d16662d5715a8620b3affc66d05fa9af3 /src/pages/options
parentAdded LightTube https://github.com/libredirect/browser_extension/issues/913 (diff)
downloadlibredirect-462011d2c30b1e0b6d60295c85216545c41a4a22.zip
Cleaning code
Diffstat (limited to 'src/pages/options')
-rw-r--r--src/pages/options/index.js86
-rw-r--r--src/pages/options/init.js53
-rw-r--r--src/pages/options/widgets/general.js16
3 files changed, 69 insertions, 86 deletions
diff --git a/src/pages/options/index.js b/src/pages/options/index.js
index 9274023f..0066df0d 100644
--- a/src/pages/options/index.js
+++ b/src/pages/options/index.js
@@ -129,9 +129,7 @@ async function loadPage(path) {
if (path != 'general') {
const service = path;
-
divs[service] = {}
-
for (const option in config.services[service].options) {
divs[service][option] = document.getElementById(`${service}-${option}`)
if (typeof config.services[service].options[option] == "boolean") divs[service][option].checked = options[service][option]
@@ -146,46 +144,39 @@ async function loadPage(path) {
changeFrontendsSettings(service)
})
}
-
changeFrontendsSettings(service)
-
-
-
- !async function () {
- blacklist = await utils.getBlacklist(options)
- redirects = await utils.getList(options)
-
- for (const frontend in config.services[service].frontends) {
- if (config.services[service].frontends[frontend].instanceList) {
- if (redirects == 'disabled' || blacklist == 'disabled') {
- document.getElementById(frontend).getElementsByClassName('clearnet')[0].style.display = 'none'
- document.getElementById(frontend).getElementsByClassName('ping')[0].style.display = 'none'
- }
- else if (!redirects || !blacklist) {
- document.getElementById(frontend)
- .getElementsByClassName('clearnet')[0]
- .getElementsByClassName("checklist")[0]
- .getElementsByClassName('loading')[0]
- .innerHTML = 'Could not fetch instances.'
- }
- else {
- createList(frontend)
- }
+ blacklist = await utils.getBlacklist(options)
+ redirects = await utils.getList(options)
+ for (const frontend in config.services[service].frontends) {
+ if (config.services[service].frontends[frontend].instanceList) {
+ if (redirects == 'disabled' || blacklist == 'disabled') {
+ document.getElementById(frontend).getElementsByClassName('clearnet')[0].style.display = 'none'
+ document.getElementById(frontend).getElementsByClassName('ping')[0].style.display = 'none'
+ }
+ else if (!redirects || !blacklist) {
+ document.getElementById(frontend)
+ .getElementsByClassName('clearnet')[0]
+ .getElementsByClassName("checklist")[0]
+ .getElementsByClassName('loading')[0]
+ .innerHTML = 'Could not fetch instances.'
+ }
+ else {
+ createList(frontend)
}
}
+ }
- for (const frontend in config.services[service].frontends) {
- if (config.services[service].frontends[frontend].instanceList) {
- processCustomInstances(frontend)
- document.getElementById(`ping-${frontend}`).addEventListener("click", async () => {
- document.getElementById(`ping-${frontend}`).getElementsByTagName('x')[0].innerHTML = "Pinging..."
- await ping(frontend)
- document.getElementById(`ping-${frontend}`).getElementsByTagName('x')[0].innerHTML = "Ping instances"
- })
- }
+ for (const frontend in config.services[service].frontends) {
+ if (config.services[service].frontends[frontend].instanceList) {
+ processCustomInstances(frontend)
+ document.getElementById(`ping-${frontend}`).addEventListener("click", async () => {
+ document.getElementById(`ping-${frontend}`).getElementsByTagName('x')[0].innerHTML = "Pinging..."
+ await ping(frontend)
+ document.getElementById(`ping-${frontend}`).getElementsByTagName('x')[0].innerHTML = "Ping instances"
+ })
}
- }()
+ }
}
}
@@ -197,11 +188,10 @@ async function calcCustomInstances(frontend) {
document.getElementById(frontend).getElementsByClassName("custom-checklist")[0].innerHTML = customInstances
.map(
x => {
- let time = pingCache[x]
- let timeText = ""
+ const time = pingCache[x]
if (time) {
const { color, text } = processTime(time)
- timeText = `<span class="ping" style="color:${color};">${text}</span>`
+ var timeText = `<span class="ping" style="color:${color};">${text}</span>`
}
const custom = isCustomInstance(frontend, x) ? "" : `<span>custom</span>`
return `<div>
@@ -226,8 +216,6 @@ async function calcCustomInstances(frontend) {
options = await utils.getOptions()
options[frontend] = customInstances
browser.storage.local.set({ options }, async () => {
- blacklist = await utils.getBlacklist(options)
- redirects = await utils.getList(options)
calcCustomInstances(frontend)
createList(frontend)
})
@@ -242,9 +230,8 @@ async function processCustomInstances(frontend) {
let options = await utils.getOptions()
let customInstances = options[frontend]
let frontendCustomInstanceInput = document.getElementById(frontend).getElementsByClassName("custom-instance")[0]
- let url
try {
- url = new URL(frontendCustomInstanceInput.value)
+ var url = new URL(frontendCustomInstanceInput.value)
} catch (error) {
return
}
@@ -270,9 +257,7 @@ async function createList(frontend) {
const pingCache = await utils.getPingCache()
const options = await utils.getOptions()
for (const network in config.networks) {
- const checklist = document.getElementById(frontend)
- .getElementsByClassName(network)[0]
- .getElementsByClassName("checklist")[0]
+ const checklist = document.getElementById(frontend).getElementsByClassName(network)[0].getElementsByClassName("checklist")[0]
if (!redirects[frontend]) {
checklist.innerHTML = '<div class="block block-option">No instances found.</div>'
@@ -282,13 +267,10 @@ async function createList(frontend) {
const instances = redirects[frontend][network]
if (!instances || instances.length === 0) continue
- document.getElementById(frontend)
- .getElementsByClassName("custom-instance")[0]
- .placeholder = redirects[frontend].clearnet[0]
-
- const sortedInstances = instances.sort((a, b) => blacklist.cloudflare.includes(a) && !blacklist.cloudflare.includes(b))
+ document.getElementById(frontend).getElementsByClassName("custom-instance")[0].placeholder = redirects[frontend].clearnet[0]
- const content = sortedInstances
+ instances.sort((a, b) => blacklist.cloudflare.includes(a) && !blacklist.cloudflare.includes(b))
+ const content = instances
.map(x => {
const cloudflare = blacklist.cloudflare.includes(x) ?
`<a target="_blank" href="https://libredirect.github.io/docs.html#instances">
diff --git a/src/pages/options/init.js b/src/pages/options/init.js
index f88c9ef9..baf3eebe 100644
--- a/src/pages/options/init.js
+++ b/src/pages/options/init.js
@@ -8,40 +8,37 @@ if (!(await utils.getOptions())) {
await servicesHelper.initDefaults()
}
-function changeTheme() {
- return new Promise(async resolve => {
- switch ((await utils.getOptions()).theme) {
- case "dark":
- document.body.classList.add("dark-theme")
- document.body.classList.remove("light-theme")
- for (const element of document.body.getElementsByClassName('dark')) {
- element.style.display = 'none';
- }
- break
- case "light":
+async function changeTheme() {
+ switch ((await utils.getOptions()).theme) {
+ case "dark":
+ document.body.classList.add("dark-theme")
+ document.body.classList.remove("light-theme")
+ for (const element of document.body.getElementsByClassName('dark')) {
+ element.style.display = 'none';
+ }
+ break
+ case "light":
+ document.body.classList.add("light-theme")
+ document.body.classList.remove("dark-theme")
+ for (const element of document.body.getElementsByClassName('light')) {
+ element.style.display = 'none';
+ }
+ break
+ default:
+ if (matchMedia("(prefers-color-scheme: light)").matches) {
document.body.classList.add("light-theme")
document.body.classList.remove("dark-theme")
for (const element of document.body.getElementsByClassName('light')) {
element.style.display = 'none';
}
- break
- default:
- if (matchMedia("(prefers-color-scheme: light)").matches) {
- document.body.classList.add("light-theme")
- document.body.classList.remove("dark-theme")
- for (const element of document.body.getElementsByClassName('light')) {
- element.style.display = 'none';
- }
- } else {
- document.body.classList.add("dark-theme")
- document.body.classList.remove("light-theme")
- for (const element of document.body.getElementsByClassName('dark')) {
- element.style.display = 'none';
- }
+ } else {
+ document.body.classList.add("dark-theme")
+ document.body.classList.remove("light-theme")
+ for (const element of document.body.getElementsByClassName('dark')) {
+ element.style.display = 'none';
}
- }
- resolve()
- })
+ }
+ }
}
changeTheme()
diff --git a/src/pages/options/widgets/general.js b/src/pages/options/widgets/general.js
index 64172287..30a8a0c8 100644
--- a/src/pages/options/widgets/general.js
+++ b/src/pages/options/widgets/general.js
@@ -8,12 +8,16 @@ const isChrome = browser.runtime.getBrowserInfo === undefined
async function setOption(option, type, event) {
let options = await utils.getOptions()
- if (type == "select") {
- options[option] = event.target.options[event.target.options.selectedIndex].value
- } else if (type == "checkbox") {
- options[option] = event.target.checked
- } else if (type == "range") {
- options[option] = event.target.value
+ switch (type) {
+ case "select":
+ options[option] = event.target.options[event.target.options.selectedIndex].value
+ break;
+ case "checkbox":
+ options[option] = event.target.checked
+ break;
+ case "range":
+ options[option] = event.target.value
+ break;
}
browser.storage.local.set({ options })
}