diff --git a/src/assets/images/uploadFiles-icon.svg b/src/assets/images/uploadFiles-icon.svg new file mode 100644 index 0000000..5557664 --- /dev/null +++ b/src/assets/images/uploadFiles-icon.svg @@ -0,0 +1,3 @@ + diff --git a/src/assets/javascripts/get-localstorage.js b/src/assets/javascripts/get-localstorage.js new file mode 100644 index 0000000..6674040 --- /dev/null +++ b/src/assets/javascripts/get-localstorage.js @@ -0,0 +1,15 @@ +window.browser = window.browser || window.chrome + +browser.storage.local.get(["localstorage", "tmp"], r => { + let localstorageJson = r.localstorage + const frontend = r.tmp[0] + const items = r.tmp[1] + localstorageJson[frontend] = {} + + for (const item of items) { + let tmp = localStorage.getItem(item) + if (tmp) localstorageJson[frontend][item] = tmp + } + + browser.storage.local.set({ localstorage: localstorageJson }) +}) diff --git a/src/assets/javascripts/init.js b/src/assets/javascripts/init.js new file mode 100644 index 0000000..c675853 --- /dev/null +++ b/src/assets/javascripts/init.js @@ -0,0 +1,60 @@ +async function getConfig() { + return new Promise(resolve => { + fetch("/config/config.json") + .then(response => response.text()) + .then(data => { + config = JSON.parse(data) + resolve() + }) + }) +} + +let config +await getConfig() + +async function initDefaults() { + return new Promise(async resolve => { + fetch("/instances/data.json") + .then(response => response.text()) + .then(async data => { + browser.storage.local.get(["options", "blacklists"], async r => { + let redirects = JSON.parse(data) + let options = r.options + let targets = {} + const localstorage = {} + const latency = {} + for (const service in config.services) { + options[service] = {} + if (config.services[service].targets == "datajson") { + targets[service] = redirects[service] + } + for (const defaultOption in config.services[service].options) { + options[service][defaultOption] = config.services[service].options[defaultOption] + } + for (const frontend in config.services[service].frontends) { + if (config.services[service].frontends[frontend].instanceList) { + options[frontend] = {} + for (const network in config.networks) { + options[frontend][network] = {} + options[frontend][network].enabled = JSON.parse(data)[frontend][network] + options[frontend][network].custom = [] + } + for (const blacklist in r.blacklists) { + for (const instance of r.blacklists[blacklist]) { + let i = options[frontend].clearnet.enabled.indexOf(instance) + if (i > -1) options[frontend].clearnet.enabled.splice(i, 1) + } + } + } + } + } + browser.storage.local.set({ redirects, options, targets, latency, localstorage }) + resolve() + }) + }) + }) +} + +export default { + initDefaults, +} diff --git a/src/assets/javascripts/services.js b/src/assets/javascripts/services.js index 404962e..718b3f5 100644 --- a/src/assets/javascripts/services.js +++ b/src/assets/javascripts/services.js @@ -2,7 +2,7 @@ window.browser = window.browser || window.chrome import utils from "./utils.js" -let config, redirects, options, targets, blacklists +let config, redirects, options, blacklists async function getConfig() { return new Promise(resolve => { @@ -17,12 +17,10 @@ async function getConfig() { function init() { return new Promise(async resolve => { - // await getConfig() - browser.storage.local.get(["options", "targets", "redirects", "blacklists"], r => { + browser.storage.local.get(["options", "redirects", "blacklists"], r => { if (r.options) { blacklists = r.blacklists redirects = r.redirects - targets = r.targets options = r.options } resolve() @@ -37,7 +35,6 @@ function fetchFrontendInstanceList(service, frontend) { let tmp = [] if (config.services[service].frontends[frontend].instanceList) { for (const network in config.networks) { - if (!redirects[frontend]) console.log(frontend) tmp.push(...redirects[frontend][network], ...options[frontend][network].custom) } } else if (config.services[service].frontends[frontend].singleInstance) tmp = config.services[service].frontends[frontend].singleInstance @@ -45,8 +42,6 @@ function fetchFrontendInstanceList(service, frontend) { } function all(service, frontend) { - // init() - // getConfig() let instances = [] if (!frontend) { for (const frontend in config.services[service].frontends) { @@ -90,10 +85,9 @@ function redirect(url, type, initiator) { if (!regexArray(service, url)) continue if (Object.keys(config.services[service].frontends).length > 1) { - frontend = options[service].frontend - } else { - frontend = Object.keys(config.services[service].frontends)[0] - } + if (type == "sub_frame") frontend = options[service].embedFrontend + else frontend = options[service].frontend + } else frontend = Object.keys(config.services[service].frontends)[0] if (config.services[service].frontends[frontend].instanceList) { let instanceList = [...options[frontend][network].enabled, ...options[frontend][network].custom] @@ -103,7 +97,7 @@ function redirect(url, type, initiator) { } else if (config.services[service].frontends[frontend].singleInstance) randomInstance = config.services[service].frontends[frontend].singleInstance break } - if (frontend == null) return + if (!frontend) return // Here is a (temperory) space for defining constants required in 2 or more switch cases. // When possible, try have the two switch cases share all their code as done with searx and searxng. @@ -391,49 +385,6 @@ function redirect(url, type, initiator) { } } -async function initDefaults() { - return new Promise(async resolve => { - fetch("/instances/data.json") - .then(response => response.text()) - .then(async data => { - browser.storage.local.get(["options", "blacklists"], async r => { - let redirects = JSON.parse(data) - let options = r.options - let targets = {} - const localstorage = {} - const latency = {} - for (const service in config.services) { - options[service] = {} - if (config.services[service].targets == "datajson") { - targets[service] = redirects[service] - } - for (const defaultOption in config.services[service].options) { - options[service][defaultOption] = config.services[service].options[defaultOption] - } - for (const frontend in config.services[service].frontends) { - if (config.services[service].frontends[frontend].instanceList) { - options[frontend] = {} - for (const network in config.networks) { - options[frontend][network] = {} - options[frontend][network].enabled = JSON.parse(data)[frontend][network] - options[frontend][network].custom = [] - } - for (const blacklist in r.blacklists) { - for (const instance of r.blacklists[blacklist]) { - let i = options[frontend].clearnet.enabled.indexOf(instance) - if (i > -1) options[frontend].clearnet.enabled.splice(i, 1) - } - } - } - } - } - browser.storage.local.set({ redirects, options, targets, latency, localstorage }) - resolve() - }) - }) - }) -} - function computeService(url, returnFrontend) { for (const service in config.services) { if (regexArray(service, url)) { @@ -454,8 +405,6 @@ function computeService(url, returnFrontend) { function switchInstance(url) { return new Promise(async resolve => { - // await init() - // await getConfig() const protocolHost = utils.protocolHost(url) for (const service in config.services) { if (!options[service].enabled) continue @@ -487,8 +436,6 @@ function switchInstance(url) { function reverse(url) { return new Promise(async resolve => { - // await init() - // await getConfig() let protocolHost = utils.protocolHost(url) let currentService for (const service in config.services) { @@ -517,8 +464,6 @@ function reverse(url) { function unifyPreferences(url, tabId) { return new Promise(async resolve => { - // await init() - // await getConfig() const protocolHost = utils.protocolHost(url) let currentFrontend, currentService serviceloop: for (const service in config.services) { @@ -540,18 +485,14 @@ function unifyPreferences(url, tabId) { } } if ("localstorage" in frontend.preferences) { + browser.storage.local.set({ tmp: [currentFrontend, frontend.preferences.localstorage] }) browser.tabs.executeScript(tabId, { - code: "const frontend = " + frontend, - code: "const items = " + config.services[currentService].frontends[currentFrontend].preferences.localStorage, - //file: "/assets/javascripts/get-localstorage.js", + file: "/assets/javascripts/get-localstorage.js", runAt: "document_start", }) - for (const instance of instancesList) browser.tabs.create({ url: instance }, tab => browser.tabs.executeScript(tab.id, { - code: "const frontend = " + frontend, - code: "const items = " + config.services[currentService].frontends[currentFrontend].preferences.localStorage, file: "/assets/javascripts/set-localstorage.js", runAt: "document_start", }) @@ -594,7 +535,6 @@ function setRedirects(redirects) { export default { redirect, - initDefaults, computeService, switchInstance, reverse, diff --git a/src/assets/javascripts/set-localstorage.js b/src/assets/javascripts/set-localstorage.js new file mode 100644 index 0000000..8dbfcad --- /dev/null +++ b/src/assets/javascripts/set-localstorage.js @@ -0,0 +1,13 @@ +window.browser = window.browser || window.chrome + +browser.storage.local.get(["localstorage", "tmp"], r => { + const localstorageJson = r.localstorage + const frontend = r.tmp[0] + const items = localstorageJson[frontend] + + for (const item in items) { + localStorage.setItem(item, items[item]) + } + + window.close() +}) diff --git a/src/assets/javascripts/utils.js b/src/assets/javascripts/utils.js index a1c559d..aaa5295 100644 --- a/src/assets/javascripts/utils.js +++ b/src/assets/javascripts/utils.js @@ -244,22 +244,20 @@ async function testLatency(element, instances, frontend) { return new Promise(async resolve => { let myList = {} let latencyThreshold, options - //let redirectsChecks = [] browser.storage.local.get(["options"], r => { latencyThreshold = r.options.latencyThreshold - //redirectsChecks = r.options[frontend].clearnet.enabled options = r.options }) - for (const href of instances) + for (const href of instances) { await ping(href).then(time => { + let color if (time) { myList[href] = time - let color if (time <= 1000) color = "green" else if (time <= 2000) color = "orange" else color = "red" - if (time > latencyThreshold) { + if (time > latencyThreshold && options[frontend].clearnet.enabled.includes(href)) { options[frontend].clearnet.enabled.splice(options[frontend].clearnet.enabled.indexOf(href), 1) } @@ -268,8 +266,13 @@ async function testLatency(element, instances, frontend) { else if (time > 5000) text = `ERROR: ${time - 5000}` else text = `${time}ms` element.innerHTML = `${href}: ${text}` + } else { + color = "red" + element.innerHTML = `${href}: Server not found` + if (options[frontend].clearnet.enabled.includes(href)) options[frontend].clearnet.enabled.splice(options[frontend].clearnet.enabled.indexOf(href), 1) } }) + } browser.storage.local.set({ options }) resolve(myList) }) @@ -423,6 +426,7 @@ function latency(service, frontend, document, location) { latencyElement.addEventListener("click", reloadWindow) browser.storage.local.get(["redirects", "latency"], r => { let redirects = r.redirects + let latency = r.latency const oldHtml = latencyLabel.innerHTML latencyLabel.innerHTML = "..." testLatency(latencyLabel, redirects[frontend].clearnet, frontend).then(r => { diff --git a/src/pages/background/background.js b/src/pages/background/background.js index 3a90060..5e164d5 100644 --- a/src/pages/background/background.js +++ b/src/pages/background/background.js @@ -3,6 +3,7 @@ import generalHelper from "../../assets/javascripts/general.js" import utils from "../../assets/javascripts/utils.js" import servicesHelper from "../../assets/javascripts/services.js" +import initHelper from "../../assets/javascripts/init.js" window.browser = window.browser || window.chrome @@ -38,7 +39,7 @@ function initDefaults() { .then(async data => { browser.storage.local.set({ blacklists: JSON.parse(data) }, async () => { await generalHelper.initDefaults() - await servicesHelper.initDefaults() + await initHelper.initDefaults() }) }) }) diff --git a/src/pages/options/index.html b/src/pages/options/index.html index 71d04fd..384b32b 100644 --- a/src/pages/options/index.html +++ b/src/pages/options/index.html @@ -3252,19 +3252,19 @@