Added custom label to instances not from the public list https://github.com/libredirect/browser_extension/issues/907. Cleaned code

This commit is contained in:
ManeraKai 2024-05-20 22:55:33 +03:00
parent abcd566305
commit 73f85a005e
No known key found for this signature in database
GPG Key ID: 5ABC31FFD562E337

View File

@ -2,6 +2,8 @@ import utils from "../../assets/javascripts/utils.js"
let config, let config,
options, options,
blacklist,
redirects,
divs = {} divs = {}
for (const a of document.getElementById("links").getElementsByTagName("a")) { for (const a of document.getElementById("links").getElementsByTagName("a")) {
@ -148,20 +150,11 @@ async function loadPage(path) {
changeFrontendsSettings(service) changeFrontendsSettings(service)
for (const frontend in config.services[service].frontends) {
if (config.services[service].frontends[frontend].instanceList) {
processCustomInstances(frontend, document)
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"
})
}
}
!async function () { !async function () {
const blacklist = await utils.getBlacklist(options) blacklist = await utils.getBlacklist(options)
const redirects = await utils.getList(options) redirects = await utils.getList(options)
for (const frontend in config.services[service].frontends) { for (const frontend in config.services[service].frontends) {
if (config.services[service].frontends[frontend].instanceList) { if (config.services[service].frontends[frontend].instanceList) {
@ -177,10 +170,21 @@ async function loadPage(path) {
.innerHTML = 'Could not fetch instances.' .innerHTML = 'Could not fetch instances.'
} }
else { else {
createList(frontend, config.networks, document, redirects, blacklist) 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"
})
}
}
}() }()
} }
} }
@ -199,10 +203,12 @@ async function calcCustomInstances(frontend) {
const { color, text } = processTime(time) const { color, text } = processTime(time)
timeText = `<span class="ping" style="color:${color};">${text}</span>` timeText = `<span class="ping" style="color:${color};">${text}</span>`
} }
const custom = isCustomInstance(frontend, x) ? "" : `<span>custom</span>`
return `<div> return `<div>
<x> <x>
<a href="${x}" target="_blank">${x}</a> <a href="${x}" target="_blank">${x}</a>
${timeText} ${timeText}
${custom}
</x> </x>
<button class="add clear-${x}"> <button class="add clear-${x}">
<svg xmlns="https://www.w3.org/2000/svg" height="20px" viewBox="0 0 24 24" width="20px" fill="currentColor"> <svg xmlns="https://www.w3.org/2000/svg" height="20px" viewBox="0 0 24 24" width="20px" fill="currentColor">
@ -220,16 +226,16 @@ async function calcCustomInstances(frontend) {
options = await utils.getOptions() options = await utils.getOptions()
options[frontend] = customInstances options[frontend] = customInstances
browser.storage.local.set({ options }, async () => { browser.storage.local.set({ options }, async () => {
blacklist = await utils.getBlacklist(options)
redirects = await utils.getList(options)
calcCustomInstances(frontend) calcCustomInstances(frontend)
const blacklist = await utils.getBlacklist(options) createList(frontend)
const redirects = await utils.getList(options)
createList(frontend, config.networks, document, redirects, blacklist)
}) })
}) })
} }
} }
async function processCustomInstances(frontend, document) { async function processCustomInstances(frontend) {
calcCustomInstances(frontend) calcCustomInstances(frontend)
document.getElementById(frontend).getElementsByClassName("custom-instance-form")[0].addEventListener("submit", async event => { document.getElementById(frontend).getElementsByClassName("custom-instance-form")[0].addEventListener("submit", async event => {
event.preventDefault() event.preventDefault()
@ -259,15 +265,11 @@ async function processCustomInstances(frontend, document) {
/** /**
* @param {string} frontend * @param {string} frontend
* @param {*} networks
* @param {Document} document
* @param {*} redirects
* @param {*} blacklist
*/ */
async function createList(frontend, networks, document, redirects, blacklist) { async function createList(frontend) {
const pingCache = await utils.getPingCache() const pingCache = await utils.getPingCache()
const options = await utils.getOptions() const options = await utils.getOptions()
for (const network in networks) { for (const network in config.networks) {
const checklist = document.getElementById(frontend) const checklist = document.getElementById(frontend)
.getElementsByClassName(network)[0] .getElementsByClassName(network)[0]
.getElementsByClassName("checklist")[0] .getElementsByClassName("checklist")[0]
@ -332,7 +334,7 @@ async function createList(frontend, networks, document, redirects, blacklist) {
options[frontend].push(instance) options[frontend].push(instance)
browser.storage.local.set({ options }, () => { browser.storage.local.set({ options }, () => {
calcCustomInstances(frontend) calcCustomInstances(frontend)
createList(frontend, config.networks, document, redirects, blacklist) createList(frontend)
}) })
} }
}) })
@ -395,3 +397,12 @@ function processTime(time) {
} }
return { color, text } return { color, text }
} }
function isCustomInstance(frontend, instance) {
for (const network in config.networks) {
if (!redirects[frontend]) return false;
const instances = redirects[frontend][network]
if (instances.includes(instance)) return true
}
return false
}