From eb7d1ce4c7bd288fb5db37b7c5c1cabed7e8d326 Mon Sep 17 00:00:00 2001 From: ManeraKai Date: Sat, 31 Dec 2022 12:37:23 +0300 Subject: Removed Wikiless, Neuters. Optimization --- src/pages/options/index.js | 199 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 199 insertions(+) (limited to 'src/pages/options/index.js') diff --git a/src/pages/options/index.js b/src/pages/options/index.js index 409fa5b5..a735110a 100644 --- a/src/pages/options/index.js +++ b/src/pages/options/index.js @@ -1,3 +1,10 @@ +import utils from "../../assets/javascripts/utils.js" +import localise from "../../assets/javascripts/localise.js" + +let config, + options, + divs = {} + for (const a of document.getElementById("links").getElementsByTagName("a")) { a.addEventListener("click", e => { const path = a.getAttribute("href").replace("#", "") @@ -6,6 +13,35 @@ for (const a of document.getElementById("links").getElementsByTagName("a")) { }) } +await new Promise(resolve => { + fetch("/config.json").then(response => response.text()) + .then(data => { + config = JSON.parse(data) + resolve() + }) +}) +await new Promise(resolve => { + browser.storage.local.get("options", r => { + options = r.options + resolve() + }) +}) + +function changeFrontendsSettings(service) { + for (const frontend in config.services[service].frontends) { + if (config.services[service].frontends[frontend].instanceList) { + const frontendDiv = document.getElementById(frontend) + if (typeof divs[service].frontend !== "undefined") { + if (frontend == divs[service].frontend.value) { + frontendDiv.style.display = "block" + } else { + frontendDiv.style.display = "none" + } + } + } + } +} + function loadPage(path) { for (const section of document.getElementById("pages").getElementsByTagName("section")) section.style.display = "none" document.getElementById(`${path}_page`).style.display = "block" @@ -16,8 +52,171 @@ function loadPage(path) { let stateObj = { id: "100" } window.history.pushState(stateObj, "Page 2", `/pages/options/index.html#${path}`) + + 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] + else divs[service][option].value = options[service][option] + + divs[service][option].addEventListener("change", () => { + browser.storage.local.get("options", r => { + let options = r.options + if (typeof config.services[service].options[option] == "boolean") options[service][option] = divs[service][option].checked + else options[service][option] = divs[service][option].value + browser.storage.local.set({ options }) + changeFrontendsSettings(service) + }) + }) + } + + if (Object.keys(config.services[service].frontends).length > 1) { + changeFrontendsSettings(service) + } + + for (const frontend in config.services[service].frontends) { + if (config.services[service].frontends[frontend].instanceList) { + for (const network in config.networks) { + processDefaultCustomInstances(frontend, network, document) + } + } + } } const r = window.location.href.match(/#(.*)/) if (r) loadPage(r[1]) else loadPage("general") + +async function processDefaultCustomInstances(frontend, network, document) { + let networkElement = document.getElementById(frontend).getElementsByClassName(network)[0] + let customInstances = [] + let checkListElement = networkElement.getElementsByClassName("checklist")[0] + + let cloudflareBlackList = [] + let authenticateBlackList = [] + await new Promise(resolve => { + fetch("/instances/blacklist.json") + .then(response => response.text()) + .then(data => { + cloudflareBlackList = JSON.parse(data).cloudflare + authenticateBlackList = JSON.parse(data).authenticate + resolve() + }) + }) + + let frontendDefaultRedirects + + let redirects, options + + await new Promise(async resolve => + browser.storage.local.get(["options", "redirects",], r => { + frontendDefaultRedirects = r.options[frontend][network].enabled + customInstances = r.options[frontend][network].custom + options = r.options + redirects = r.redirects + resolve() + }) + ) + + function calcCheckBoxes() { + for (const element of checkListElement.getElementsByTagName("input")) { + element.checked = frontendDefaultRedirects.includes(element.className) + } + } + if (redirects[frontend][network].length > 0) + checkListElement.innerHTML = [ + ` +
+

${utils.camelCase(network)}

+
+ `, + ...redirects[frontend][network] + .sort((a, b) => + (cloudflareBlackList.includes(a) && !cloudflareBlackList.includes(b)) + || + (authenticateBlackList.includes(a) && !authenticateBlackList.includes(b)) + ) + .map(x => { + const cloudflare = cloudflareBlackList.includes(x) ? ' cloudflare' : "" + const authenticate = authenticateBlackList.includes(x) ? ' authenticate' : "" + + let warnings = [cloudflare, authenticate].join(" ") + return ` +
+ + ${x}${warnings} + + +
` + }), + '
' + ].join("\n
\n") + + localise.localisePage() + + calcCheckBoxes() + + for (let element of checkListElement.getElementsByTagName("input")) { + networkElement.getElementsByClassName(element.className)[0].addEventListener("change", async event => { + if (event.target.checked) frontendDefaultRedirects.push(element.className) + else { + let index = frontendDefaultRedirects.indexOf(element.className) + if (index > -1) frontendDefaultRedirects.splice(index, 1) + } + + options[frontend][network].enabled = frontendDefaultRedirects + browser.storage.local.set({ options }, () => calcCheckBoxes()) + }) + } + + function calcCustomInstances() { + document.getElementById(frontend).getElementsByClassName("custom-checklist")[0].innerHTML = customInstances + .map( + x => ` +
+ ${x} + +
+
` + ) + .join("\n") + + for (const item of customInstances) { + document.getElementById(frontend).getElementsByClassName(`clear-${item}`)[0].addEventListener("click", async () => { + let index = customInstances.indexOf(item) + if (index > -1) customInstances.splice(index, 1) + options[frontend][network].custom = customInstances + browser.storage.local.set({ options }, () => calcCustomInstances()) + }) + } + } + calcCustomInstances() + document.getElementById(frontend).getElementsByClassName("custom-instance-form")[0].addEventListener("submit", async event => { + event.preventDefault(); + event.preventDefault() + let frontendCustomInstanceInput = document.getElementById(frontend).getElementsByClassName("custom-instance")[0] + let url + try { + url = new URL(frontendCustomInstanceInput.value) + } catch (error) { + return + } + let protocolHostVar = utils.protocolHost(url) + if (frontendCustomInstanceInput.validity.valid && !redirects[frontend][network].includes(protocolHostVar)) { + if (!customInstances.includes(protocolHostVar)) { + customInstances.push(protocolHostVar) + options[frontend][network].custom = customInstances + browser.storage.local.set({ options }, () => { + frontendCustomInstanceInput.value = "" + calcCustomInstances() + }) + } + } + }) +} \ No newline at end of file -- cgit 1.4.1