From 320d0175a8e1223618eb3b8d740b420ef674fc8d Mon Sep 17 00:00:00 2001 From: Hygna Date: Thu, 22 Sep 2022 19:58:07 +0100 Subject: Completed Migration to ejs --- src/pages/popup/popup.html | 396 +++++++++++++++++++++++++-------------------- 1 file changed, 217 insertions(+), 179 deletions(-) (limited to 'src/pages/popup/popup.html') diff --git a/src/pages/popup/popup.html b/src/pages/popup/popup.html index 25f70f48..e21a63a0 100644 --- a/src/pages/popup/popup.html +++ b/src/pages/popup/popup.html @@ -8,189 +8,227 @@
- - - - - - - - - - -
-

IMDb

- -
- - -
-

LBRY

- -
- - - - + + + + + + + + + + + + + + + + + + +

- - - - - - - - - - -
-

IMDb

- -
- - -
-

LBRY

- -
- - - - + + + + + + + + + + + + + + + + + + +

@@ -222,4 +260,4 @@ - \ No newline at end of file + -- cgit 1.4.1 From 800d0cbd9fd4286f0affcb5a28f01f03532655fb Mon Sep 17 00:00:00 2001 From: Hygna Date: Wed, 28 Sep 2022 20:18:32 +0100 Subject: Unify cookies --- README.md | 14 ++--- package.json | 2 - src/assets/javascripts/general.js | 6 +- src/assets/javascripts/services.js | 41 ++++++++----- src/assets/javascripts/utils.js | 106 +++++++++++++++++----------------- src/config/config.json | 2 +- src/pages/background/background.js | 2 +- src/pages/options/index.html | 2 +- src/pages/options/init.js | 4 +- src/pages/options/widgets/general.ejs | 2 +- src/pages/popup/popup.html | 2 +- src/pages/popup/popup.js | 10 ++-- 12 files changed, 99 insertions(+), 94 deletions(-) (limited to 'src/pages/popup/popup.html') diff --git a/README.md b/README.md index 4de39765..2c06b0a2 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ Imgur => [Rimgo](https://codeberg.org/video-prize-ranch/rimgo)\ Wikipedia => [Wikiless](https://codeberg.org/orenom/wikiless)\ Medium => [Scribe](https://sr.ht/~edwardloveall/scribe/)\ Quora => [Quetre](https://github.com/zyachel/quetre)\ -IMDb => [Libremdb](https://github.com/zyachel/libremdb)\ +IMDb => [libremdb](https://github.com/zyachel/libremdb)\ PeerTube => [SimpleerTube](https://git.sr.ht/~metalune/simpleweb_peertube)\ LBRY/Odysee => [Librarian](https://codeberg.org/librarian/librarian), [LBRY Desktop](https://lbry.com/get)\ Search => [SearXNG](https://github.com/searxng/searxng), [SearX](https://searx.github.io/searx/), [Whoogle](https://benbusby.com/projects/whoogle-search/), [LibreX](https://github.com/hnhx/librex/)\ @@ -72,21 +72,19 @@ npm update npm install ``` -If you are modifying any files ending with .pug, the pug cli needs to be installed with the following command (with root privileges): +If you are modifying any files ending with .ejs, you need to run the following command to render html: ``` -npm install -g pug-cli +npm run ejs ``` -and then run `npm run pug` to generate pages in the background. - -### Build +### Build the extention zip archive: ``` npm run build ``` -### Test +### Run automated tests ``` npm run test @@ -120,4 +118,4 @@ select `load unpacked extension`\ select `src` folder [Privacy Policy](Privacy-Policy.md)\ -Credits: [Privacy Redirect](https://github.com/SimonBrazell/privacy-redirect) +Forked from [Privacy Redirect](https://github.com/SimonBrazell/privacy-redirect) diff --git a/package.json b/package.json index 76f57474..ec39aee4 100644 --- a/package.json +++ b/package.json @@ -9,8 +9,6 @@ "start": "web-ext run --browser-console --source-dir ./src/", "build": "web-ext build --overwrite-dest --source-dir ./src/", "test": "web-ext lint --source-dir ./src/ || true", - "pug": "pug ./src/pages/options/*.pug ./src/pages/popup/ -P -w", - "prettier": "npx prettier --write .", "instances": "python3 src/instances/get_instances.py; git update-index --assume-unchanged src/instances/blacklist.json src/instances/data.json", "ejs": "npx ejs src/pages/options/index.ejs -f src/config/config.json -o src/pages/options/index.html; npx ejs src/pages/popup/popup.ejs -f src/config/config.json -o src/pages/popup/popup.html" }, diff --git a/src/assets/javascripts/general.js b/src/assets/javascripts/general.js index 3a8987ac..9dcba752 100644 --- a/src/assets/javascripts/general.js +++ b/src/assets/javascripts/general.js @@ -11,8 +11,8 @@ function isException(url) { function init() { return new Promise(resolve => { - browser.storage.local.get("exceptions", r => { - exceptions = r.exceptions + browser.storage.local.get("options", r => { + exceptions = r.options.exceptions resolve() }) }) @@ -30,7 +30,7 @@ async function initDefaults() { url: [], regex: [], }, - theme: "DEFAULT", + theme: "detect", popupServices: ["youtube", "twitter", "instagram", "tiktok", "imgur", "reddit", "quora", "translate", "maps"], autoRedirect: false, firstPartyIsolate: false, diff --git a/src/assets/javascripts/services.js b/src/assets/javascripts/services.js index 9fc5a0a3..3db12595 100644 --- a/src/assets/javascripts/services.js +++ b/src/assets/javascripts/services.js @@ -19,25 +19,28 @@ function init() { return new Promise(async resolve => { // await getConfig() browser.storage.local.get(["options", "targets", "redirects", "blacklists"], r => { - blacklists = r.blacklists - redirects = r.redirects - targets = r.targets - options = r.options + if (r.options) { + blacklists = r.blacklists + redirects = r.redirects + targets = r.targets + options = r.options + } resolve() }) }) } -await init() await getConfig() +await init() function fetchFrontendInstanceList(service, frontend) { let tmp = [] - if (!config.services[service].frontends[frontend].singleInstance) { + 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 != undefined) tmp = config.services[service].frontends[frontend].singleInstance + } else if (config.services[service].frontends[frontend].singleInstance) tmp = config.services[service].frontends[frontend].singleInstance return tmp } @@ -58,8 +61,8 @@ function all(service, frontend) { function regexArray(service, url) { let targets if (config.services[service].targets == "datajson") { - browser.storage.local.get(`${service}Targets`, r => { - targets = r[service + "Targets"] + browser.storage.local.get("targets", r => { + targets = r.targets[service] }) } else { targets = config.services[service].targets @@ -397,8 +400,10 @@ function initDefaults() { let redirects = JSON.parse(data) let options = r.options let targets = {} + // let latency = {} for (const service in config.services) { options[service] = {} + // latency[service] = {} if (config.services[service].targets == "datajson") { targets[service] = redirects[service] //delete dataJson[service] @@ -423,19 +428,25 @@ function initDefaults() { } } } - browser.storage.local.set({ redirects, options, targets }) + browser.storage.local.set({ redirects, options, targets /*, latency*/ }) resolve() }) }) }) } -function computeService(url) { +function computeService(url, returnFrontend) { for (const service in config.services) { if (regexArray(service, url)) { - return service - } else if (all(service).includes(utils.protocolHost(url))) { - return service + if (returnFrontend) return [service, null] + else return service + } else { + for (const frontend in config.services[service].frontends) { + if (all(service, frontend).includes(utils.protocolHost(url))) { + if (returnFrontend) return [service, frontend] + else return service + } + } } } return null @@ -524,7 +535,7 @@ function unifyPreferences(url) { const frontend = config.services[currentService].frontends[currentFrontend] if ("cookies" in frontend.preferences) { - for (const cookie in frontend.preferences.cookies) { + for (const cookie of frontend.preferences.cookies) { await utils.copyCookie(currentFrontend, url, instancesList, cookie) } } diff --git a/src/assets/javascripts/utils.js b/src/assets/javascripts/utils.js index 0e8acb3c..70a93240 100644 --- a/src/assets/javascripts/utils.js +++ b/src/assets/javascripts/utils.js @@ -111,29 +111,27 @@ function protocolHost(url) { return `${url.protocol}//${url.host}` } -async function processDefaultCustomInstances(target, name, protocol, document) { - let latencyKey = `${name}Latency` +async function processDefaultCustomInstances(service, name, network, document) { let instancesLatency - let nameProtocolElement = document.getElementById(name).getElementsByClassName(protocol)[0] + let nameNetworkElement = document.getElementById(name).getElementsByClassName(network)[0] let nameCustomInstances = [] - let nameCheckListElement = nameProtocolElement.getElementsByClassName("checklist")[0] + let nameCheckListElement = nameNetworkElement.getElementsByClassName("checklist")[0] await initBlackList() let nameDefaultRedirects - let redirectsChecks = `${name}${camelCase(protocol)}RedirectsChecks` - let customRedirects = `${name}${camelCase(protocol)}CustomRedirects` - - let redirects + let redirects, options async function getFromStorage() { return new Promise(async resolve => - browser.storage.local.get([redirectsChecks, customRedirects, "redirects", latencyKey], r => { - nameDefaultRedirects = r[redirectsChecks] - nameCustomInstances = r[customRedirects] - instancesLatency = r[latencyKey] ?? [] + browser.storage.local.get(["options", "redirects", "latency"], r => { + nameDefaultRedirects = r.options[name][network].enabled + nameCustomInstances = r.options[name][network].custom + options = r.options + if (r.latency) instancesLatency = r.latency[name] ?? [] + else instancesLatency = [] redirects = r.redirects resolve() }) @@ -141,12 +139,11 @@ async function processDefaultCustomInstances(target, name, protocol, document) { } await getFromStorage() - if (nameCustomInstances === undefined) console.log(customRedirects) function calcNameCheckBoxes() { let isTrue = true - for (const item of redirects[name][protocol]) { - if (nameDefaultRedirects === undefined) console.log(name + protocol + " is undefined") + for (const item of redirects[name][network]) { + if (nameDefaultRedirects === undefined) console.log(name + network + " is undefined") if (!nameDefaultRedirects.includes(item)) { isTrue = false break @@ -156,14 +153,14 @@ async function processDefaultCustomInstances(target, name, protocol, document) { element.checked = nameDefaultRedirects.includes(element.className) } if (nameDefaultRedirects.length == 0) isTrue = false - nameProtocolElement.getElementsByClassName("toggle-all")[0].checked = isTrue + nameNetworkElement.getElementsByClassName("toggle-all")[0].checked = isTrue } nameCheckListElement.innerHTML = [ `
Toggle All
`, - ...redirects[name][protocol].map(x => { + ...redirects[name][network].map(x => { const cloudflare = cloudflareBlackList.includes(x) ? ' cloudflare' : "" const authenticate = authenticateBlackList.includes(x) ? ' authenticate' : "" const offline = offlineBlackList.includes(x) ? ' offline' : "" @@ -188,31 +185,32 @@ async function processDefaultCustomInstances(target, name, protocol, document) { localise.localisePage() calcNameCheckBoxes() - nameProtocolElement.getElementsByClassName("toggle-all")[0].addEventListener("change", async event => { - if (event.target.checked) nameDefaultRedirects = [...redirects[name][protocol]] + nameNetworkElement.getElementsByClassName("toggle-all")[0].addEventListener("change", async event => { + if (event.service.checked) nameDefaultRedirects = [...redirects[name][network]] else nameDefaultRedirects = [] - browser.storage.local.set({ [redirectsChecks]: nameDefaultRedirects }) + options[service][network].enabled = nameDefaultRedirects + browser.storage.local.set({ options }) calcNameCheckBoxes() }) for (let element of nameCheckListElement.getElementsByTagName("input")) { if (element.className != "toggle-all") - nameProtocolElement.getElementsByClassName(element.className)[0].addEventListener("change", async event => { - if (event.target.checked) nameDefaultRedirects.push(element.className) + nameNetworkElement.getElementsByClassName(element.className)[0].addEventListener("change", async event => { + if (event.service.checked) nameDefaultRedirects.push(element.className) else { let index = nameDefaultRedirects.indexOf(element.className) if (index > -1) nameDefaultRedirects.splice(index, 1) } - browser.storage.local.set({ - [redirectsChecks]: nameDefaultRedirects, - }) + + options[service][network].enabled = nameDefaultRedirects + browser.storage.local.set({ options }) calcNameCheckBoxes() }) } function calcNameCustomInstances() { - nameProtocolElement.getElementsByClassName("custom-checklist")[0].innerHTML = nameCustomInstances + nameNetworkElement.getElementsByClassName("custom-checklist")[0].innerHTML = nameCustomInstances .map( x => `
${x} @@ -227,24 +225,26 @@ async function processDefaultCustomInstances(target, name, protocol, document) { .join("\n") for (const item of nameCustomInstances) { - nameProtocolElement.getElementsByClassName(`clear-${item}`)[0].addEventListener("click", async () => { + nameNetworkElement.getElementsByClassName(`clear-${item}`)[0].addEventListener("click", async () => { let index = nameCustomInstances.indexOf(item) if (index > -1) nameCustomInstances.splice(index, 1) - browser.storage.local.set({ [customRedirects]: nameCustomInstances }) + options[service][network].custom = nameCustomInstances + browser.storage.local.set({ options }) calcNameCustomInstances() }) } } calcNameCustomInstances() - nameProtocolElement.getElementsByClassName("custom-instance-form")[0].addEventListener("submit", async event => { + nameNetworkElement.getElementsByClassName("custom-instance-form")[0].addEventListener("submit", async event => { event.preventDefault() - let nameCustomInstanceInput = nameProtocolElement.getElementsByClassName("custom-instance")[0] + let nameCustomInstanceInput = nameNetworkElement.getElementsByClassName("custom-instance")[0] let url = new URL(nameCustomInstanceInput.value) let protocolHostVar = protocolHost(url) - if (nameCustomInstanceInput.validity.valid && !redirects[name][protocol].includes(protocolHostVar)) { + if (nameCustomInstanceInput.validity.valid && !redirects[name][network].includes(protocolHostVar)) { if (!nameCustomInstances.includes(protocolHostVar)) { nameCustomInstances.push(protocolHostVar) - browser.storage.local.set({ [customRedirects]: nameCustomInstances }) + options[service][network].custom = nameCustomInstances + browser.storage.local.set({ options }) nameCustomInstanceInput.value = "" } calcNameCustomInstances() @@ -299,9 +299,9 @@ async function testLatency(element, instances, frontend) { let myList = {} let latencyThreshold let redirectsChecks = [] - browser.storage.local.get(["latencyThreshold", `${frontend}ClearnetRedirectsChecks`], r => { - latencyThreshold = r.latencyThreshold - redirectsChecks = r[`${frontend}ClearnetRedirectsChecks`] + browser.storage.local.get(["options"], r => { + latencyThreshold = r.options.latencyThreshold + redirectsChecks = r.options[frontend].clearnet.enabled }) for (const href of instances) await ping(href).then(time => { @@ -331,9 +331,9 @@ async function testLatency(element, instances, frontend) { function copyCookie(frontend, targetUrl, urls, name) { return new Promise(resolve => { - browser.storage.local.get("firstPartyIsolate", r => { + browser.storage.local.get("options", r => { let query - if (!r.firstPartyIsolate) + if (!r.options.firstPartyIsolate) query = { url: protocolHost(targetUrl), name: name, @@ -348,7 +348,7 @@ function copyCookie(frontend, targetUrl, urls, name) { for (const cookie of cookies) if (cookie.name == name) { for (const url of urls) { - const setQuery = r.firstPartyIsolate + const setQuery = r.options.firstPartyIsolate ? { url: url, name: name, @@ -375,23 +375,21 @@ function copyCookie(frontend, targetUrl, urls, name) { function getPreferencesFromToken(frontend, targetUrl, urls, name, endpoint) { return new Promise(resolve => { - browser.storage.local.get("firstPartyIsolate", r => { + const http = new XMLHttpRequest() + const url = `${targetUrl}${endpoint}` + http.open("GET", url, false) + //http.setRequestHeader("Cookie", `${name}=${cookie.value}`) + http.send(null) + const preferences = JSON.parse(http.responseText) + let formdata = new FormData() + for (var key in preferences) formdata.append(key, preferences[key]) + for (const url of urls) { const http = new XMLHttpRequest() - const url = `${targetUrl}${endpoint}` - http.open("GET", url, false) - http.setRequestHeader("Cookie", `${name}=${cookie.value}`) + http.open("POST", `${url}/settings/stay`, false) http.send(null) - const preferences = JSON.parse(http.responseText) - let formdata = new FormData() - for (var key in preferences) formdata.append(key, preferences[key]) - for (const url of urls) { - const http = new XMLHttpRequest() - http.open("POST", `${url}/settings/stay`, false) - http.send(null) - } - resolve() - return - }) + } + resolve() + return }) } @@ -427,7 +425,7 @@ function copyRaw(test, copyRawElement) { }) } -function unify(test) { +function unify() { return new Promise(resolve => { browser.tabs.query({ active: true, currentWindow: true }, async tabs => { let currTab = tabs[0] diff --git a/src/config/config.json b/src/config/config.json index 09774895..6f926034 100644 --- a/src/config/config.json +++ b/src/config/config.json @@ -184,7 +184,7 @@ "instanceList": true } }, - "targets": ["^https?:\\/{2}(www\\.)?instagram\\.com"], + "targets": ["^https?:\\/{2}(www\\.)?instagram\\.com\\/p\\/"], "name": "Instagram", "options": { "enabled": true }, "imageType": "png", diff --git a/src/pages/background/background.js b/src/pages/background/background.js index 241b51de..e9bf691f 100644 --- a/src/pages/background/background.js +++ b/src/pages/background/background.js @@ -121,7 +121,7 @@ async function redirectOfflineInstance(url, tabId) { let counter = 0 function isAutoRedirect() { - return new Promise(resolve => browser.storage.local.get("autoRedirect", r => resolve(r.autoRedirect == true))) + return new Promise(resolve => browser.storage.local.get("options", r => resolve(r.options.autoRedirect == true))) } browser.webRequest.onResponseStarted.addListener( diff --git a/src/pages/options/index.html b/src/pages/options/index.html index 20592637..6ef33723 100644 --- a/src/pages/options/index.html +++ b/src/pages/options/index.html @@ -99,7 +99,7 @@

Theme

diff --git a/src/pages/options/init.js b/src/pages/options/init.js index 5b679da4..cac23748 100644 --- a/src/pages/options/init.js +++ b/src/pages/options/init.js @@ -4,8 +4,8 @@ import localise from "../../assets/javascripts/localise.js" function changeTheme() { return new Promise(resolve => { - browser.storage.local.get("theme", r => { - switch (r.theme) { + browser.storage.local.get("options", r => { + switch (r.options.theme) { case "dark": document.body.classList.add("dark-theme") document.body.classList.remove("light-theme") diff --git a/src/pages/options/widgets/general.ejs b/src/pages/options/widgets/general.ejs index fa85e8bc..8f7ab029 100644 --- a/src/pages/options/widgets/general.ejs +++ b/src/pages/options/widgets/general.ejs @@ -6,7 +6,7 @@
-
+

Unify Settings

diff --git a/src/pages/popup/popup.js b/src/pages/popup/popup.js index 098ae01a..5a5ccb53 100644 --- a/src/pages/popup/popup.js +++ b/src/pages/popup/popup.js @@ -78,11 +78,11 @@ browser.storage.local.get("options", r => { return } - const currentService = serviceHelper.computeService(url) - if (currentService != null) { - divs[currentService].current.classList.remove("hide") - divs[currentService].all.classList.add("hide") - if (config.services[currentService].preferences != undefined) { + const [service, frontend] = serviceHelper.computeService(url, true) + if (service) { + divs[service].current.classList.remove("hide") + divs[service].all.classList.add("hide") + if (config.services[service].frontends[frontend].preferences) { const unify = document.getElementById("unify") const textElement = document.getElementById("unify").getElementsByTagName("h4")[0] unify.addEventListener("click", () => { -- cgit 1.4.1 From 18facf37a2f058b1314f5fd3a1f9d36c70ba27f5 Mon Sep 17 00:00:00 2001 From: Hygna Date: Fri, 30 Sep 2022 14:54:56 +0100 Subject: Squashed a few bugs --- src/assets/images/sendTargets-icon.svg | 3 - src/assets/javascripts/services.js | 36 +++++++--- src/assets/javascripts/utils.js | 124 ++++++++++++++++----------------- src/config/config.json | 19 ++--- src/pages/background/background.js | 30 ++++---- src/pages/options/index.html | 14 ++-- src/pages/options/widgets/general.js | 2 +- src/pages/options/widgets/services.js | 2 +- src/pages/popup/popup.html | 20 +++--- 9 files changed, 131 insertions(+), 119 deletions(-) delete mode 100644 src/assets/images/sendTargets-icon.svg (limited to 'src/pages/popup/popup.html') diff --git a/src/assets/images/sendTargets-icon.svg b/src/assets/images/sendTargets-icon.svg deleted file mode 100644 index 5557664e..00000000 --- a/src/assets/images/sendTargets-icon.svg +++ /dev/null @@ -1,3 +0,0 @@ - - - diff --git a/src/assets/javascripts/services.js b/src/assets/javascripts/services.js index 49af6d1f..404962ee 100644 --- a/src/assets/javascripts/services.js +++ b/src/assets/javascripts/services.js @@ -391,7 +391,7 @@ function redirect(url, type, initiator) { } } -function initDefaults() { +async function initDefaults() { return new Promise(async resolve => { fetch("/instances/data.json") .then(response => response.text()) @@ -400,13 +400,12 @@ function initDefaults() { let redirects = JSON.parse(data) let options = r.options let targets = {} - // let latency = {} + const localstorage = {} + const latency = {} for (const service in config.services) { options[service] = {} - // latency[service] = {} if (config.services[service].targets == "datajson") { targets[service] = redirects[service] - //delete dataJson[service] } for (const defaultOption in config.services[service].options) { options[service][defaultOption] = config.services[service].options[defaultOption] @@ -416,11 +415,11 @@ function initDefaults() { options[frontend] = {} for (const network in config.networks) { options[frontend][network] = {} - options[frontend][network].enabled = redirects[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 blacklist) { + 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) } @@ -428,7 +427,7 @@ function initDefaults() { } } } - browser.storage.local.set({ redirects, options, targets /*, latency*/ }) + browser.storage.local.set({ redirects, options, targets, latency, localstorage }) resolve() }) }) @@ -449,7 +448,8 @@ function computeService(url, returnFrontend) { } } } - return null + if (returnFrontend) return [null, null] + else return null } function switchInstance(url) { @@ -515,7 +515,7 @@ function reverse(url) { }) } -function unifyPreferences(url) { +function unifyPreferences(url, tabId) { return new Promise(async resolve => { // await init() // await getConfig() @@ -539,7 +539,23 @@ function unifyPreferences(url) { await utils.copyCookie(currentFrontend, url, instancesList, cookie) } } - if ("localStorage" in frontend.preferences) { + if ("localstorage" in frontend.preferences) { + browser.tabs.executeScript(tabId, { + code: "const frontend = " + frontend, + code: "const items = " + config.services[currentService].frontends[currentFrontend].preferences.localStorage, + //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", + }) + ) } if ("indexeddb" in frontend.preferences) { } diff --git a/src/assets/javascripts/utils.js b/src/assets/javascripts/utils.js index 186fae43..a1c559df 100644 --- a/src/assets/javascripts/utils.js +++ b/src/assets/javascripts/utils.js @@ -59,27 +59,26 @@ function protocolHost(url) { return `${url.protocol}//${url.host}` } -async function processDefaultCustomInstances(service, name, network, document) { +async function processDefaultCustomInstances(service, frontend, network, document) { let instancesLatency - let nameNetworkElement = document.getElementById(name).getElementsByClassName(network)[0] + let frontendNetworkElement = document.getElementById(frontend).getElementsByClassName(network)[0] - let nameCustomInstances = [] - let nameCheckListElement = nameNetworkElement.getElementsByClassName("checklist")[0] + let frontendCustomInstances = [] + let frontendCheckListElement = frontendNetworkElement.getElementsByClassName("checklist")[0] await initBlackList() - let nameDefaultRedirects + let frontendDefaultRedirects let redirects, options async function getFromStorage() { return new Promise(async resolve => browser.storage.local.get(["options", "redirects", "latency"], r => { - nameDefaultRedirects = r.options[name][network].enabled - nameCustomInstances = r.options[name][network].custom + frontendDefaultRedirects = r.options[frontend][network].enabled + frontendCustomInstances = r.options[frontend][network].custom options = r.options - if (r.latency) instancesLatency = r.latency[name] ?? [] - else instancesLatency = [] + instancesLatency = r.latency[frontend] ?? [] redirects = r.redirects resolve() }) @@ -88,27 +87,26 @@ async function processDefaultCustomInstances(service, name, network, document) { await getFromStorage() - function calcNameCheckBoxes() { + function calcFrontendCheckBoxes() { let isTrue = true - for (const item of redirects[name][network]) { - if (nameDefaultRedirects === undefined) console.log(name + network + " is undefined") - if (!nameDefaultRedirects.includes(item)) { + for (const item of redirects[frontend][network]) { + if (!frontendDefaultRedirects.includes(item)) { isTrue = false break } } - for (const element of nameCheckListElement.getElementsByTagName("input")) { - element.checked = nameDefaultRedirects.includes(element.className) + for (const element of frontendCheckListElement.getElementsByTagName("input")) { + element.checked = frontendDefaultRedirects.includes(element.className) } - if (nameDefaultRedirects.length == 0) isTrue = false - nameNetworkElement.getElementsByClassName("toggle-all")[0].checked = isTrue + if (frontendDefaultRedirects.length == 0) isTrue = false + frontendNetworkElement.getElementsByClassName("toggle-all")[0].checked = isTrue } - nameCheckListElement.innerHTML = [ + frontendCheckListElement.innerHTML = [ `
Toggle All
`, - ...redirects[name][network].map(x => { + ...redirects[frontend][network].map(x => { const cloudflare = cloudflareBlackList.includes(x) ? ' cloudflare' : "" const authenticate = authenticateBlackList.includes(x) ? ' authenticate' : "" const offline = offlineBlackList.includes(x) ? ' offline' : "" @@ -132,33 +130,33 @@ async function processDefaultCustomInstances(service, name, network, document) { localise.localisePage() - calcNameCheckBoxes() - nameNetworkElement.getElementsByClassName("toggle-all")[0].addEventListener("change", async event => { - if (event.service.checked) nameDefaultRedirects = [...redirects[name][network]] - else nameDefaultRedirects = [] + calcFrontendCheckBoxes() + frontendNetworkElement.getElementsByClassName("toggle-all")[0].addEventListener("change", async event => { + if (event.target.checked) frontendDefaultRedirects = [...redirects[frontend][network]] + else frontendDefaultRedirects = [] - options[service][network].enabled = nameDefaultRedirects + options[frontend][network].enabled = frontendDefaultRedirects browser.storage.local.set({ options }) - calcNameCheckBoxes() + calcFrontendCheckBoxes() }) - for (let element of nameCheckListElement.getElementsByTagName("input")) { + for (let element of frontendCheckListElement.getElementsByTagName("input")) { if (element.className != "toggle-all") - nameNetworkElement.getElementsByClassName(element.className)[0].addEventListener("change", async event => { - if (event.service.checked) nameDefaultRedirects.push(element.className) + frontendNetworkElement.getElementsByClassName(element.className)[0].addEventListener("change", async event => { + if (event.target.checked) frontendDefaultRedirects.push(element.className) else { - let index = nameDefaultRedirects.indexOf(element.className) - if (index > -1) nameDefaultRedirects.splice(index, 1) + let index = frontendDefaultRedirects.indexOf(element.className) + if (index > -1) frontendDefaultRedirects.splice(index, 1) } - options[service][network].enabled = nameDefaultRedirects + options[frontend][network].enabled = frontendDefaultRedirects browser.storage.local.set({ options }) - calcNameCheckBoxes() + calcFrontendCheckBoxes() }) } - function calcNameCustomInstances() { - nameNetworkElement.getElementsByClassName("custom-checklist")[0].innerHTML = nameCustomInstances + function calcFrontendCustomInstances() { + frontendNetworkElement.getElementsByClassName("custom-checklist")[0].innerHTML = frontendCustomInstances .map( x => `
${x} @@ -172,30 +170,30 @@ async function processDefaultCustomInstances(service, name, network, document) { ) .join("\n") - for (const item of nameCustomInstances) { - nameNetworkElement.getElementsByClassName(`clear-${item}`)[0].addEventListener("click", async () => { - let index = nameCustomInstances.indexOf(item) - if (index > -1) nameCustomInstances.splice(index, 1) - options[service][network].custom = nameCustomInstances + for (const item of frontendCustomInstances) { + frontendNetworkElement.getElementsByClassName(`clear-${item}`)[0].addEventListener("click", async () => { + let index = frontendCustomInstances.indexOf(item) + if (index > -1) frontendCustomInstances.splice(index, 1) + options[frontend][network].custom = frontendCustomInstances browser.storage.local.set({ options }) - calcNameCustomInstances() + calcFrontendCustomInstances() }) } } - calcNameCustomInstances() - nameNetworkElement.getElementsByClassName("custom-instance-form")[0].addEventListener("submit", async event => { + calcFrontendCustomInstances() + frontendNetworkElement.getElementsByClassName("custom-instance-form")[0].addEventListener("submit", async event => { event.preventDefault() - let nameCustomInstanceInput = nameNetworkElement.getElementsByClassName("custom-instance")[0] - let url = new URL(nameCustomInstanceInput.value) + let frontendCustomInstanceInput = frontendNetworkElement.getElementsByClassName("custom-instance")[0] + let url = new URL(frontendCustomInstanceInput.value) let protocolHostVar = protocolHost(url) - if (nameCustomInstanceInput.validity.valid && !redirects[name][network].includes(protocolHostVar)) { - if (!nameCustomInstances.includes(protocolHostVar)) { - nameCustomInstances.push(protocolHostVar) - options[service][network].custom = nameCustomInstances + if (frontendCustomInstanceInput.validity.valid && !redirects[frontend][network].includes(protocolHostVar)) { + if (!frontendCustomInstances.includes(protocolHostVar)) { + frontendCustomInstances.push(protocolHostVar) + options[frontend][network].custom = frontendCustomInstances browser.storage.local.set({ options }) - nameCustomInstanceInput.value = "" + frontendCustomInstanceInput.value = "" } - calcNameCustomInstances() + calcFrontendCustomInstances() } }) } @@ -245,11 +243,12 @@ function pingOnce(href) { async function testLatency(element, instances, frontend) { return new Promise(async resolve => { let myList = {} - let latencyThreshold - let redirectsChecks = [] + let latencyThreshold, options + //let redirectsChecks = [] browser.storage.local.get(["options"], r => { latencyThreshold = r.options.latencyThreshold - redirectsChecks = r.options[frontend].clearnet.enabled + //redirectsChecks = r.options[frontend].clearnet.enabled + options = r.options }) for (const href of instances) await ping(href).then(time => { @@ -261,11 +260,9 @@ async function testLatency(element, instances, frontend) { else color = "red" if (time > latencyThreshold) { - redirectsChecks.splice(redirectsChecks.indexOf(href), 1) + options[frontend].clearnet.enabled.splice(options[frontend].clearnet.enabled.indexOf(href), 1) } - browser.storage.local.set({ [`${frontend}ClearnetRedirectsChecks`]: redirectsChecks }) - let text if (time == 5000) text = "5000ms+" else if (time > 5000) text = `ERROR: ${time - 5000}` @@ -273,6 +270,7 @@ async function testLatency(element, instances, frontend) { element.innerHTML = `${href}: ${text}` } }) + browser.storage.local.set({ options }) resolve(myList) }) } @@ -386,7 +384,7 @@ function unify() { return } - let result = await servicesHelper.unifyPreferences(url) + let result = await servicesHelper.unifyPreferences(url, currTab.id) resolve(result) } @@ -417,21 +415,21 @@ function switchInstance(test) { }) } -function latency(name, frontend, document, location) { +function latency(service, frontend, document, location) { let latencyElement = document.getElementById(`latency-${frontend}`) let latencyLabel = document.getElementById(`latency-${frontend}-label`) latencyElement.addEventListener("click", async () => { let reloadWindow = () => location.reload() latencyElement.addEventListener("click", reloadWindow) - let key = `${name} Redirects` - browser.storage.local.get(key, r => { - let redirects = r[key] + browser.storage.local.get(["redirects", "latency"], r => { + let redirects = r.redirects const oldHtml = latencyLabel.innerHTML latencyLabel.innerHTML = "..." testLatency(latencyLabel, redirects[frontend].clearnet, frontend).then(r => { - browser.storage.local.set({ [`${frontend}Latency`]: r }) + latency[frontend] = r + browser.storage.local.set({ latency }) latencyLabel.innerHTML = oldHtml - processDefaultCustomInstances(name, frontend, "clearnet", document) + processDefaultCustomInstances(service, frontend, "clearnet", document) latencyElement.removeEventListener("click", reloadWindow) }) }) diff --git a/src/config/config.json b/src/config/config.json index e2b511b9..b15c20d4 100644 --- a/src/config/config.json +++ b/src/config/config.json @@ -48,7 +48,8 @@ "sponsorblock", "theme", "volume", - "watchHistory" + "watchHistory", + "localSubscriptions" ] }, "name": "Piped", @@ -490,13 +491,13 @@ }, "lingva": { "preferences": { - "localstorage": ["isauto", "source", "target"] + "localstorage": ["isauto", "source", "target", "chakra-ui-color-mode"] }, - "name": "Lingva", + "name": "Lingva Translate", "instanceList": true } }, - "targets": ["^https?:\\/{2}translate\\.google(\\.[a-z]{2,3}){1,2}\\/"], + "targets": ["^https?:\\/{2}translate\\.google(\\.[a-z]{2,3}){1,2}\\/", "^https?:\\/{2}translate\\.libredirect\\.invalid"], "name": "Translate", "options": { "enabled": true, @@ -504,7 +505,7 @@ }, "imageType": "svgMono", "embeddable": false, - "url": "https://translate.google.com" + "url": "https://translate.libredirect.invalid" }, "maps": { "frontends": { @@ -518,7 +519,7 @@ "singleInstance": "https://www.openstreetmap.org" } }, - "targets": ["^https?:\\/{2}(((www|maps)\\.)?(google\\.).*(\\/maps)|maps\\.(google\\.).*)"], + "targets": ["^https?:\\/{2}maps\\.libredirect\\.invalid", "^https?:\\/{2}(((www|maps)\\.)?(google\\.).*(\\/maps)|maps\\.(google\\.).*)"], "name": "Maps", "options": { "enabled": true, @@ -526,16 +527,16 @@ }, "imageType": "svgMono", "embeddable": false, - "url": "https://maps.google.com" + "url": "https://maps.libredirect.invalid" }, - "sendTargets": { + "uploadFiles": { "frontends": { "send": { "name": "Send", "instanceList": "true" } }, - "targets": ["^https?:\\/{2}send\\.libredirect\\.invalid\\/?$", "^https?:\\/{2}send\\.firefox\\.com\\/?$", "^https?:\\/{2}sendfiles\\.online\\/?$"], + "targets": ["^https?:\\/{2}send\\.libredirect\\.invalid", "^https?:\\/{2}send\\.firefox\\.com\\/?$", "^https?:\\/{2}sendfiles\\.online\\/?$"], "name": "Send Files", "options": { "enabled": true }, "imageType": "svgMono", diff --git a/src/pages/background/background.js b/src/pages/background/background.js index e9bf691f..3a900609 100644 --- a/src/pages/background/background.js +++ b/src/pages/background/background.js @@ -6,20 +6,7 @@ import servicesHelper from "../../assets/javascripts/services.js" window.browser = window.browser || window.chrome -browser.runtime.onInstalled.addListener(details => { - function initDefaults() { - fetch("/instances/blacklist.json") - .then(response => response.text()) - .then(async data => { - browser.storage.local.clear(() => { - browser.storage.local.set({ blacklists: JSON.parse(data) }, () => { - generalHelper.initDefaults() - servicesHelper.initDefaults() - }) - }) - }) - } - +browser.runtime.onInstalled.addListener(async details => { // if (details.reason == 'install' || (details.reason == "update" && details.previousVersion != browser.runtime.getManifest().version)) { // if (details.reason == "update") // browser.storage.local.get(null, r => { @@ -38,12 +25,25 @@ browser.runtime.onInstalled.addListener(details => { case "update": switch (details.previousVersion) { case "2.2.1": - //do stuff + initDefaults() break } } }) +function initDefaults() { + browser.storage.local.clear(() => { + fetch("/instances/blacklist.json") + .then(response => response.text()) + .then(async data => { + browser.storage.local.set({ blacklists: JSON.parse(data) }, async () => { + await generalHelper.initDefaults() + await servicesHelper.initDefaults() + }) + }) + }) +} + let BYPASSTABs = [] browser.webRequest.onBeforeRequest.addListener( details => { diff --git a/src/pages/options/index.html b/src/pages/options/index.html index 6ef33723..71d04fd0 100644 --- a/src/pages/options/index.html +++ b/src/pages/options/index.html @@ -84,7 +84,7 @@ - Send Files
+ Send Files
@@ -318,9 +318,9 @@ - Send Files + Send Files
- +
@@ -2818,7 +2818,7 @@

Frontend

@@ -3134,14 +3134,14 @@
-
+
-

Send Files

+

Send Files


Enable

- +

diff --git a/src/pages/options/widgets/general.js b/src/pages/options/widgets/general.js index 23ed73ee..cd5cfb47 100644 --- a/src/pages/options/widgets/general.js +++ b/src/pages/options/widgets/general.js @@ -37,7 +37,6 @@ function setOption(option, multiChoice, event) { browser.storage.local.get("options", r => { let options = r.options if (multiChoice) { - console.log(event.target.options) options[option] = event.target.options[event.target.options.selectedIndex].value } else { options[option] = event.target.checked @@ -95,6 +94,7 @@ resetSettings.addEventListener("click", async () => { .then(response => response.text()) .then(async data => { browser.storage.local.set({ blacklists: JSON.parse(data) }, async () => { + await generalHelper.initDefaults() await servicesHelper.initDefaults() location.reload() }) diff --git a/src/pages/options/widgets/services.js b/src/pages/options/widgets/services.js index 873950f3..98e068a1 100644 --- a/src/pages/options/widgets/services.js +++ b/src/pages/options/widgets/services.js @@ -77,7 +77,7 @@ function changeNetworkSettings() { changeNetworkSettings() for (const service in config.services) { divs[service] = {} - divs[service][service] = document.getElementById(`${service}_page`) + //divs[service].page = document.getElementById(`${service}_page`) for (const option in config.services[service].options) { divs[service][option] = document.getElementById(`${service}-${option}`) diff --git a/src/pages/popup/popup.html b/src/pages/popup/popup.html index d52a34b6..56e4ca14 100644 --- a/src/pages/popup/popup.html +++ b/src/pages/popup/popup.html @@ -91,7 +91,7 @@

Search

-
+ -
+ -
+
-

Send Files

- +

Send Files

+
@@ -204,7 +204,7 @@

Search

-
+ -
+ -
+
-

Send Files

- +

Send Files

+
-- cgit 1.4.1 From c1af086c2756f401d8403b259e29f1354c5e001f Mon Sep 17 00:00:00 2001 From: Hygna Date: Mon, 3 Oct 2022 06:44:49 +0100 Subject: Finished options upgrading --- src/_locales/filter.py | 8 - src/assets/images/sendFiles-icon.svg | 3 + src/assets/images/uploadFiles-icon.svg | 3 - src/assets/javascripts/services.js | 277 +++++++++++++++++---------------- src/assets/javascripts/utils.js | 8 +- src/config/config.json | 5 +- src/instances/get_instances.py | 1 + src/pages/options/index.html | 17 +- src/pages/options/widgets/general.js | 25 ++- src/pages/popup/popup.html | 12 +- src/pages/popup/popup.js | 6 +- 11 files changed, 190 insertions(+), 175 deletions(-) create mode 100644 src/assets/images/sendFiles-icon.svg delete mode 100644 src/assets/images/uploadFiles-icon.svg (limited to 'src/pages/popup/popup.html') diff --git a/src/_locales/filter.py b/src/_locales/filter.py index 581bb36c..0d5fb44d 100644 --- a/src/_locales/filter.py +++ b/src/_locales/filter.py @@ -1,12 +1,4 @@ -import requests import json -from urllib.parse import urlparse -from bs4 import BeautifulSoup -import re -from colorama import Fore, Back, Style -from urllib.parse import urlparse -import socket -import subprocess ar_json = {} diff --git a/src/assets/images/sendFiles-icon.svg b/src/assets/images/sendFiles-icon.svg new file mode 100644 index 00000000..5557664e --- /dev/null +++ b/src/assets/images/sendFiles-icon.svg @@ -0,0 +1,3 @@ + + + diff --git a/src/assets/images/uploadFiles-icon.svg b/src/assets/images/uploadFiles-icon.svg deleted file mode 100644 index 5557664e..00000000 --- a/src/assets/images/uploadFiles-icon.svg +++ /dev/null @@ -1,3 +0,0 @@ - - - diff --git a/src/assets/javascripts/services.js b/src/assets/javascripts/services.js index 279c8c21..59316dfc 100644 --- a/src/assets/javascripts/services.js +++ b/src/assets/javascripts/services.js @@ -406,123 +406,115 @@ function computeService(url, returnFrontend) { } } } - // if (returnFrontend) return [null, null] - // else return null + resolve() }) }) }) } -async function switchInstance(url) { - fetch("/config/config.json") - .then(response => response.text()) - .then(configData => { - const config = JSON.parse(configData) - browser.storage.local.get(["redirects", "options"], r => { - const redirects = r.redirects - const options = r.options - const protocolHost = utils.protocolHost(url) - for (const service in config.services) { - if (!options[service].enabled) continue - if (!all(service, null, options, config, redirects).includes(protocolHost)) continue - - let instancesList = [...options[options[service].frontend][options.network].enabled, ...options[options[service].frontend][options.network].custom] - if (instancesList.length === 0 && options.networkFallback) instancesList = [...options[options[service].frontend].clearnet.enabled, ...options[options[service].frontend].clearnet.custom] - - let oldInstance - const i = instancesList.indexOf(protocolHost) - if (i > -1) { - oldInstance = instancesList[i] - instancesList.splice(i, 1) - } - if (instancesList.length === 0) return - const randomInstance = utils.getRandomInstance(instancesList) - const oldUrl = `${oldInstance}${url.pathname}${url.search}` - // This is to make instance switching work when the instance depends on the pathname, eg https://darmarit.org/searx - // Doesn't work because of .includes array method, not a top priotiry atm - return oldUrl.replace(oldInstance, randomInstance) - } - }) - }) +function switchInstance(url) { + return new Promise(async resolve => { + await init() + const protocolHost = utils.protocolHost(url) + for (const service in config.services) { + if (!all(service, null, options, config, redirects).includes(protocolHost)) continue + + let instancesList = [...options[options[service].frontend][options.network].enabled, ...options[options[service].frontend][options.network].custom] + if (instancesList.length === 0 && options.networkFallback) instancesList = [...options[options[service].frontend].clearnet.enabled, ...options[options[service].frontend].clearnet.custom] + + let oldInstance + const i = instancesList.indexOf(protocolHost) + if (i > -1) { + oldInstance = instancesList[i] + instancesList.splice(i, 1) + } + if (instancesList.length === 0) { + resolve() + return + } + const randomInstance = utils.getRandomInstance(instancesList) + const oldUrl = `${oldInstance}${url.pathname}${url.search}` + // This is to make instance switching work when the instance depends on the pathname, eg https://darmarit.org/searx + // Doesn't work because of .includes array method, not a top priotiry atm + resolve(oldUrl.replace(oldInstance, randomInstance)) + return + } + resolve() + }) } -async function reverse(url) { - fetch("/config/config.json") - .then(response => response.text()) - .then(configData => { - const config = JSON.parse(configData) - browser.storage.local.get(["redirects", "options"], r => { - const redirects = r.redirects - const options = r.options - let protocolHost = utils.protocolHost(url) - for (const service in config.services) { - if (!all(service, null, options, config, redirects).includes(protocolHost)) continue - - switch (service) { - case "instagram": - if (url.pathname.startsWith("/p")) return `https://instagram.com${url.pathname.replace("/p", "")}${url.search}` - if (url.pathname.startsWith("/u")) return `https://instagram.com${url.pathname.replace("/u", "")}${url.search}` - return config.services[service].url + url.pathname + url.search - case "youtube": - case "imdb": - case "imgur": - case "tiktok": - case "twitter": - case "reddit": - return config.services[service].url + url.pathname + url.search - default: - return - } - } - }) - }) +function reverse(url) { + return new Promise(async resolve => { + await init() + let protocolHost = utils.protocolHost(url) + for (const service in config.services) { + if (!all(service, null, options, config, redirects).includes(protocolHost)) continue + + switch (service) { + case "instagram": + if (url.pathname.startsWith("/p")) resolve(`https://instagram.com${url.pathname.replace("/p", "")}${url.search}`) + if (url.pathname.startsWith("/u")) resolve(`https://instagram.com${url.pathname.replace("/u", "")}${url.search}`) + resolve(config.services[service].url + url.pathname + url.search) + return + case "youtube": + case "imdb": + case "imgur": + case "tiktok": + case "twitter": + case "reddit": + resolve(config.services[service].url + url.pathname + url.search) + return + default: + resolve() + return + } + } + resolve() + }) } -async function unifyPreferences(url, tabId) { - fetch("/config/config.json") - .then(response => response.text()) - .then(configData => { - const config = JSON.parse(configData) - browser.storage.local.get(["options", "reidrects"], r => { - const redirects = r.redirects - const options = r.options - const protocolHost = utils.protocolHost(url) - for (const service in config.services) { - for (const frontend in config.services[service].frontends) { - if (all(service, frontend, options, config, redirects).includes(protocolHost)) { - let instancesList = [...options[frontend][options.network].enabled, ...options[frontend][options.network].custom] - if (options.networkFallback && options.network != "clearnet") instancesList.push(...options[frontend].clearnet.enabled, ...options[frontend].clearnet.custom) - - const frontend = config.services[service].frontends[frontend] - if ("cookies" in frontend.preferences) { - for (const cookie of frontend.preferences.cookies) { - utils.copyCookie(frontend, url, instancesList, cookie) - } - } - if ("localstorage" in frontend.preferences) { - browser.storage.local.set({ tmp: [frontend, frontend.preferences.localstorage] }) - browser.tabs.executeScript(tabId, { - file: "/assets/javascripts/get-localstorage.js", +function unifyPreferences(url, tabId) { + return new Promise(async resolve => { + await init() + const protocolHost = utils.protocolHost(url) + for (const service in config.services) { + for (const frontend in config.services[service].frontends) { + if (all(service, frontend, options, config, redirects).includes(protocolHost)) { + let instancesList = [...options[frontend][options.network].enabled, ...options[frontend][options.network].custom] + if (options.networkFallback && options.network != "clearnet") instancesList.push(...options[frontend].clearnet.enabled, ...options[frontend].clearnet.custom) + + const frontendObject = config.services[service].frontends[frontend] + if ("cookies" in frontendObject.preferences) { + for (const cookie of frontendObject.preferences.cookies) { + utils.copyCookie(frontendObject, url, instancesList, cookie) + } + } + if ("localstorage" in frontendObject.preferences) { + browser.storage.local.set({ tmp: [frontend, frontendObject.preferences.localstorage] }) + browser.tabs.executeScript(tabId, { + 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, { + file: "/assets/javascripts/set-localstorage.js", runAt: "document_start", }) - for (const instance of instancesList) - browser.tabs.create({ url: instance }, tab => - browser.tabs.executeScript(tab.id, { - file: "/assets/javascripts/set-localstorage.js", - runAt: "document_start", - }) - ) - } - if ("indexeddb" in frontend.preferences) { - } - if ("token" in frontend.preferences) { - } - return true - } + ) + } + /* + if ("indexeddb" in frontendObject.preferences) { } + if ("token" in frontendObject.preferences) { + } + */ + resolve(true) + return } - }) - }) + } + } + }) } async function setRedirects(redirects) { @@ -612,46 +604,67 @@ function upgradeOptions() { fetch("/config/config.json") .then(response => response.text()) .then(configData => { - browser.storage.local.get(["options", "exceptions", "theme", "popupFrontends", "autoRedirect", "firstPartyIsolate"], r => { + browser.storage.local.get(null, r => { let options = r.options let latency = {} const config = JSON.parse(configData) options.exceptions = r.exceptions if (r.theme != "DEFAULT") options.theme = r.theme options.popupServices = r.popupFrontends + let tmp = options.popupServices.indexOf("tikTok") + if (tmp > -1) { + options.popupServices.splice(tmp, 1) + options.popupServices.push("tiktok") + } + tmp = options.popupServices.indexOf("sendTarget") + if (tmp > -1) { + options.popupServices.splice(tmp, 1) + options.popupServices.push("sendFiles") + } options.firstPartyIsolate = r.firstPartyIsolate options.autoRedirect = r.autoRedirect + switch (r.onlyEmbeddedVideo) { + case "onlyNotEmbedded": + options.youtube.redirectType = "main_frame" + case "onlyEmbedded": + options.youtube.redirectType = "sub_frame" + case "both": + options.youtube.redirectType = "both" + } for (const service in config.services) { - browser.storage.local.get([`disable${utils.camelCase(service)}`, `${service}RedirectType`, `${service}Frontend`, `${service}Latency`, `${service}EmbedFrontend`], r => { - if (r) { - options[service].enabled = !r["disable" + utils.camelCase(service)] - if (r[service + "Frontend"]) { - if (r[service + "Frontend"] == "yatte") options[service].frontend = "yattee" - else options[service].frontend = r[service + "Frontend"] - } - if (r[service + "RedirectType"]) options[service].redirectType = r[service + "RedirectType"] - if (r[service + "EmbedFrontend"] && (service != "youtube" || r[service + "EmbedFrontend"] == "invidious" || "piped")) options[service].embedFrontend = r[service + "EmbedFrontend"] - for (const frontend in config.services[service].frontends) { - browser.local.storage.get(`${frontend}Latency`, r => { - if (r) latency[frontend] = r[frontend + "Latency"] - for (const network in config.networks) { - let protocol - if (network == "clearnet") protocol = "normal" - else protocol = network - browser.storage.local.get([`${frontend}${utils.camelCase(protocol)}RedirectsChecks`, `${frontend}${utils.camelCase(protocol)}CustomRedirects`], r => { - if (r) { - options[frontend][network].checks = r[frontend + utils.camelCase(protocol) + "RedirectsChecks"] - options[frontend][network].custom = r[frontend + utils.camelCase(protocol) + "CustomRedirects"] - } - }) - } - }) + let oldService + switch (service) { + case "tiktok": + oldService = "tikTok" + break + case "sendFiles": + oldService = "sendTarget" + break + default: + oldService = service + } + options[service].enabled = !r["disable" + utils.camelCase(oldService)] + if (r[oldService + "Frontend"]) { + if (r[oldService + "Frontend"] == "yatte") options[service].frontend = "yattee" + else options[service].frontend = r[oldService + "Frontend"] + } + if (r[oldService + "RedirectType"]) options[service].redirectType = r[oldService + "RedirectType"] + if (r[oldService + "EmbedFrontend"] && (service != "youtube" || r[oldService + "EmbedFrontend"] == "invidious" || r[oldService + "EmbedFrontend"] == "piped")) + options[service].embedFrontend = r[oldService + "EmbedFrontend"] + for (const frontend in config.services[service].frontends) { + if (r[frontend + "Latency"]) latency[frontend] = r[frontend + "Latency"] + for (const network in config.networks) { + let protocol + if (network == "clearnet") protocol = "normal" + else protocol = network + if (r[frontend + utils.camelCase(protocol) + "RedirectsChecks"]) { + options[frontend][network].enabled = r[frontend + utils.camelCase(protocol) + "RedirectsChecks"] + options[frontend][network].custom = r[frontend + utils.camelCase(protocol) + "CustomRedirects"] } } - }) + } } - browser.storage.local.set({ options, latency }) - resolve() + browser.storage.local.set({ options, latency }, () => resolve()) }) }) }) diff --git a/src/assets/javascripts/utils.js b/src/assets/javascripts/utils.js index 26fe11d6..056bbc00 100644 --- a/src/assets/javascripts/utils.js +++ b/src/assets/javascripts/utils.js @@ -342,7 +342,7 @@ function getPreferencesFromToken(frontend, targetUrl, urls, name, endpoint) { }) } -function copyRaw(test, copyRawElement, config) { +function copyRaw(test, copyRawElement) { return new Promise(resolve => { browser.tabs.query({ active: true, currentWindow: true }, async tabs => { let currTab = tabs[0] @@ -355,7 +355,7 @@ function copyRaw(test, copyRawElement, config) { return } - let newUrl = servicesHelper.reverse(url, config) + const newUrl = await servicesHelper.reverse(url) if (newUrl) { resolve(newUrl) @@ -387,9 +387,7 @@ function unify() { return } - const result = await servicesHelper.unifyPreferences(url, currTab.id) - - resolve(result) + resolve(await servicesHelper.unifyPreferences(url, currTab.id)) } }) }) diff --git a/src/config/config.json b/src/config/config.json index b15c20d4..6866453c 100644 --- a/src/config/config.json +++ b/src/config/config.json @@ -22,7 +22,8 @@ "frontends": { "invidious": { "preferences": { - "cookies": ["PREFS"] + "cookies": ["PREFS"], + "localstorage": ["dark_mode"] }, "name": "Invidious", "embeddable": true, @@ -529,7 +530,7 @@ "embeddable": false, "url": "https://maps.libredirect.invalid" }, - "uploadFiles": { + "sendFiles": { "frontends": { "send": { "name": "Send", diff --git a/src/instances/get_instances.py b/src/instances/get_instances.py index 06b547b3..9a53f927 100644 --- a/src/instances/get_instances.py +++ b/src/instances/get_instances.py @@ -431,6 +431,7 @@ def peertube(): def isValid(url): # This code is contributed by avanitrachhadiya2155 try: + url.encode('ascii') result = urlparse(url) return all([result.scheme, result.netloc]) except Exception: diff --git a/src/pages/options/index.html b/src/pages/options/index.html index 2549566c..5421bb81 100644 --- a/src/pages/options/index.html +++ b/src/pages/options/index.html @@ -84,7 +84,7 @@ - Send Files
+ Send Files
@@ -150,11 +150,6 @@
-
- +
@@ -3139,14 +3134,14 @@
-
+
-

Send Files

+

Send Files


Enable

- +

diff --git a/src/pages/options/widgets/general.js b/src/pages/options/widgets/general.js index 1a2f44c6..92632f23 100644 --- a/src/pages/options/widgets/general.js +++ b/src/pages/options/widgets/general.js @@ -49,16 +49,16 @@ function setOption(option, multiChoice, event) { let exportSettingsElement = document.getElementById("export-settings") function exportSettings() { - browser.storage.local.get(null, result => { - let resultString = JSON.stringify(result, null, " ") - exportSettingsElement.href = "data:application/json;base64," + btoa(encodeURI(resultString)) + browser.storage.local.get("options", result => { + result.options.version = browser.runtime.getManifest().version + let resultString = JSON.stringify(result.options, null, " ") + exportSettingsElement.href = "data:application/json;base64," + btoa(resultString) exportSettingsElement.download = "libredirect-settings.json" }) } exportSettings() document.getElementById("general_page").addEventListener("click", exportSettings) -document.getElementById("test").addEventListener("click", servicesHelper.upgradeOptions) let importSettingsElement = document.getElementById("import-settings") let importSettingsElementText = document.getElementById("import_settings_text") @@ -70,7 +70,22 @@ importSettingsElement.addEventListener("change", () => { reader.onload = async () => { const data = JSON.parse(reader.result) if ("theme" in data && "disableImgur" in data && "imgurRedirects" in data) { - browser.storage.local.clear(() => browser.storage.local.set({ ...data }, () => location.reload())) + browser.storage.local.clear(() => + browser.storage.local.set({ ...data }, () => { + fetch("/instances/blacklist.json") + .then(response => response.text()) + .then(async data => { + browser.storage.local.set({ blacklists: JSON.parse(data) }, async () => { + await generalHelper.initDefaults() + await servicesHelper.initDefaults() + await servicesHelper.upgradeOptions() + location.reload() + }) + }) + }) + ) + } else if ("version" in data) { + browser.storage.local.clear(() => browser.storage.local.set({ options: data }, () => location.reload())) } else { console.log("incompatible settings") importError() diff --git a/src/pages/popup/popup.html b/src/pages/popup/popup.html index 56e4ca14..b5076635 100644 --- a/src/pages/popup/popup.html +++ b/src/pages/popup/popup.html @@ -107,13 +107,13 @@

Maps

-
+
-

Send Files

- +

Send Files

+
@@ -220,13 +220,13 @@

Maps

-
+
-

Send Files

- +

Send Files

+
diff --git a/src/pages/popup/popup.js b/src/pages/popup/popup.js index 05dd70f7..ed1546c7 100644 --- a/src/pages/popup/popup.js +++ b/src/pages/popup/popup.js @@ -26,7 +26,7 @@ utils.switchInstance(true).then(r => { else document.getElementById("change_instance").addEventListener("click", () => utils.switchInstance(false)) }) -utils.copyRaw(true, null, config).then(r => { +utils.copyRaw(true).then(r => { if (!r) document.getElementById("copy_raw_div").style.display = "none" else { const copy_raw = document.getElementById("copy_raw") @@ -81,13 +81,13 @@ browser.storage.local.get("options", r => { let service = await serviceHelper.computeService(url, true) let frontend if (service) { - if (service[1]) { + if (service[0]) { frontend = service[1] service = service[0] } divs[service].current.classList.remove("hide") divs[service].all.classList.add("hide") - if (config.services[service].frontends[frontend].preferences && !config.services[service].frontends[frontend].preferences.token) { + if (frontend && config.services[service].frontends[frontend].preferences && !config.services[service].frontends[frontend].preferences.token) { const unify = document.getElementById("unify") const textElement = document.getElementById("unify").getElementsByTagName("h4")[0] unify.addEventListener("click", () => { -- cgit 1.4.1 From ebc1259ca4ade09e53af17ee86b67eb09f0f9c75 Mon Sep 17 00:00:00 2001 From: Hygna Date: Mon, 3 Oct 2022 19:49:12 +0100 Subject: Squashed many bugs --- src/assets/javascripts/services.js | 2 +- src/config/config.json | 2 +- src/instances/get_instances.py | 39 +++++++++++++++++++++-------- src/pages/options/index.html | 38 ++++++++++++++-------------- src/pages/options/widgets/general.ejs | 4 +-- src/pages/options/widgets/general.js | 47 +++++++++++++++++++---------------- src/pages/popup/popup.html | 4 +-- src/pages/stylesheets/styles.css | 13 ++++++++++ 8 files changed, 91 insertions(+), 58 deletions(-) (limited to 'src/pages/popup/popup.html') diff --git a/src/assets/javascripts/services.js b/src/assets/javascripts/services.js index 59316dfc..afd7906f 100644 --- a/src/assets/javascripts/services.js +++ b/src/assets/javascripts/services.js @@ -76,7 +76,7 @@ function redirect(url, type, initiator) { } if (Object.keys(config.services[service].frontends).length > 1) { - if (type == "sub_frame") frontend = options[service].embedFrontend + if (type == "sub_frame" && config.services[service].embeddable && !config.services[service].frontends[options[service].frontend].embeddable) frontend = options[service].embedFrontend else frontend = options[service].frontend } else frontend = Object.keys(config.services[service].frontends)[0] diff --git a/src/config/config.json b/src/config/config.json index 6866453c..09c95bbb 100644 --- a/src/config/config.json +++ b/src/config/config.json @@ -279,7 +279,7 @@ "options": { "enabled": false }, "imageType": "svg", "embeddable": false, - "url": "https://wikipedia.com" + "url": "https://wikipedia.org" }, "medium": { "frontends": { diff --git a/src/instances/get_instances.py b/src/instances/get_instances.py index 9a53f927..c789f551 100644 --- a/src/instances/get_instances.py +++ b/src/instances/get_instances.py @@ -25,16 +25,33 @@ with open('./src/config/config.json', 'rt') as tmp: def filterLastSlash(urlList): tmp = {} - for x in urlList: - tmp[x] = {} - for y in urlList[x]: - tmp[x][y] = [] - for z in urlList[x][y]: - if z.endswith('/'): - tmp[x][y].append(z[:-1]) - print(Fore.YELLOW + "Fixed " + Style.RESET_ALL + z) + for frontend in urlList: + tmp[frontend] = {} + for network in urlList[frontend]: + tmp[frontend][network] = [] + for url in urlList[frontend][network]: + if url.endswith('/'): + tmp[frontend][network].append(url[:-1]) + print(Fore.YELLOW + "Fixed " + Style.RESET_ALL + url) else: - tmp[x][y].append(z) + tmp[frontend][network].append(url) + return tmp + + +def idnaEncode(urlList): + tmp = {} + for frontend in urlList: + tmp[frontend] = {} + for network in urlList[frontend]: + tmp[frontend][network] = [] + for url in urlList[frontend][network]: + try: + encodedUrl = url.encode("idna").decode("utf8") + tmp[frontend][network].append(encodedUrl) + if (encodedUrl != url): + print(Fore.YELLOW + "Fixed " + Style.RESET_ALL + url) + except Exception: + tmp[frontend][network].append(url) return tmp @@ -247,7 +264,7 @@ def piped(): _list['loki'] = [] r = requests.get( 'https://raw.githubusercontent.com/wiki/TeamPiped/Piped/Instances.md') - + tmp = re.findall( r'(?:[^\s\/]+\.)+[a-zA-Z]+ (?:\(Official\) )?\| (https:\/{2}(?:[^\s\/]+\.)+[a-zA-Z]+) \| ', r.text) for item in tmp: @@ -431,7 +448,6 @@ def peertube(): def isValid(url): # This code is contributed by avanitrachhadiya2155 try: - url.encode('ascii') result = urlparse(url) return all([result.scheme, result.netloc]) except Exception: @@ -465,6 +481,7 @@ hyperpipe() facil() simpleertube() mightyList = filterLastSlash(mightyList) +mightyList = idnaEncode(mightyList) cloudflare = [] authenticate = [] diff --git a/src/pages/options/index.html b/src/pages/options/index.html index 5421bb81..d79da014 100644 --- a/src/pages/options/index.html +++ b/src/pages/options/index.html @@ -116,7 +116,7 @@
-

Fallback to normal if no instances are available for the current network

+

Fallback to clearnet if no instances are available for the current network

@@ -182,56 +182,56 @@
- Youtube + Youtube
- YT Music + YT Music
- Twitter + Twitter
- Instagram + Instagram
- TikTok + TikTok
- Reddit + Reddit
- Imgur + Imgur
- Wikipedia + Wikipedia
@@ -243,42 +243,42 @@ - Medium + Medium
- Quora + Quora
- IMDb + IMDb
- Reuters + Reuters
- PeerTube + PeerTube
- LBRY + LBRY
@@ -288,7 +288,7 @@ - Search + Search @@ -298,7 +298,7 @@ - Translate + Translate @@ -308,7 +308,7 @@ - Maps + Maps @@ -318,7 +318,7 @@ - Send Files + Send Files diff --git a/src/pages/options/widgets/general.ejs b/src/pages/options/widgets/general.ejs index 8f7ab029..77028ac2 100644 --- a/src/pages/options/widgets/general.ejs +++ b/src/pages/options/widgets/general.ejs @@ -21,7 +21,7 @@
-

Fallback to normal if no instances are available for the current network

+

Fallback to clearnet if no instances are available for the current network

@@ -92,7 +92,7 @@ <% } else { _%> <%- include ('src/assets/images/' + service + '-icon.svg') %> <% } _%> - <%= config.services[service].name %> + <%= config.services[service].name %> diff --git a/src/pages/options/widgets/general.js b/src/pages/options/widgets/general.js index 92632f23..a58ca3e6 100644 --- a/src/pages/options/widgets/general.js +++ b/src/pages/options/widgets/general.js @@ -28,7 +28,7 @@ async function getConfig() { }) } -function setOption(option, multiChoice, event) { +function setOption(option, type, event) { browser.storage.local.get("options", r => { let options = r.options browser.storage.local.set({ options }) @@ -36,13 +36,15 @@ function setOption(option, multiChoice, event) { browser.storage.local.get("options", r => { let options = r.options - if (multiChoice) { + if (type == "select") { options[option] = event.target.options[event.target.options.selectedIndex].value - } else { + } else if (type == "checkbox") { options[option] = event.target.checked + } else if (type == "range") { + options[option] = event.target.value } + browser.storage.local.set({ options }) - location.reload() }) } @@ -120,30 +122,30 @@ resetSettings.addEventListener("click", async () => { let autoRedirectElement = document.getElementById("auto-redirect") autoRedirectElement.addEventListener("change", event => { - setOption("autoRedirect", false, event) + setOption("autoRedirect", "checkbox", event) }) let themeElement = document.getElementById("theme") themeElement.addEventListener("change", event => { - setOption("theme", true, event) + setOption("theme", "select", event) location.reload() }) let networkElement = document.getElementById("network") networkElement.addEventListener("change", event => { - setOption("network", true, event) + setOption("network", "select", event) location.reload() }) let networkFallbackCheckbox = document.getElementById("network-fallback-checkbox") networkFallbackCheckbox.addEventListener("change", event => { - setOption("networkFallback", false, event) + setOption("networkFallback", "checkbox", event) }) let latencyOutput = document.getElementById("latency-output") let latencyInput = document.getElementById("latency-input") latencyInput.addEventListener("change", event => { - setOption("latencyThreshold", false, event) + setOption("latencyThreshold", "range", event) }) latencyInput.addEventListener("input", event => { latencyOutput.value = event.target.value @@ -153,18 +155,19 @@ let nameCustomInstanceInput = document.getElementById("exceptions-custom-instanc let instanceTypeElement = document.getElementById("exceptions-custom-instance-type") let instanceType = "url" -let popupServices - await getConfig() for (const service in config.services) { document.getElementById(service).addEventListener("change", event => { - if (event.target.checked && !popupServices.includes(service)) popupServices.push(service) - else if (popupServices.includes(service)) { - var index = popupServices.indexOf(service) - if (index !== -1) popupServices.splice(index, 1) - } - browser.storage.local.set({ popupServices }) + browser.storage.local.get("options", r => { + let options = r.options + if (event.target.checked && !options.popupServices.includes(service)) options.popupServices.push(service) + else if (options.popupServices.includes(service)) { + var index = options.popupServices.indexOf(service) + if (index !== -1) options.popupServices.splice(index, 1) + } + browser.storage.local.set({ options }) + }) }) } // const firstPartyIsolate = document.getElementById('firstPartyIsolate'); @@ -176,13 +179,14 @@ browser.storage.local.get("options", r => { networkElement.value = r.options.network networkFallbackCheckbox.checked = r.options.networkFallback latencyOutput.value = r.options.latencyThreshold + let options = r.options // firstPartyIsolate.checked = r.firstPartyIsolate; - let networkFallbackElement = document.getElementById("network-fallback") + //let networkFallbackElement = document.getElementById("network-fallback") if (networkElement.value == "clearnet") { - networkFallbackElement.style.display = "none" + networkFallbackCheckbox.disabled = true } else { - networkFallbackElement.style.display = "block" + networkFallbackCheckbox.disabled = false } instanceTypeElement.addEventListener("change", event => { @@ -251,6 +255,5 @@ browser.storage.local.get("options", r => { calcExceptionsCustomInstances() }) - popupServices = r.options.popupServices - for (const service in config.services) document.getElementById(service).checked = popupServices.includes(service) + for (const service in config.services) document.getElementById(service).checked = options.popupServices.includes(service) }) diff --git a/src/pages/popup/popup.html b/src/pages/popup/popup.html index b5076635..54c4fefa 100644 --- a/src/pages/popup/popup.html +++ b/src/pages/popup/popup.html @@ -43,7 +43,7 @@

Imgur

-
+

Wikipedia

@@ -156,7 +156,7 @@

Imgur

-
+

Wikipedia

diff --git a/src/pages/stylesheets/styles.css b/src/pages/stylesheets/styles.css index 06abab3a..eb599656 100644 --- a/src/pages/stylesheets/styles.css +++ b/src/pages/stylesheets/styles.css @@ -80,11 +80,13 @@ select { margin: 0; max-width: 500px; border-radius: 3px; + cursor: pointer; } input[type="url"], input[type="text"] { width: 400px; + cursor: text; } input:invalid { @@ -141,6 +143,7 @@ input[type="range"] { height: 7px; border-radius: 50px; background: var(--text); + cursor: ew-resize; } input[type="range"]:hover { @@ -464,3 +467,13 @@ div.about > div { div.about h4 { width: auto; } + +select:disabled { + opacity: 0.6; + cursor: not-allowed; +} + +input:disabled { + opacity: 0.6; + cursor: not-allowed; +} -- cgit 1.4.1