diff options
Diffstat (limited to 'src/pages/options')
-rw-r--r-- | src/pages/options/index.ejs | 13 | ||||
-rw-r--r-- | src/pages/options/index.js | 199 | ||||
-rw-r--r-- | src/pages/options/widgets/general.js | 12 | ||||
-rw-r--r-- | src/pages/options/widgets/services.js | 76 | ||||
-rw-r--r-- | src/pages/options/widgets/services.pug | 11 |
5 files changed, 202 insertions, 109 deletions
diff --git a/src/pages/options/index.ejs b/src/pages/options/index.ejs deleted file mode 100644 index 7f09e6da..00000000 --- a/src/pages/options/index.ejs +++ /dev/null @@ -1,13 +0,0 @@ -<!DOCTYPE html> -<html id="elementToShowWithJavaScript" lang="en"> - <%- include('src/pages/widgets/head') -%> - <body class="option" dir="auto"> - <%- include('src/pages/widgets/links', {services: services}) -%> - <div id="pages"> - <%- include('src/pages/options/widgets/general', {config: {networks, services}}) -%> - <%- include('src/pages/options/widgets/services', {config: {networks, services}}) -%> - <%- include('src/pages/options/widgets/about') -%> - </div> - </body> - <script type="module" src="./index.js"></script> -</html> 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 = [ + ` + <div class="some-block option-block"> + <h4>${utils.camelCase(network)}</h4> + </div> + `, + ...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) ? ' <span style="color:red;">cloudflare</span>' : "" + const authenticate = authenticateBlackList.includes(x) ? ' <span style="color:orange;">authenticate</span>' : "" + + let warnings = [cloudflare, authenticate].join(" ") + return ` + <div> + <x> + <a href="${x}" target="_blank">${x}</a>${warnings} + </x> + <input type="checkbox" class="${x}"/> + </div>` + }), + '<br>' + ].join("\n<hr>\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 => ` + <div> + ${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"> + <path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12 19 6.41z" /> + </svg> + </button> + </div> + <hr>` + ) + .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 diff --git a/src/pages/options/widgets/general.js b/src/pages/options/widgets/general.js index a73ac85e..2fd6a9a5 100644 --- a/src/pages/options/widgets/general.js +++ b/src/pages/options/widgets/general.js @@ -5,21 +5,11 @@ import utils from "../../../assets/javascripts/utils.js" import generalHelper from "../../../assets/javascripts/general.js" import servicesHelper from "../../../assets/javascripts/services.js" -let updateInstancesElement = document.getElementById("update-instances") -updateInstancesElement.addEventListener("click", async () => { - let oldHtml = updateInstancesElement.innerHTML - updateInstancesElement.innerHTML = "..." - if (await utils.updateInstances()) { - updateInstancesElement.innerHTML = oldHtml - location.reload() - } else updateInstancesElement.innerHTML = "Failed Miserabely" -}) - let config async function getConfig() { return new Promise(resolve => { - fetch("/config/config.json") + fetch("/config.json") .then(response => response.text()) .then(data => { config = JSON.parse(data) diff --git a/src/pages/options/widgets/services.js b/src/pages/options/widgets/services.js deleted file mode 100644 index 06c560f8..00000000 --- a/src/pages/options/widgets/services.js +++ /dev/null @@ -1,76 +0,0 @@ -import utils from "../../../assets/javascripts/utils.js" - -let config, - options, - divs = {} - -function getConfig() { - return new Promise(resolve => { - fetch("/config/config.json") - .then(response => response.text()) - .then(data => { - config = JSON.parse(data) - resolve() - }) - }) -} - -function getOptions() { - return new Promise(resolve => { - browser.storage.local.get("options", r => { - options = r.options - resolve() - }) - }) -} - -await getConfig() -await getOptions() - -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" - } - } - } - } -} - -for (const service in config.services) { - divs[service] = {} - //divs[service].page = document.getElementById(`${service}_page`) - 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) { - utils.processDefaultCustomInstances(service, frontend, network, document) - } - } - } -} diff --git a/src/pages/options/widgets/services.pug b/src/pages/options/widgets/services.pug index 3a32f517..ddd244d4 100644 --- a/src/pages/options/widgets/services.pug +++ b/src/pages/options/widgets/services.pug @@ -28,25 +28,18 @@ each val, service in services option(value="sub_frame" data-localise="__MSG_onlyEmbedded__") Only Embedded option(value="main_frame" data-localise="__MSG_onlyNotEmbedded__") Only Not Embedded - hr - each val, frontend in services[service].frontends if services[service].frontends[frontend].instanceList div(id=frontend) each val, network in networks div(class=network) - div(class="some-block option-block") - h4=network.charAt(0).toUpperCase() + network.slice(1) div(class="checklist") - hr div(class="some-block option-block") h4(data-localise="__MSG_customInstances__") Custom Instances form(class="custom-instance-form") div(class="some-block option-block") - input(class="custom-instance" placeholder="http://<%= frontend %>.com" type="url" ) + input(class="custom-instance" placeholder=`http://${frontend}.com` type="url" ) button(class="add add-instance" type="submit") svg(xmlns="https://www.w3.org/2000/svg" height="20px" viewBox="0 0 24 24" width="20px" fill="currentColor") path(d="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z") - div(class="checklist custom-checklist") - - script(type="module" src="./widgets/services.js") + div(class="checklist custom-checklist") \ No newline at end of file |