From 9d750897ec4b36766ca876549c0af31746f9c05b Mon Sep 17 00:00:00 2001 From: ManeraKai Date: Mon, 20 May 2024 13:57:31 +0300 Subject: Cleaning code --- src/pages/options/index.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'src/pages/options/index.js') diff --git a/src/pages/options/index.js b/src/pages/options/index.js index fcc51298..ec47f3c5 100644 --- a/src/pages/options/index.js +++ b/src/pages/options/index.js @@ -260,7 +260,7 @@ async function processCustomInstances(frontend, document) { /** * @param {string} frontend * @param {*} networks - * @param {*} document + * @param {Document} document * @param {*} redirects * @param {*} blacklist */ @@ -393,7 +393,5 @@ function processTime(time) { color = "red" text = 'Server not found' } - return { - color, text - } + return { color, text } } -- cgit 1.4.1 From 73f85a005eeb28e999839e90077874defc16d00b Mon Sep 17 00:00:00 2001 From: ManeraKai Date: Mon, 20 May 2024 22:55:33 +0300 Subject: Added custom label to instances not from the public list https://github.com/libredirect/browser_extension/issues/907. Cleaned code --- src/pages/options/index.js | 59 +++++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 24 deletions(-) (limited to 'src/pages/options/index.js') diff --git a/src/pages/options/index.js b/src/pages/options/index.js index ec47f3c5..9274023f 100644 --- a/src/pages/options/index.js +++ b/src/pages/options/index.js @@ -2,6 +2,8 @@ import utils from "../../assets/javascripts/utils.js" let config, options, + blacklist, + redirects, divs = {} for (const a of document.getElementById("links").getElementsByTagName("a")) { @@ -148,20 +150,11 @@ async function loadPage(path) { changeFrontendsSettings(service) - for (const frontend in config.services[service].frontends) { - if (config.services[service].frontends[frontend].instanceList) { - processCustomInstances(frontend, document) - document.getElementById(`ping-${frontend}`).addEventListener("click", async () => { - document.getElementById(`ping-${frontend}`).getElementsByTagName('x')[0].innerHTML = "Pinging..." - await ping(frontend) - document.getElementById(`ping-${frontend}`).getElementsByTagName('x')[0].innerHTML = "Ping instances" - }) - } - } + !async function () { - const blacklist = await utils.getBlacklist(options) - const redirects = await utils.getList(options) + blacklist = await utils.getBlacklist(options) + redirects = await utils.getList(options) for (const frontend in config.services[service].frontends) { if (config.services[service].frontends[frontend].instanceList) { @@ -177,10 +170,21 @@ async function loadPage(path) { .innerHTML = 'Could not fetch instances.' } else { - createList(frontend, config.networks, document, redirects, blacklist) + createList(frontend) } } } + + for (const frontend in config.services[service].frontends) { + if (config.services[service].frontends[frontend].instanceList) { + processCustomInstances(frontend) + document.getElementById(`ping-${frontend}`).addEventListener("click", async () => { + document.getElementById(`ping-${frontend}`).getElementsByTagName('x')[0].innerHTML = "Pinging..." + await ping(frontend) + document.getElementById(`ping-${frontend}`).getElementsByTagName('x')[0].innerHTML = "Ping instances" + }) + } + } }() } } @@ -199,10 +203,12 @@ async function calcCustomInstances(frontend) { const { color, text } = processTime(time) timeText = `${text}` } + const custom = isCustomInstance(frontend, x) ? "" : `custom` return `
${x} ${timeText} + ${custom}

` - }) - .join("\n") - for (const item of customInstances) { - document.getElementById(frontend).getElementsByClassName(`clear-${item}`)[0].addEventListener("click", async () => { - const index = customInstances.indexOf(item) - if (index > -1) customInstances.splice(index, 1) - options = await utils.getOptions() - options[frontend] = customInstances - browser.storage.local.set({ options }, async () => { - calcCustomInstances(frontend) - createList(frontend) - }) - }) - } + }) + .join("\n") + for (const item of customInstances) { + document + .getElementById(frontend) + .getElementsByClassName(`clear-${item}`)[0] + .addEventListener("click", async () => { + const index = customInstances.indexOf(item) + if (index > -1) customInstances.splice(index, 1) + options = await utils.getOptions() + options[frontend] = customInstances + browser.storage.local.set({ options }, async () => { + calcCustomInstances(frontend) + createList(frontend) + }) + }) + } } async function processCustomInstances(frontend) { - calcCustomInstances(frontend) - document.getElementById(frontend).getElementsByClassName("custom-instance-form")[0].addEventListener("submit", async event => { - event.preventDefault() - let options = await utils.getOptions() - let customInstances = options[frontend] - let frontendCustomInstanceInput = document.getElementById(frontend).getElementsByClassName("custom-instance")[0] - try { - var url = new URL(frontendCustomInstanceInput.value) - } catch (error) { - return - } - let protocolHostVar = utils.protocolHost(url) - if (frontendCustomInstanceInput.validity.valid) { - if (!customInstances.includes(protocolHostVar)) { - customInstances.push(protocolHostVar) - options = await utils.getOptions() - options[frontend] = customInstances - browser.storage.local.set({ options }, () => { - frontendCustomInstanceInput.value = "" - calcCustomInstances(frontend) - }) - } - } - }) + calcCustomInstances(frontend) + document + .getElementById(frontend) + .getElementsByClassName("custom-instance-form")[0] + .addEventListener("submit", async event => { + event.preventDefault() + let options = await utils.getOptions() + let customInstances = options[frontend] + let frontendCustomInstanceInput = document.getElementById(frontend).getElementsByClassName("custom-instance")[0] + try { + var url = new URL(frontendCustomInstanceInput.value) + } catch (error) { + return + } + let protocolHostVar = utils.protocolHost(url) + if (frontendCustomInstanceInput.validity.valid) { + if (!customInstances.includes(protocolHostVar)) { + customInstances.push(protocolHostVar) + options = await utils.getOptions() + options[frontend] = customInstances + browser.storage.local.set({ options }, () => { + frontendCustomInstanceInput.value = "" + calcCustomInstances(frontend) + }) + } + } + }) } /** * @param {string} frontend */ async function createList(frontend) { - const pingCache = await utils.getPingCache() - const options = await utils.getOptions() - for (const network in config.networks) { - const checklist = document.getElementById(frontend).getElementsByClassName(network)[0].getElementsByClassName("checklist")[0] - - if (!redirects[frontend]) { - checklist.innerHTML = '
No instances found.
' - break - } - - const instances = redirects[frontend][network] - if (!instances || instances.length === 0) continue - - document.getElementById(frontend).getElementsByClassName("custom-instance")[0].placeholder = redirects[frontend].clearnet[0] - - instances.sort((a, b) => blacklist.cloudflare.includes(a) && !blacklist.cloudflare.includes(b)) - const content = instances - .map(x => { - const cloudflare = blacklist.cloudflare.includes(x) ? - ` + const pingCache = await utils.getPingCache() + const options = await utils.getOptions() + for (const network in config.networks) { + const checklist = document + .getElementById(frontend) + .getElementsByClassName(network)[0] + .getElementsByClassName("checklist")[0] + + if (!redirects[frontend]) { + checklist.innerHTML = '
No instances found.
' + break + } + + const instances = redirects[frontend][network] + if (!instances || instances.length === 0) continue + + document.getElementById(frontend).getElementsByClassName("custom-instance")[0].placeholder = + redirects[frontend].clearnet[0] + + instances.sort((a, b) => blacklist.cloudflare.includes(a) && !blacklist.cloudflare.includes(b)) + const content = instances.map(x => { + const cloudflare = blacklist.cloudflare.includes(x) + ? `
cloudflare - ` : "" + ` + : "" - let time = pingCache[x] - let timeText = "" - if (time) { - const { color, text } = processTime(time) - timeText = `${text}` - } + let time = pingCache[x] + let timeText = "" + if (time) { + const { color, text } = processTime(time) + timeText = `${text}` + } - const chosen = options[frontend].includes(x) ? `chosen` : "" + const chosen = options[frontend].includes(x) ? `chosen` : "" - const warnings = [cloudflare, timeText, chosen].join(" ") - return `
+ const warnings = [cloudflare, timeText, chosen].join(" ") + return `
${x} ${warnings} @@ -300,30 +310,29 @@ async function createList(frontend) {
` - }) + }) - checklist.innerHTML = [ - `
+ checklist.innerHTML = [ + `
`, - ...content, - "
" - ].join("\n
\n") - - for (const instance of instances) { - checklist.getElementsByClassName(`add-${instance}`)[0] - .addEventListener("click", async () => { - let options = await utils.getOptions() - if (!options[frontend].includes(instance)) { - options[frontend].push(instance) - browser.storage.local.set({ options }, () => { - calcCustomInstances(frontend) - createList(frontend) - }) - } - }) - } - } + ...content, + "
", + ].join("\n
\n") + + for (const instance of instances) { + checklist.getElementsByClassName(`add-${instance}`)[0].addEventListener("click", async () => { + let options = await utils.getOptions() + if (!options[frontend].includes(instance)) { + options[frontend].push(instance) + browser.storage.local.set({ options }, () => { + calcCustomInstances(frontend) + createList(frontend) + }) + } + }) + } + } } const r = window.location.href.match(/#(.*)/) @@ -334,59 +343,57 @@ else loadPage("general") * @param {string} frontend */ async function ping(frontend) { - const instanceElements = [ - ...document.getElementById(frontend).getElementsByClassName("custom-checklist")[0].getElementsByTagName('x'), - ...document.getElementById(frontend).getElementsByClassName('clearnet')[0].getElementsByTagName('x') - ] - - let pingCache = await utils.getPingCache() - let redundancyList = {} - for (const element of instanceElements) { - let span = element.getElementsByClassName('ping')[0] - if (!span) span = document.createElement('span') - span.classList = ['ping'] - span.innerHTML = 'pinging...' - element.appendChild(span) - const href = element.getElementsByTagName('a')[0].href - const innerHTML = element.getElementsByTagName('a')[0].innerHTML - const time = redundancyList[innerHTML] ?? await utils.ping(href) - const { color, text } = processTime(time) - span.innerHTML = `${text}` - pingCache[innerHTML] = time - redundancyList[innerHTML] = time - - browser.storage.local.set({ pingCache }) - } + const instanceElements = [ + ...document.getElementById(frontend).getElementsByClassName("custom-checklist")[0].getElementsByTagName("x"), + ...document.getElementById(frontend).getElementsByClassName("clearnet")[0].getElementsByTagName("x"), + ] + + let pingCache = await utils.getPingCache() + let redundancyList = {} + for (const element of instanceElements) { + let span = element.getElementsByClassName("ping")[0] + if (!span) span = document.createElement("span") + span.classList = ["ping"] + span.innerHTML = 'pinging...' + element.appendChild(span) + const href = element.getElementsByTagName("a")[0].href + const innerHTML = element.getElementsByTagName("a")[0].innerHTML + const time = redundancyList[innerHTML] ?? (await utils.ping(href)) + const { color, text } = processTime(time) + span.innerHTML = `${text}` + pingCache[innerHTML] = time + redundancyList[innerHTML] = time + + browser.storage.local.set({ pingCache }) + } } /** * @param {number} time */ function processTime(time) { - let text - let color - if (time < 5000) { - text = `${time}ms` - if (time <= 1000) color = "green" - else if (time <= 2000) color = "orange" - } - else if (time >= 5000) { - color = "red" - if (time == 5000) text = "5000ms+" - if (time > 5000) text = `Error: ${time - 5000}` - } - else { - color = "red" - text = 'Server not found' - } - return { color, text } + let text + let color + if (time < 5000) { + text = `${time}ms` + if (time <= 1000) color = "green" + else if (time <= 2000) color = "orange" + } else if (time >= 5000) { + color = "red" + if (time == 5000) text = "5000ms+" + if (time > 5000) text = `Error: ${time - 5000}` + } else { + color = "red" + text = "Server not found" + } + return { color, text } } function isCustomInstance(frontend, instance) { - for (const network in config.networks) { - if (!redirects[frontend]) return false; - const instances = redirects[frontend][network] - if (instances.includes(instance)) return true - } - return false -} \ No newline at end of file + for (const network in config.networks) { + if (!redirects[frontend]) return false + const instances = redirects[frontend][network] + if (instances.includes(instance)) return true + } + return false +} diff --git a/src/pages/options/init.js b/src/pages/options/init.js index baf3eebe..07da1859 100644 --- a/src/pages/options/init.js +++ b/src/pages/options/init.js @@ -5,46 +5,46 @@ import utils from "../../assets/javascripts/utils.js" import servicesHelper from "../../assets/javascripts/services.js" if (!(await utils.getOptions())) { - await servicesHelper.initDefaults() + await servicesHelper.initDefaults() } async function changeTheme() { - switch ((await utils.getOptions()).theme) { - case "dark": - document.body.classList.add("dark-theme") - document.body.classList.remove("light-theme") - for (const element of document.body.getElementsByClassName('dark')) { - element.style.display = 'none'; - } - break - case "light": - document.body.classList.add("light-theme") - document.body.classList.remove("dark-theme") - for (const element of document.body.getElementsByClassName('light')) { - element.style.display = 'none'; - } - break - default: - if (matchMedia("(prefers-color-scheme: light)").matches) { - document.body.classList.add("light-theme") - document.body.classList.remove("dark-theme") - for (const element of document.body.getElementsByClassName('light')) { - element.style.display = 'none'; - } - } else { - document.body.classList.add("dark-theme") - document.body.classList.remove("light-theme") - for (const element of document.body.getElementsByClassName('dark')) { - element.style.display = 'none'; - } - } - } + switch ((await utils.getOptions()).theme) { + case "dark": + document.body.classList.add("dark-theme") + document.body.classList.remove("light-theme") + for (const element of document.body.getElementsByClassName("dark")) { + element.style.display = "none" + } + break + case "light": + document.body.classList.add("light-theme") + document.body.classList.remove("dark-theme") + for (const element of document.body.getElementsByClassName("light")) { + element.style.display = "none" + } + break + default: + if (matchMedia("(prefers-color-scheme: light)").matches) { + document.body.classList.add("light-theme") + document.body.classList.remove("dark-theme") + for (const element of document.body.getElementsByClassName("light")) { + element.style.display = "none" + } + } else { + document.body.classList.add("dark-theme") + document.body.classList.remove("light-theme") + for (const element of document.body.getElementsByClassName("dark")) { + element.style.display = "none" + } + } + } } changeTheme() if (["ar", "iw", "ku", "fa", "ur"].includes(browser.i18n.getUILanguage())) { - document.getElementsByTagName("body")[0].classList.add("rtl") - document.getElementsByTagName("body")[0].dir = "rtl" + document.getElementsByTagName("body")[0].classList.add("rtl") + document.getElementsByTagName("body")[0].dir = "rtl" } localise.localisePage() diff --git a/src/pages/options/widgets/general.js b/src/pages/options/widgets/general.js index 30a8a0c8..b9ddfd08 100644 --- a/src/pages/options/widgets/general.js +++ b/src/pages/options/widgets/general.js @@ -7,29 +7,29 @@ import servicesHelper from "../../../assets/javascripts/services.js" const isChrome = browser.runtime.getBrowserInfo === undefined async function setOption(option, type, event) { - let options = await utils.getOptions() - switch (type) { - case "select": - options[option] = event.target.options[event.target.options.selectedIndex].value - break; - case "checkbox": - options[option] = event.target.checked - break; - case "range": - options[option] = event.target.value - break; - } - browser.storage.local.set({ options }) + let options = await utils.getOptions() + switch (type) { + case "select": + options[option] = event.target.options[event.target.options.selectedIndex].value + break + case "checkbox": + options[option] = event.target.checked + break + case "range": + options[option] = event.target.value + break + } + browser.storage.local.set({ options }) } const exportSettingsElement = document.getElementById("export-settings") async function exportSettings() { - const options = await utils.getOptions() - options.version = browser.runtime.getManifest().version - let resultString = JSON.stringify(options, null, " ") - exportSettingsElement.href = "data:application/json;base64," + btoa(resultString) - exportSettingsElement.download = `libredirect-settings-v${options.version}.json` - return + const options = await utils.getOptions() + options.version = browser.runtime.getManifest().version + let resultString = JSON.stringify(options, null, " ") + exportSettingsElement.href = "data:application/json;base64," + btoa(resultString) + exportSettingsElement.download = `libredirect-settings-v${options.version}.json` + return } exportSettings() document.getElementById("general_page").onclick = exportSettings @@ -37,35 +37,32 @@ document.getElementById("general_page").onclick = exportSettings const importSettingsElement = document.getElementById("import-settings") const importSettingsElementText = document.getElementById("import_settings_text") importSettingsElement.addEventListener("change", () => { - function importError() { - const oldHTML = importSettingsElementText.innerHTML - importSettingsElementText.innerHTML = 'Error!' - setTimeout(() => (importSettingsElementText.innerHTML = oldHTML), 1000) - } - importSettingsElementText.innerHTML = "..." - let file = importSettingsElement.files[0] - const reader = new FileReader() - reader.readAsText(file) - reader.onload = async () => { - const data = JSON.parse(reader.result) - if ( - "theme" in data - && data.version == browser.runtime.getManifest().version - ) { - browser.storage.local.clear(async () => { - browser.storage.local.set({ options: data }, () => { - location.reload() - }) - }) - } else { - console.log("incompatible settings") - importError() - } - } - reader.onerror = error => { - console.log("error", error) - importError() - } + function importError() { + const oldHTML = importSettingsElementText.innerHTML + importSettingsElementText.innerHTML = 'Error!' + setTimeout(() => (importSettingsElementText.innerHTML = oldHTML), 1000) + } + importSettingsElementText.innerHTML = "..." + let file = importSettingsElement.files[0] + const reader = new FileReader() + reader.readAsText(file) + reader.onload = async () => { + const data = JSON.parse(reader.result) + if ("theme" in data && data.version == browser.runtime.getManifest().version) { + browser.storage.local.clear(async () => { + browser.storage.local.set({ options: data }, () => { + location.reload() + }) + }) + } else { + console.log("incompatible settings") + importError() + } + } + reader.onerror = error => { + console.log("error", error) + importError() + } }) const exportSettingsSync = document.getElementById("export-settings-sync") @@ -73,58 +70,57 @@ const importSettingsSync = document.getElementById("import-settings-sync") const importSettingsSyncText = document.getElementById("import_settings_sync_text") exportSettingsSync.addEventListener("click", async () => { - let options = await utils.getOptions() - options.version = browser.runtime.getManifest().version - browser.storage.sync.set({ options }, () => location.reload()) + let options = await utils.getOptions() + options.version = browser.runtime.getManifest().version + browser.storage.sync.set({ options }, () => location.reload()) }) importSettingsSync.addEventListener("click", () => { - function importError() { - importSettingsSyncText.innerHTML = 'Error!' - setTimeout(() => (importSettingsSyncText.innerHTML = oldHTML), 1000) - } - const oldHTML = importSettingsSyncText.innerHTML - importSettingsSyncText.innerHTML = "..." - browser.storage.sync.get({ options }, r => { - const options = r.options - if (options.version == browser.runtime.getManifest().version) { - browser.storage.local.set({ options }, () => location.reload()) - } else { - importError() - } - }) + function importError() { + importSettingsSyncText.innerHTML = 'Error!' + setTimeout(() => (importSettingsSyncText.innerHTML = oldHTML), 1000) + } + const oldHTML = importSettingsSyncText.innerHTML + importSettingsSyncText.innerHTML = "..." + browser.storage.sync.get({ options }, r => { + const options = r.options + if (options.version == browser.runtime.getManifest().version) { + browser.storage.local.set({ options }, () => location.reload()) + } else { + importError() + } + }) }) const resetSettings = document.getElementById("reset-settings") resetSettings.addEventListener("click", async () => { - resetSettings.innerHTML = "..." - await servicesHelper.initDefaults() - location.reload() + resetSettings.innerHTML = "..." + await servicesHelper.initDefaults() + location.reload() }) -const fetchInstancesElement = document.getElementById('fetch-instances') -fetchInstancesElement.addEventListener('change', event => { - setOption('fetchInstances', 'select', event) - location.reload() +const fetchInstancesElement = document.getElementById("fetch-instances") +fetchInstancesElement.addEventListener("change", event => { + setOption("fetchInstances", "select", event) + location.reload() }) -const redirectOnlyInIncognitoElement = document.getElementById('redirectOnlyInIncognito') -redirectOnlyInIncognitoElement.addEventListener('change', event => { - setOption('redirectOnlyInIncognito', 'checkbox', event) +const redirectOnlyInIncognitoElement = document.getElementById("redirectOnlyInIncognito") +redirectOnlyInIncognitoElement.addEventListener("change", event => { + setOption("redirectOnlyInIncognito", "checkbox", event) }) -const bookmarksMenuElement = document.getElementById('bookmarksMenu') -bookmarksMenuElement.addEventListener('change', async event => { - if (event.target.checked) - browser.permissions.request({ permissions: ["bookmarks"] }, r => bookmarksMenuElement.checked = r) - else - browser.permissions.remove({ permissions: ["bookmarks"] }, r => bookmarksMenuElement.checked = !r) +const bookmarksMenuElement = document.getElementById("bookmarksMenu") +bookmarksMenuElement.addEventListener("change", async event => { + if (event.target.checked) + browser.permissions.request({ permissions: ["bookmarks"] }, r => (bookmarksMenuElement.checked = r)) + else browser.permissions.remove({ permissions: ["bookmarks"] }, r => (bookmarksMenuElement.checked = !r)) }) let themeElement = document.getElementById("theme") themeElement.addEventListener("change", event => { - setOption("theme", "select", event) - location.reload() + setOption("theme", "select", event) + location.reload() }) let nameCustomInstanceInput = document.getElementById("exceptions-custom-instance") @@ -134,40 +130,44 @@ let instanceType = "url" let config = await utils.getConfig() for (const service in config.services) { - document.getElementById(service).addEventListener("change", async event => { - let options = await utils.getOptions() - 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 }) - }) + document.getElementById(service).addEventListener("change", async event => { + let options = await utils.getOptions() + 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 }) + }) } let options = await utils.getOptions() themeElement.value = options.theme fetchInstancesElement.value = options.fetchInstances redirectOnlyInIncognitoElement.checked = options.redirectOnlyInIncognito -browser.permissions.contains({ permissions: ["bookmarks"] }, r => bookmarksMenuElement.checked = r) -for (const service in config.services) document.getElementById(service).checked = options.popupServices.includes(service) +browser.permissions.contains({ permissions: ["bookmarks"] }, r => (bookmarksMenuElement.checked = r)) +for (const service in config.services) + document.getElementById(service).checked = options.popupServices.includes(service) instanceTypeElement.addEventListener("change", event => { - instanceType = event.target.options[instanceTypeElement.selectedIndex].value - if (instanceType == "url") { - nameCustomInstanceInput.setAttribute("type", "url") - nameCustomInstanceInput.setAttribute("placeholder", "https://www.google.com") - } else if (instanceType == "regex") { - nameCustomInstanceInput.setAttribute("type", "text") - nameCustomInstanceInput.setAttribute("placeholder", "https?://(www.|)youtube.com/") - } + instanceType = event.target.options[instanceTypeElement.selectedIndex].value + if (instanceType == "url") { + nameCustomInstanceInput.setAttribute("type", "url") + nameCustomInstanceInput.setAttribute("placeholder", "https://www.google.com") + } else if (instanceType == "regex") { + nameCustomInstanceInput.setAttribute("type", "text") + nameCustomInstanceInput.setAttribute("placeholder", "https?://(www.|)youtube.com/") + } }) let exceptionsCustomInstances = options.exceptions function calcExceptionsCustomInstances() { - document.getElementById("exceptions-custom-checklist").innerHTML = [...exceptionsCustomInstances.url, ...exceptionsCustomInstances.regex] - .map( - x => `
+ document.getElementById("exceptions-custom-checklist").innerHTML = [ + ...exceptionsCustomInstances.url, + ...exceptionsCustomInstances.regex, + ] + .map( + x => `
${x}

` - ) - .join("\n") - - for (const x of [...exceptionsCustomInstances.url, ...exceptionsCustomInstances.regex]) { - document.getElementById(`clear-${x}`).addEventListener("click", async () => { - let index - index = exceptionsCustomInstances.url.indexOf(x) - if (index > -1) exceptionsCustomInstances.url.splice(index, 1) - else { - index = exceptionsCustomInstances.regex.indexOf(x) - if (index > -1) exceptionsCustomInstances.regex.splice(index, 1) - } - options = await utils.getOptions() - options.exceptions = exceptionsCustomInstances - browser.storage.local.set({ options }) - calcExceptionsCustomInstances() - }) - } + ) + .join("\n") + + for (const x of [...exceptionsCustomInstances.url, ...exceptionsCustomInstances.regex]) { + document.getElementById(`clear-${x}`).addEventListener("click", async () => { + let index + index = exceptionsCustomInstances.url.indexOf(x) + if (index > -1) exceptionsCustomInstances.url.splice(index, 1) + else { + index = exceptionsCustomInstances.regex.indexOf(x) + if (index > -1) exceptionsCustomInstances.regex.splice(index, 1) + } + options = await utils.getOptions() + options.exceptions = exceptionsCustomInstances + browser.storage.local.set({ options }) + calcExceptionsCustomInstances() + }) + } } calcExceptionsCustomInstances() document.getElementById("custom-exceptions-instance-form").addEventListener("submit", async event => { - event.preventDefault() - let val - if (instanceType == "url" && nameCustomInstanceInput.validity.valid) { - val = nameCustomInstanceInput.value - if (!exceptionsCustomInstances.url.includes(val)) exceptionsCustomInstances.url.push(val) - } else if (instanceType == "regex") { - val = nameCustomInstanceInput.value - if (val.trim() != "" && !exceptionsCustomInstances.regex.includes(val)) exceptionsCustomInstances.regex.push(val) - } - if (val) { - options = await utils.getOptions() - options.exceptions = exceptionsCustomInstances - browser.storage.local.set({ options }, () => nameCustomInstanceInput.value = "") - } - calcExceptionsCustomInstances() + event.preventDefault() + let val + if (instanceType == "url" && nameCustomInstanceInput.validity.valid) { + val = nameCustomInstanceInput.value + if (!exceptionsCustomInstances.url.includes(val)) exceptionsCustomInstances.url.push(val) + } else if (instanceType == "regex") { + val = nameCustomInstanceInput.value + if (val.trim() != "" && !exceptionsCustomInstances.regex.includes(val)) exceptionsCustomInstances.regex.push(val) + } + if (val) { + options = await utils.getOptions() + options.exceptions = exceptionsCustomInstances + browser.storage.local.set({ options }, () => (nameCustomInstanceInput.value = "")) + } + calcExceptionsCustomInstances() }) diff --git a/src/pages/popup/popup.js b/src/pages/popup/popup.js index 485ec0e0..77a43000 100644 --- a/src/pages/popup/popup.js +++ b/src/pages/popup/popup.js @@ -5,15 +5,15 @@ import servicesHelper from "../../assets/javascripts/services.js" import utils from "../../assets/javascripts/utils.js" document.getElementById("more-options").href = browser.runtime.getURL("pages/options/index.html") -document.getElementById("more-options").setAttribute('target', '_blank') +document.getElementById("more-options").setAttribute("target", "_blank") await browser.runtime.getPlatformInfo(r => { - switch (r.os) { - case "fuchsia": - case "ios": - case "android": - document.getElementsByTagName("html")[0].classList.add("mobile") - } + switch (r.os) { + case "fuchsia": + case "ios": + case "android": + document.getElementsByTagName("html")[0].classList.add("mobile") + } }) const allSites = document.getElementById("all_sites") @@ -24,94 +24,96 @@ const config = await utils.getConfig() const divs = {} for (const service in config.services) { - divs[service] = {} - - divs[service].all = allSites.getElementsByClassName(service)[0] - divs[service].current = currSite.getElementsByClassName(service)[0] - - divs[service].all_toggle = allSites.getElementsByClassName(`${service}-enabled`)[0] - divs[service].all_toggle.addEventListener("change", async () => { - const options = await utils.getOptions() - options[service].enabled = divs[service].all_toggle.checked - browser.storage.local.set({ options }) - }) - - allSites.getElementsByClassName(`${service}-change_instance`)[0].addEventListener("click", () => { - browser.tabs.query({ active: true, currentWindow: true }, async tabs => { - if (tabs[0].url) { - const url = new URL(tabs[0].url) - browser.tabs.update({ url: await servicesHelper.switchInstance(url, service) }) - } - }) - }) - - divs[service].current_toggle = currSite.getElementsByClassName(`${service}-enabled`)[0] - divs[service].current_toggle.addEventListener("change", async () => { - const options = await utils.getOptions() - options[service].enabled = divs[service].current_toggle.checked - browser.storage.local.set({ options }) - }) - - currSite.getElementsByClassName(`${service}-change_instance`)[0].addEventListener("click", () => { - browser.tabs.query({ active: true, currentWindow: true }, async tabs => { - if (tabs[0].url) { - const url = new URL(tabs[0].url) - browser.tabs.update({ url: await servicesHelper.switchInstance(url, service) }) - } - }) - }) + divs[service] = {} + + divs[service].all = allSites.getElementsByClassName(service)[0] + divs[service].current = currSite.getElementsByClassName(service)[0] + + divs[service].all_toggle = allSites.getElementsByClassName(`${service}-enabled`)[0] + divs[service].all_toggle.addEventListener("change", async () => { + const options = await utils.getOptions() + options[service].enabled = divs[service].all_toggle.checked + browser.storage.local.set({ options }) + }) + + allSites.getElementsByClassName(`${service}-change_instance`)[0].addEventListener("click", () => { + browser.tabs.query({ active: true, currentWindow: true }, async tabs => { + if (tabs[0].url) { + const url = new URL(tabs[0].url) + browser.tabs.update({ url: await servicesHelper.switchInstance(url, service) }) + } + }) + }) + + divs[service].current_toggle = currSite.getElementsByClassName(`${service}-enabled`)[0] + divs[service].current_toggle.addEventListener("change", async () => { + const options = await utils.getOptions() + options[service].enabled = divs[service].current_toggle.checked + browser.storage.local.set({ options }) + }) + + currSite.getElementsByClassName(`${service}-change_instance`)[0].addEventListener("click", () => { + browser.tabs.query({ active: true, currentWindow: true }, async tabs => { + if (tabs[0].url) { + const url = new URL(tabs[0].url) + browser.tabs.update({ url: await servicesHelper.switchInstance(url, service) }) + } + }) + }) } browser.tabs.query({ active: true, currentWindow: true }, async tabs => { - // Set visibility of control buttons - if (tabs[0].url) { - const hr = document.getElementById("hr") - var url = new URL(tabs[0].url) - servicesHelper.switchInstance(url).then(r => { - if (r) { - document.getElementById("change_instance_div").style.display = "" - hr.style.display = "" - document.getElementById("change_instance").addEventListener("click", async () => - browser.tabs.update({ url: await servicesHelper.switchInstance(url) }) - ) - } - }) - servicesHelper.reverse(url).then(r => { - if (r) { - hr.style.display = "" - - document.getElementById("copy_original_div").style.display = "" - document.getElementById("copy_original").addEventListener("click", () => servicesHelper.copyRaw(url)) - - document.getElementById("redirect_to_original_div").style.display = "" - document.getElementById("redirect_to_original").addEventListener("click", () => browser.runtime.sendMessage("reverseTab")) - } - }) - servicesHelper.redirectAsync(url, "main_frame", null, true).then(r => { - if (r) { - document.getElementById("redirect_div").style.display = "" - hr.style.display = "" - document.getElementById("redirect").addEventListener("click", () => browser.runtime.sendMessage("redirectTab")) - } - }) - } - - const options = await utils.getOptions() - - // Set visibility of all service buttons - for (const service of options.popupServices) { - divs[service].all.classList.remove("hide") - divs[service].all_toggle.checked = options[service].enabled - } - - // Set visibility of current page service button - if (url) { - const service = await servicesHelper.computeService(url) - if (service) { - divs[service].all.classList.add("hide") - divs[service].current.classList.remove("hide") - divs[service].current_toggle.checked = options[service].enabled - currentSiteDivider.style.display = "" - } - } -}) \ No newline at end of file + // Set visibility of control buttons + if (tabs[0].url) { + const hr = document.getElementById("hr") + var url = new URL(tabs[0].url) + servicesHelper.switchInstance(url).then(r => { + if (r) { + document.getElementById("change_instance_div").style.display = "" + hr.style.display = "" + document + .getElementById("change_instance") + .addEventListener("click", async () => browser.tabs.update({ url: await servicesHelper.switchInstance(url) })) + } + }) + servicesHelper.reverse(url).then(r => { + if (r) { + hr.style.display = "" + + document.getElementById("copy_original_div").style.display = "" + document.getElementById("copy_original").addEventListener("click", () => servicesHelper.copyRaw(url)) + + document.getElementById("redirect_to_original_div").style.display = "" + document + .getElementById("redirect_to_original") + .addEventListener("click", () => browser.runtime.sendMessage("reverseTab")) + } + }) + servicesHelper.redirectAsync(url, "main_frame", null, true).then(r => { + if (r) { + document.getElementById("redirect_div").style.display = "" + hr.style.display = "" + document.getElementById("redirect").addEventListener("click", () => browser.runtime.sendMessage("redirectTab")) + } + }) + } + + const options = await utils.getOptions() + + // Set visibility of all service buttons + for (const service of options.popupServices) { + divs[service].all.classList.remove("hide") + divs[service].all_toggle.checked = options[service].enabled + } + + // Set visibility of current page service button + if (url) { + const service = await servicesHelper.computeService(url) + if (service) { + divs[service].all.classList.add("hide") + divs[service].current.classList.remove("hide") + divs[service].current_toggle.checked = options[service].enabled + currentSiteDivider.style.display = "" + } + } +}) diff --git a/src/pages/popup/style.css b/src/pages/popup/style.css index 5e39b4ed..41510615 100644 --- a/src/pages/popup/style.css +++ b/src/pages/popup/style.css @@ -1,65 +1,65 @@ body { - width: 270px; - min-height: auto; + width: 270px; + min-height: auto; } html, body { - margin: 0; + margin: 0; } .hide { - display: none !important; + display: none !important; } .button { - display: flex; - margin: 0 auto; - justify-content: space-between; + display: flex; + margin: 0 auto; + justify-content: space-between; } .button svg { - width: 26px; - height: 26px; + width: 26px; + height: 26px; } .bottom-button { - width: 100%; + width: 100%; } .space { - height: 10px; + height: 10px; } input { - height: 23px; - width: 46px; + height: 23px; + width: 46px; } div.block label { - margin: 0; - font-size: 18px; - font-weight: bold; - max-width: 180px; + margin: 0; + font-size: 18px; + font-weight: bold; + max-width: 180px; } div.block label:hover { - cursor: pointer; + cursor: pointer; } div.block div { - display: flex; + display: flex; } html.mobile body { - width: 100%; + width: 100%; } html.mobile div.block label { - font-size: 24px; + font-size: 24px; } html.mobile .button svg { - width: 30px; - height: 30px; -} \ No newline at end of file + width: 30px; + height: 30px; +} diff --git a/src/pages/stylesheets/styles.css b/src/pages/stylesheets/styles.css index b220efdb..ca6f0dca 100644 --- a/src/pages/stylesheets/styles.css +++ b/src/pages/stylesheets/styles.css @@ -1,450 +1,449 @@ body { - --text: #fff; - --bg-main: #121212; - --bg-secondary: #202020; - --active: #fbc117; - --danger: #f04141; - --light-grey: #c3c3c3; + --text: #fff; + --bg-main: #121212; + --bg-secondary: #202020; + --active: #fbc117; + --danger: #f04141; + --light-grey: #c3c3c3; } @font-face { - font-family: "Inter"; - src: url("Inter-VariableFont_slnt,wght.ttf"); - font-weight: normal; - font-style: normal; + font-family: "Inter"; + src: url("Inter-VariableFont_slnt,wght.ttf"); + font-weight: normal; + font-style: normal; } @font-face { - font-family: "Vazirmatn"; - src: url("Vazirmatn-VariableFont_wght.ttf"); - font-weight: normal; - font-style: normal; + font-family: "Vazirmatn"; + src: url("Vazirmatn-VariableFont_wght.ttf"); + font-weight: normal; + font-style: normal; } body { - margin: auto; - padding: 0; - font-family: "Inter"; - font-size: 16px; - background-color: var(--bg-main); - color: var(--text); + margin: auto; + padding: 0; + font-family: "Inter"; + font-size: 16px; + background-color: var(--bg-main); + color: var(--text); } body * { - font-family: "Inter"; + font-family: "Inter"; } body.rtl { - font-family: "Vazirmatn"; + font-family: "Vazirmatn"; } body.rtl * { - font-family: "Vazirmatn"; + font-family: "Vazirmatn"; } div.block input[type="checkbox"] { - appearance: none; - -moz-appearance: none; - -webkit-appearance: none; + appearance: none; + -moz-appearance: none; + -webkit-appearance: none; } .title { - display: flex; - align-items: center; - text-decoration: none; - width: min-content; - color: var(--text); - transition: .1s; + display: flex; + align-items: center; + text-decoration: none; + width: min-content; + color: var(--text); + transition: 0.1s; } .title:hover { - opacity: 1 !important; + opacity: 1 !important; } .title:hover a { - color: var(--active); + color: var(--active); } img, svg { - margin-right: 10px; - height: 26px; - width: 26px; - color: var(--text); + margin-right: 10px; + height: 26px; + width: 26px; + color: var(--text); } body.rtl img, body.rtl svg { - margin-right: 0px; - margin-left: 10px; + margin-right: 0px; + margin-left: 10px; } input[type="url"], input[type="text"], select { - font-weight: bold; - box-sizing: border-box; - border-style: solid; - border-color: #767676; - color: var(--text); - font-size: 16px; - padding: 8px; - background-color: var(--bg-secondary); - border: none; - margin: 0; - max-width: 500px; - border-radius: 3px; + font-weight: bold; + box-sizing: border-box; + border-style: solid; + border-color: #767676; + color: var(--text); + font-size: 16px; + padding: 8px; + background-color: var(--bg-secondary); + border: none; + margin: 0; + max-width: 500px; + border-radius: 3px; } input[type="url"], input[type="text"] { - width: 400px; - cursor: text; + width: 400px; + cursor: text; } input:invalid { - color: var(--danger); + color: var(--danger); } .button svg { - height: 18px; - width: 18px; + height: 18px; + width: 18px; } section.block-option { - width: 750px; - margin: 0 50px; + width: 750px; + margin: 0 50px; } section.block-option h2 { - margin: 0; + margin: 0; } body.option { - display: flex; - padding: 40px; - width: 1160px; + display: flex; + padding: 40px; + width: 1160px; } section.links { - display: flex; - flex-wrap: wrap; - flex-direction: column; - width: 350px; - max-height: 1030px; + display: flex; + flex-wrap: wrap; + flex-direction: column; + width: 350px; + max-height: 1030px; } section.links div { - margin: 10px; - width: max-content; + margin: 10px; + width: max-content; } a { - text-decoration: none; - color: var(--text); - transition: 0.1s; + text-decoration: none; + color: var(--text); + transition: 0.1s; } a:hover { - color: var(--active); + color: var(--active); } section.links a { - display: flex; - align-items: center; - font-size: 18px; - text-decoration: none; - color: white; - transition: 0.1s; + display: flex; + align-items: center; + font-size: 18px; + text-decoration: none; + color: white; + transition: 0.1s; } section.links a:hover, section.links .selected { - opacity: 1 !important; + opacity: 1 !important; } section.links .selected a { - color: var(--active); + color: var(--active); } ::placeholder { - color: var(--text); - opacity: 0.7; + color: var(--text); + opacity: 0.7; } hr { - height: 2px; - margin: 0 15px; - background-color: rgb(77, 77, 77); - border: none; + height: 2px; + margin: 0 15px; + background-color: rgb(77, 77, 77); + border: none; } div.block { - padding: 0 15px; - justify-content: space-between; - display: flex; - align-items: center; - margin-top: 10px; - margin-bottom: 10px; + padding: 0 15px; + justify-content: space-between; + display: flex; + align-items: center; + margin-top: 10px; + margin-bottom: 10px; } div.block-option { - margin: 30px 0; + margin: 30px 0; } div.block-option label { - margin-right: 5px; - width: 80%; - min-width: 150px; - font-size: 18px; + margin-right: 5px; + width: 80%; + min-width: 150px; + font-size: 18px; } div.block-option h1 { - margin: 0; - font-size: 28px; - color: var(--text); + margin: 0; + font-size: 28px; + color: var(--text); } div.block-option div { - text-align: center; + text-align: center; } div.block input[type="checkbox"] { - width: 46px; - height: 24px; - background-color: var(--light-grey); - border-radius: 50px; - transition: 0.4s; - cursor: pointer; + width: 46px; + height: 24px; + background-color: var(--light-grey); + border-radius: 50px; + transition: 0.4s; + cursor: pointer; } div.block input[type="checkbox"]:checked { - background-color: var(--active); + background-color: var(--active); } div.block input[type="checkbox"]::before { - content: ""; - display: inline-block; - width: 18px; - height: 18px; - box-sizing: border-box; - position: relative; - top: 2.5px; - left: 3.5px; - background-color: white; - border-radius: 50%; - transition: 0.3s; + content: ""; + display: inline-block; + width: 18px; + height: 18px; + box-sizing: border-box; + position: relative; + top: 2.5px; + left: 3.5px; + background-color: white; + border-radius: 50%; + transition: 0.3s; } body.rtl div.block input[type="checkbox"]::before { - left: auto; - right: 4px; + left: auto; + right: 4px; } div.block input[type="checkbox"]:checked::before { - left: 24px; + left: 24px; } body.rtl div.block input[type="checkbox"]:checked::before { - left: auto; - right: 24px; + left: auto; + right: 24px; } div.buttons { - display: flex; - margin: 0 15px; - margin-bottom: 15px; - margin-top: 15px; - flex-wrap: wrap; - align-items: center; - justify-content: start; + display: flex; + margin: 0 15px; + margin-bottom: 15px; + margin-top: 15px; + flex-wrap: wrap; + align-items: center; + justify-content: start; } .button { - color: var(--text); - font-size: 16px; - font-weight: bold; - text-decoration: none; - cursor: pointer; - transition-duration: 0.1s; + color: var(--text); + font-size: 16px; + font-weight: bold; + text-decoration: none; + cursor: pointer; + transition-duration: 0.1s; } .button:hover { - color: var(--active); + color: var(--active); } .button svg { - width: auto; - height: auto; - padding: 0; - margin-right: 5px; + width: auto; + height: auto; + padding: 0; + margin-right: 5px; } .button:hover svg { - color: var(--active); + color: var(--active); } .button-inline { - display: inline-flex; - align-items: center; - margin: 7.5px 0; - background-color: var(--bg-secondary); - border-radius: 5px; - padding: 10px; + display: inline-flex; + align-items: center; + margin: 7.5px 0; + background-color: var(--bg-secondary); + border-radius: 5px; + padding: 10px; } .button:active { - transform: translateY(1px); + transform: translateY(1px); } button svg { - color: var(--text); + color: var(--text); } div.checklist div { - justify-content: space-between; - margin: 5px 15px; - padding: 10px 0; - word-wrap: break-word; - display: flex; + justify-content: space-between; + margin: 5px 15px; + padding: 10px 0; + word-wrap: break-word; + display: flex; } div.checklist a { - text-decoration: none; - color: var(--text); + text-decoration: none; + color: var(--text); } div.checklist a:hover { - text-decoration: underline; + text-decoration: underline; } div.custom-checklist x a { - color: var(--active); + color: var(--active); } button.add { - background-color: transparent; - border: none; - padding: 0; - margin: 0; - text-decoration: none; - display: inline-block; - cursor: pointer; + background-color: transparent; + border: none; + padding: 0; + margin: 0; + text-decoration: none; + display: inline-block; + cursor: pointer; } body.light-theme { - --text: black; - --bg-main: white; - --bg-secondary: #e4e4e4; - --active: #fb9817; + --text: black; + --bg-main: white; + --bg-secondary: #e4e4e4; + --active: #fb9817; } body.light-theme select { - border: 1px solid black; + border: 1px solid black; } body.light-theme a { - color: black; + color: black; } body.light-theme a:hover { - color: var(--active) + color: var(--active); } button { - background-color: transparent; - color: var(--text); - border: none; - text-decoration: none; - display: inline-block; - cursor: pointer; - border-radius: 5px; + background-color: transparent; + color: var(--text); + border: none; + text-decoration: none; + display: inline-block; + cursor: pointer; + border-radius: 5px; } body div section { - display: none; + display: none; } select:disabled { - opacity: 0.6; - cursor: not-allowed; + opacity: 0.6; + cursor: not-allowed; } input:disabled { - opacity: 0.6; - cursor: not-allowed; + opacity: 0.6; + cursor: not-allowed; } @media (max-width: 1250px) { - body.option { - flex-direction: column; - width: 95vw; - align-items: center; - padding: 40px 0px; - } + body.option { + flex-direction: column; + width: 95vw; + align-items: center; + padding: 40px 0px; + } - section.links { - flex-direction: row; - width: 95vw; - padding: 0 55px; - } + section.links { + flex-direction: row; + width: 95vw; + padding: 0 55px; + } - section.block-option { - width: 95vw; - } + section.block-option { + width: 95vw; + } - div.checklist div x { - overflow: hidden; - } + div.checklist div x { + overflow: hidden; + } } html.mobile img, html.mobile svg { - margin-right: 10px; - height: 30px; - width: 30px; - color: var(--text); + margin-right: 10px; + height: 30px; + width: 30px; + color: var(--text); } html.mobile div.block { - padding: 0 15px; - justify-content: space-between; - display: flex; - align-items: center; - margin-top: 20px; - margin-bottom: 20px; + padding: 0 15px; + justify-content: space-between; + display: flex; + align-items: center; + margin-top: 20px; + margin-bottom: 20px; } html.mobile div.block input[type="checkbox"] { - width: 58px; - height: 30px; + width: 58px; + height: 30px; } html.mobile div.block input[type="checkbox"]::before { - width: 24px; - height: 24px; - top: 3px; - left: 3.5px; + width: 24px; + height: 24px; + top: 3px; + left: 3.5px; } html.mobile div.block input[type="checkbox"]:checked::before { - left: 30px; + left: 30px; } html.mobile body.option { - flex-direction: column; - width: 100%; - padding: 0; - align-items: center; + flex-direction: column; + width: 100%; + padding: 0; + align-items: center; } - html.mobile section.links { - flex-direction: row; - width: 100%; - padding: 0 55px; + flex-direction: row; + width: 100%; + padding: 0 55px; } html.mobile section.block-option { - width: 100%; -} \ No newline at end of file + width: 100%; +} diff --git a/test-conditions.md b/test-conditions.md index 843b263e..7909a1a2 100644 --- a/test-conditions.md +++ b/test-conditions.md @@ -16,11 +16,11 @@ YouTube Music (Tested with YouTube turned off) Homepage - [https://music.youtube.com](https://music.youtube.com) Page - [https://hyperpipe.surge.sh/channel/UCPC0L1d253x-KuMNwa05TpA](https://hyperpipe.surge.sh/channel/UCPC0L1d253x-KuMNwa05TpA) Explore - [https://hyperpipe.surge.sh/explore/](https://hyperpipe.surge.sh/explore/) - + YT Embeds - [https://famiboards.com/threads/nintendo-switch-sports-announced-launches-april-29th-update-main-theme-in-threadmarks.1907/](https://famiboards.com/threads/nintendo-switch-sports-announced-launches-april-29th-update-main-theme-in-threadmarks.1907/) Twitch -[https://www.twitch.tv/pokimane](https://www.twitch.tv/pokimane) - + TikTok - [https://www.tiktok.com/@zoecolletti?lang=en](https://www.tiktok.com/@zoecolletti?lang=en) Reddit & Imgur `(Embeds)` - [https://www.reddit.com/61ns2w/](https://www.reddit.com/61ns2w/) @@ -32,22 +32,22 @@ Quora - [https://www.quora.com/What-is-the-equivalent-weight-of-hydrocloric-acid Pinterest - [https://www.pinterest.com/aldiukstores/aldi-recipes/](https://www.pinterest.com/aldiukstores/aldi-recipes/) IMDb - [https://www.imdb.com/title/tt23556786/](https://www.imdb.com/title/tt23556786/) **[Check if new URL schemes are supported by the Dev.]** - + Fandom - [https://naruto.fandom.com](https://naruto.fandom.com) - + Genius - [https://genius.com/Doja-cat-demons-lyrics](https://genius.com/Doja-cat-demons-lyrics) - + Urbandictionary - [https://urbandictionary.com/define.php?term=Roads](https://urbandictionary.com/define.php?term=Roads) - + Stackoverflow - [https://stackoverflow.com/questions/16330404/how-to-remove-remote-origin-from-a-git-repository](https://stackoverflow.com/questions/16330404/how-to-remove-remote-origin-from-a-git-repository) - + Goodreads - [https://www.goodreads.com/book/show/3869.A_Brief_History_of_Time](https://www.goodreads.com/book/show/3869.A_Brief_History_of_Time) - + Bandcamp - [https://thorwegian.bandcamp.com/track/just-because](https://thorwegian.bandcamp.com/track/just-because) - + Instructables - [https://instructables.com/DIY-Arduino-Obstacle-Avoiding-Car-at-Home/](https://instructables.com/DIY-Arduino-Obstacle-Avoiding-Car-at-Home/) - -Web archive - [https://web.archive.org/web/20230131222432if_/https://www.dailymail.co.uk/news/article-11687675/Army-spied-lockdown-critics-Sceptics-including-Peter-Hitchens-suspected-watched.html](https://web.archive.org/web/20230131222432if_/https://www.dailymail.co.uk/news/article-11687675/Army-spied-lockdown-critics-Sceptics-including-Peter-Hitchens-suspected-watched.html) + +Web archive - [https://web.archive.org/web/20230131222432if\_/https://www.dailymail.co.uk/news/article-11687675/Army-spied-lockdown-critics-Sceptics-including-Peter-Hitchens-suspected-watched.html](https://web.archive.org/web/20230131222432if_/https://www.dailymail.co.uk/news/article-11687675/Army-spied-lockdown-critics-Sceptics-including-Peter-Hitchens-suspected-watched.html) --- -- cgit 1.4.1 From 5be3c07ebf228b5595d872a7733371fe59675443 Mon Sep 17 00:00:00 2001 From: ManeraKai Date: Wed, 24 Jul 2024 12:20:49 +0300 Subject: Migrating UI to Svelte --- .gitignore | 1 + package.json | 13 +- rollup.config.js | 35 +++ src/pages/options/index.js | 399 --------------------------- src/pages/options/index.pug | 10 - src/pages/options/widgets/general.js | 216 --------------- src/pages/options/widgets/general.pug | 88 ------ src/pages/options/widgets/services.pug | 83 ------ src/pages/src/App.svelte | 118 ++++++++ src/pages/src/General/Exceptions.svelte | 108 ++++++++ src/pages/src/General/General.svelte | 70 +++++ src/pages/src/General/SettingsButtons.svelte | 118 ++++++++ src/pages/src/Services/Instances.svelte | 234 ++++++++++++++++ src/pages/src/Services/RedirectType.svelte | 84 ++++++ src/pages/src/Services/Services.svelte | 119 ++++++++ src/pages/src/Sidebar.svelte | 43 +++ src/pages/src/components/Button.svelte | 29 ++ src/pages/src/components/Checkbox.svelte | 52 ++++ src/pages/src/components/Input.svelte | 41 +++ src/pages/src/components/Label.svelte | 18 ++ src/pages/src/components/Row.svelte | 12 + src/pages/src/components/RowCheckbox.svelte | 14 + src/pages/src/components/RowSelect.svelte | 19 ++ src/pages/src/components/Select.svelte | 34 +++ src/pages/src/icons/AboutIcon.svelte | 11 + src/pages/src/icons/AddIcon.svelte | 3 + src/pages/src/icons/CloseIcon.svelte | 3 + src/pages/src/icons/ExportIcon.svelte | 5 + src/pages/src/icons/GeneralIcon.svelte | 13 + src/pages/src/icons/ImportIcon.svelte | 5 + src/pages/src/icons/PingIcon.svelte | 5 + src/pages/src/icons/ResetIcon.svelte | 15 + src/pages/src/icons/ServicesIcon.svelte | 11 + src/pages/src/main.js | 7 + src/pages/src/stores.js | 5 + src/pages/widgets/head.pug | 7 - src/pages/widgets/links.pug | 22 -- 37 files changed, 1243 insertions(+), 827 deletions(-) create mode 100644 rollup.config.js delete mode 100644 src/pages/options/index.js delete mode 100644 src/pages/options/index.pug delete mode 100644 src/pages/options/widgets/general.js delete mode 100644 src/pages/options/widgets/general.pug delete mode 100644 src/pages/options/widgets/services.pug create mode 100644 src/pages/src/App.svelte create mode 100644 src/pages/src/General/Exceptions.svelte create mode 100644 src/pages/src/General/General.svelte create mode 100644 src/pages/src/General/SettingsButtons.svelte create mode 100644 src/pages/src/Services/Instances.svelte create mode 100644 src/pages/src/Services/RedirectType.svelte create mode 100644 src/pages/src/Services/Services.svelte create mode 100644 src/pages/src/Sidebar.svelte create mode 100644 src/pages/src/components/Button.svelte create mode 100644 src/pages/src/components/Checkbox.svelte create mode 100644 src/pages/src/components/Input.svelte create mode 100644 src/pages/src/components/Label.svelte create mode 100644 src/pages/src/components/Row.svelte create mode 100644 src/pages/src/components/RowCheckbox.svelte create mode 100644 src/pages/src/components/RowSelect.svelte create mode 100644 src/pages/src/components/Select.svelte create mode 100644 src/pages/src/icons/AboutIcon.svelte create mode 100644 src/pages/src/icons/AddIcon.svelte create mode 100644 src/pages/src/icons/CloseIcon.svelte create mode 100644 src/pages/src/icons/ExportIcon.svelte create mode 100644 src/pages/src/icons/GeneralIcon.svelte create mode 100644 src/pages/src/icons/ImportIcon.svelte create mode 100644 src/pages/src/icons/PingIcon.svelte create mode 100644 src/pages/src/icons/ResetIcon.svelte create mode 100644 src/pages/src/icons/ServicesIcon.svelte create mode 100644 src/pages/src/main.js create mode 100644 src/pages/src/stores.js delete mode 100644 src/pages/widgets/head.pug delete mode 100644 src/pages/widgets/links.pug (limited to 'src/pages/options/index.js') diff --git a/.gitignore b/.gitignore index b1b53216..7ca34ebb 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ package-lock.json src/pages/options/index.html src/pages/popup/popup.html pnpm-lock.yaml +src/pages/options/build \ No newline at end of file diff --git a/package.json b/package.json index dba432a5..234dd5e1 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,6 @@ { "name": "libredirect", + "type": "module", "description": "Redirects YouTube, Twitter, TikTok and more to privacy friendly frontends.", "engines": { "node": ">=16.13.1", @@ -11,7 +12,7 @@ "android": "web-ext run -t firefox-android --adb-device emulator-5554 --firefox-apk org.mozilla.fenix", "build": "web-ext build", "test": "web-ext lint", - "html": "pug --basedir ./ --obj ./src/config.json src/pages/options/index.pug --out src/pages/options/ && pug --basedir ./ --obj ./src/config.json src/pages/popup/popup.pug --out src/pages/popup/" + "html": "rollup -c" }, "repository": { "type": "git", @@ -24,8 +25,16 @@ }, "homepage": "https://libredirect.github.io", "devDependencies": { + "prettier": "3.3.3", "pug-cli": "^1.0.0-alpha6", - "web-ext": "^7.2.0" + "web-ext": "^7.2.0", + "@rollup/plugin-commonjs": "^24.0.0", + "@rollup/plugin-node-resolve": "^15.0.0", + "@rollup/plugin-terser": "^0.4.0", + "rollup": "^3.15.0", + "rollup-plugin-css-only": "^4.3.0", + "rollup-plugin-svelte": "^7.1.2", + "svelte": "^3.55.0" }, "webExt": { "sourceDir": "./src/", diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 00000000..71222de8 --- /dev/null +++ b/rollup.config.js @@ -0,0 +1,35 @@ +import svelte from "rollup-plugin-svelte" +import commonjs from "@rollup/plugin-commonjs" +import terser from "@rollup/plugin-terser" +import resolve from "@rollup/plugin-node-resolve" +import css from "rollup-plugin-css-only" + +const production = !process.env.ROLLUP_WATCH + +export default { + input: "src/pages/src/main.js", + output: { + sourcemap: true, + format: "iife", + name: "app", + file: "src/pages/options/build/bundle.js", + }, + plugins: [ + svelte({ + compilerOptions: { + dev: !production, + }, + }), + css({ output: "bundle.css" }), + resolve({ + browser: true, + dedupe: ["svelte"], + exportConditions: ["svelte"], + }), + commonjs(), + // production && terser(), + ], + watch: { + clearScreen: false, + }, +} diff --git a/src/pages/options/index.js b/src/pages/options/index.js deleted file mode 100644 index 8d5676e0..00000000 --- a/src/pages/options/index.js +++ /dev/null @@ -1,399 +0,0 @@ -import utils from "../../assets/javascripts/utils.js" - -let config, - options, - blacklist, - redirects, - divs = {} - -for (const a of document.getElementById("links").getElementsByTagName("a")) { - if (!a.href.includes("https://")) { - a.addEventListener("click", e => { - const path = a.getAttribute("href").replace("#", "") - loadPage(path) - e.preventDefault() - }) - } -} - -config = await utils.getConfig() -options = await utils.getOptions() - -/** - * @param {string} service - */ -async function changeFrontendsSettings(service) { - options = await utils.getOptions() - const opacityDiv = document.getElementById(`${service}-opacity`) - if (document.getElementById(`${service}-enabled`).checked) { - opacityDiv.style.pointerEvents = "auto" - opacityDiv.style.opacity = 1 - opacityDiv.style.userSelect = "auto" - } else { - opacityDiv.style.pointerEvents = "none" - opacityDiv.style.opacity = 0.4 - opacityDiv.style.userSelect = "none" - } - 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 || - (config.services[service].frontends[divs[service].frontend.value].desktopApp && - divs[service].embedFrontend && - frontend == divs[service].embedFrontend.value) - ) { - frontendDiv.style.display = "" - if (config.services[service].frontends[frontend].localhost === true) { - document.getElementById(`${service}-instance-div`).style.display = "" - - if (options[service].instance == "localhost") { - frontendDiv.style.display = "none" - } - } else { - document.getElementById(`${service}-instance-div`).style.display = "none" - } - } else { - frontendDiv.style.display = "none" - } - } - } - } - if (document.getElementById(`${service}-redirectType`)) { - const frontend = options[service].frontend - if (config.services[service].frontends[frontend].embeddable) { - document.getElementById(`${service}-redirectType`).innerHTML = ` - - - ` - } else if ( - config.services[service].frontends[frontend].desktopApp && - Object.values(config.services[service].frontends).some(frontend => frontend.embeddable) - ) { - document.getElementById(`${service}-redirectType`).innerHTML = ` - - ` - if (options[service].redirectType == "sub_frame") { - options[service].redirectType = "main_frame" - browser.storage.local.set({ options }) - } - } else { - document.getElementById(`${service}-redirectType`).innerHTML = - '' - options[service].redirectType = "main_frame" - - browser.storage.local.set({ options }) - } - document.getElementById(`${service}-redirectType`).value = options[service].redirectType - if (config.services[service].frontends[frontend].desktopApp && options[service].redirectType != "main_frame") { - document.getElementById(`${service}-embedFrontend-div`).style.display = "" - document.getElementById(divs[service].embedFrontend.value).style.display = "" - } else if ( - config.services[service].frontends[frontend].desktopApp && - options[service].redirectType == "main_frame" - ) { - document.getElementById(`${service}-embedFrontend-div`).style.display = "none" - document.getElementById(divs[service].embedFrontend.value).style.display = "none" - } else { - document.getElementById(`${service}-embedFrontend-div`).style.display = "none" - } - } - const frontend_name_element = document.getElementById(`${service}_page`).getElementsByClassName("frontend_name")[0] - frontend_name_element.href = config.services[service].frontends[divs[service].frontend.value].url -} - -/** - * @param {string} path - */ -async function loadPage(path) { - options = await utils.getOptions() - for (const section of document.getElementById("pages").getElementsByTagName("section")) section.style.display = "none" - document.getElementById(`${path}_page`).style.display = "block" - - for (const element of document.getElementsByClassName("title")) { - const a = element.getElementsByTagName("a")[0] - if (a.getAttribute("href") == `#${path}`) { - element.classList.add("selected") - } else { - element.classList.remove("selected") - } - } - - for (const service in config.services) { - if (options[service].enabled) { - document.getElementById(`${service}-link`).style.opacity = 1 - } else { - document.getElementById(`${service}-link`).style.opacity = 0.4 - } - } - - window.history.pushState({ id: "100" }, "Page 2", `/pages/options/index.html#${path}`) - - if (path != "general") { - 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", async () => { - let options = await utils.getOptions() - 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) - }) - } - changeFrontendsSettings(service) - - blacklist = await utils.getBlacklist(options) - redirects = await utils.getList(options) - for (const frontend in config.services[service].frontends) { - if (config.services[service].frontends[frontend].instanceList) { - if (redirects == "disabled" || blacklist == "disabled") { - document.getElementById(frontend).getElementsByClassName("clearnet")[0].style.display = "none" - document.getElementById(frontend).getElementsByClassName("ping")[0].style.display = "none" - } else if (!redirects || !blacklist) { - document - .getElementById(frontend) - .getElementsByClassName("clearnet")[0] - .getElementsByClassName("checklist")[0] - .getElementsByClassName("loading")[0].innerHTML = "Could not fetch instances." - } else { - createList(frontend) - } - } - } - - for (const frontend in config.services[service].frontends) { - if (config.services[service].frontends[frontend].instanceList) { - processCustomInstances(frontend) - document.getElementById(`ping-${frontend}`).addEventListener("click", async () => { - document.getElementById(`ping-${frontend}`).getElementsByTagName("x")[0].innerHTML = "Pinging..." - await ping(frontend) - document.getElementById(`ping-${frontend}`).getElementsByTagName("x")[0].innerHTML = "Ping instances" - }) - } - } - } -} - -async function calcCustomInstances(frontend) { - let options = await utils.getOptions() - let customInstances = options[frontend] - const pingCache = await utils.getPingCache() - - document.getElementById(frontend).getElementsByClassName("custom-checklist")[0].innerHTML = customInstances - .map(x => { - const time = pingCache[x] - if (time) { - var { color, text } = processTime(time) - } - const timeText = time ? `${text}` : "" - const custom = isCustomInstance(frontend, x) ? "" : `custom` - return `
- - ${x} - ${timeText} - ${custom} - - -
-
` - }) - .join("\n") - for (const item of customInstances) { - document - .getElementById(frontend) - .getElementsByClassName(`clear-${item}`)[0] - .addEventListener("click", async () => { - const index = customInstances.indexOf(item) - if (index > -1) customInstances.splice(index, 1) - options = await utils.getOptions() - options[frontend] = customInstances - browser.storage.local.set({ options }, async () => { - calcCustomInstances(frontend) - createList(frontend) - }) - }) - } -} - -async function processCustomInstances(frontend) { - calcCustomInstances(frontend) - document - .getElementById(frontend) - .getElementsByClassName("custom-instance-form")[0] - .addEventListener("submit", async event => { - event.preventDefault() - let options = await utils.getOptions() - let customInstances = options[frontend] - let frontendCustomInstanceInput = document.getElementById(frontend).getElementsByClassName("custom-instance")[0] - try { - var url = new URL(frontendCustomInstanceInput.value) - } catch (error) { - return - } - let protocolHostVar = utils.protocolHost(url) - if (frontendCustomInstanceInput.validity.valid) { - if (!customInstances.includes(protocolHostVar)) { - customInstances.push(protocolHostVar) - options = await utils.getOptions() - options[frontend] = customInstances - browser.storage.local.set({ options }, () => { - frontendCustomInstanceInput.value = "" - calcCustomInstances(frontend) - }) - } - } - }) -} - -/** - * @param {string} frontend - */ -async function createList(frontend) { - const pingCache = await utils.getPingCache() - const options = await utils.getOptions() - for (const network in config.networks) { - const checklist = document - .getElementById(frontend) - .getElementsByClassName(network)[0] - .getElementsByClassName("checklist")[0] - - if (!redirects[frontend]) { - checklist.innerHTML = '
No instances found.
' - break - } - - const instances = redirects[frontend][network] - if (!instances || instances.length === 0) continue - - document.getElementById(frontend).getElementsByClassName("custom-instance")[0].placeholder = - redirects[frontend].clearnet[0] - - instances.sort((a, b) => blacklist.cloudflare.includes(a) && !blacklist.cloudflare.includes(b)) - const content = instances.map(x => { - const cloudflare = blacklist.cloudflare.includes(x) - ? ` - cloudflare - ` - : "" - - let time = pingCache[x] - let timeText = "" - if (time) { - const { color, text } = processTime(time) - timeText = `${text}` - } - - const chosen = options[frontend].includes(x) ? `chosen` : "" - - const warnings = [cloudflare, timeText, chosen].join(" ") - return `
- - ${x} - ${warnings} - - -
` - }) - - checklist.innerHTML = [ - `
- -
`, - ...content, - "
", - ].join("\n
\n") - - for (const instance of instances) { - checklist.getElementsByClassName(`add-${instance}`)[0].addEventListener("click", async () => { - let options = await utils.getOptions() - if (!options[frontend].includes(instance)) { - options[frontend].push(instance) - browser.storage.local.set({ options }, () => { - calcCustomInstances(frontend) - createList(frontend) - }) - } - }) - } - } -} - -const r = window.location.href.match(/#(.*)/) -if (r) loadPage(r[1]) -else loadPage("general") - -/** - * @param {string} frontend - */ -async function ping(frontend) { - const instanceElements = [ - ...document.getElementById(frontend).getElementsByClassName("custom-checklist")[0].getElementsByTagName("x"), - ...document.getElementById(frontend).getElementsByClassName("clearnet")[0].getElementsByTagName("x"), - ] - - let pingCache = await utils.getPingCache() - let redundancyList = {} - for (const element of instanceElements) { - let span = element.getElementsByClassName("ping")[0] - if (!span) span = document.createElement("span") - span.classList = ["ping"] - span.innerHTML = 'pinging...' - element.appendChild(span) - const href = element.getElementsByTagName("a")[0].href - const innerHTML = element.getElementsByTagName("a")[0].innerHTML - const time = redundancyList[innerHTML] ?? (await utils.ping(href)) - const { color, text } = processTime(time) - span.innerHTML = `${text}` - pingCache[innerHTML] = time - redundancyList[innerHTML] = time - - browser.storage.local.set({ pingCache }) - } -} - -/** - * @param {number} time - */ -function processTime(time) { - let text - let color - if (time < 5000) { - text = `${time}ms` - if (time <= 1000) color = "green" - else if (time <= 2000) color = "orange" - } else if (time >= 5000) { - color = "red" - if (time == 5000) text = "5000ms+" - if (time > 5000) text = `Error: ${time - 5000}` - } else { - color = "red" - text = "Server not found" - } - return { color, text } -} - -function isCustomInstance(frontend, instance) { - for (const network in config.networks) { - if (!redirects[frontend]) return false - const instances = redirects[frontend][network] - if (instances.includes(instance)) return true - } - return false -} diff --git a/src/pages/options/index.pug b/src/pages/options/index.pug deleted file mode 100644 index 4e19b087..00000000 --- a/src/pages/options/index.pug +++ /dev/null @@ -1,10 +0,0 @@ -doctype html -html(id="elementToShowWithJavaScript" lang="en") - include /src/pages/widgets/head - body(class="option" dir="auto") - include /src/pages/widgets/links - div#pages - include /src/pages/options/widgets/general - include /src/pages/options/widgets/services - script(type="module" src="./index.js") - \ No newline at end of file diff --git a/src/pages/options/widgets/general.js b/src/pages/options/widgets/general.js deleted file mode 100644 index b9ddfd08..00000000 --- a/src/pages/options/widgets/general.js +++ /dev/null @@ -1,216 +0,0 @@ -"use strict" -window.browser = window.browser || window.chrome - -import utils from "../../../assets/javascripts/utils.js" -import servicesHelper from "../../../assets/javascripts/services.js" - -const isChrome = browser.runtime.getBrowserInfo === undefined - -async function setOption(option, type, event) { - let options = await utils.getOptions() - switch (type) { - case "select": - options[option] = event.target.options[event.target.options.selectedIndex].value - break - case "checkbox": - options[option] = event.target.checked - break - case "range": - options[option] = event.target.value - break - } - browser.storage.local.set({ options }) -} - -const exportSettingsElement = document.getElementById("export-settings") -async function exportSettings() { - const options = await utils.getOptions() - options.version = browser.runtime.getManifest().version - let resultString = JSON.stringify(options, null, " ") - exportSettingsElement.href = "data:application/json;base64," + btoa(resultString) - exportSettingsElement.download = `libredirect-settings-v${options.version}.json` - return -} -exportSettings() -document.getElementById("general_page").onclick = exportSettings - -const importSettingsElement = document.getElementById("import-settings") -const importSettingsElementText = document.getElementById("import_settings_text") -importSettingsElement.addEventListener("change", () => { - function importError() { - const oldHTML = importSettingsElementText.innerHTML - importSettingsElementText.innerHTML = 'Error!' - setTimeout(() => (importSettingsElementText.innerHTML = oldHTML), 1000) - } - importSettingsElementText.innerHTML = "..." - let file = importSettingsElement.files[0] - const reader = new FileReader() - reader.readAsText(file) - reader.onload = async () => { - const data = JSON.parse(reader.result) - if ("theme" in data && data.version == browser.runtime.getManifest().version) { - browser.storage.local.clear(async () => { - browser.storage.local.set({ options: data }, () => { - location.reload() - }) - }) - } else { - console.log("incompatible settings") - importError() - } - } - reader.onerror = error => { - console.log("error", error) - importError() - } -}) - -const exportSettingsSync = document.getElementById("export-settings-sync") -const importSettingsSync = document.getElementById("import-settings-sync") -const importSettingsSyncText = document.getElementById("import_settings_sync_text") - -exportSettingsSync.addEventListener("click", async () => { - let options = await utils.getOptions() - options.version = browser.runtime.getManifest().version - browser.storage.sync.set({ options }, () => location.reload()) -}) - -importSettingsSync.addEventListener("click", () => { - function importError() { - importSettingsSyncText.innerHTML = 'Error!' - setTimeout(() => (importSettingsSyncText.innerHTML = oldHTML), 1000) - } - const oldHTML = importSettingsSyncText.innerHTML - importSettingsSyncText.innerHTML = "..." - browser.storage.sync.get({ options }, r => { - const options = r.options - if (options.version == browser.runtime.getManifest().version) { - browser.storage.local.set({ options }, () => location.reload()) - } else { - importError() - } - }) -}) - -const resetSettings = document.getElementById("reset-settings") -resetSettings.addEventListener("click", async () => { - resetSettings.innerHTML = "..." - await servicesHelper.initDefaults() - location.reload() -}) - -const fetchInstancesElement = document.getElementById("fetch-instances") -fetchInstancesElement.addEventListener("change", event => { - setOption("fetchInstances", "select", event) - location.reload() -}) - -const redirectOnlyInIncognitoElement = document.getElementById("redirectOnlyInIncognito") -redirectOnlyInIncognitoElement.addEventListener("change", event => { - setOption("redirectOnlyInIncognito", "checkbox", event) -}) - -const bookmarksMenuElement = document.getElementById("bookmarksMenu") -bookmarksMenuElement.addEventListener("change", async event => { - if (event.target.checked) - browser.permissions.request({ permissions: ["bookmarks"] }, r => (bookmarksMenuElement.checked = r)) - else browser.permissions.remove({ permissions: ["bookmarks"] }, r => (bookmarksMenuElement.checked = !r)) -}) - -let themeElement = document.getElementById("theme") -themeElement.addEventListener("change", event => { - setOption("theme", "select", event) - location.reload() -}) - -let nameCustomInstanceInput = document.getElementById("exceptions-custom-instance") -let instanceTypeElement = document.getElementById("exceptions-custom-instance-type") -let instanceType = "url" - -let config = await utils.getConfig() - -for (const service in config.services) { - document.getElementById(service).addEventListener("change", async event => { - let options = await utils.getOptions() - 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 }) - }) -} - -let options = await utils.getOptions() -themeElement.value = options.theme -fetchInstancesElement.value = options.fetchInstances -redirectOnlyInIncognitoElement.checked = options.redirectOnlyInIncognito -browser.permissions.contains({ permissions: ["bookmarks"] }, r => (bookmarksMenuElement.checked = r)) -for (const service in config.services) - document.getElementById(service).checked = options.popupServices.includes(service) - -instanceTypeElement.addEventListener("change", event => { - instanceType = event.target.options[instanceTypeElement.selectedIndex].value - if (instanceType == "url") { - nameCustomInstanceInput.setAttribute("type", "url") - nameCustomInstanceInput.setAttribute("placeholder", "https://www.google.com") - } else if (instanceType == "regex") { - nameCustomInstanceInput.setAttribute("type", "text") - nameCustomInstanceInput.setAttribute("placeholder", "https?://(www.|)youtube.com/") - } -}) - -let exceptionsCustomInstances = options.exceptions -function calcExceptionsCustomInstances() { - document.getElementById("exceptions-custom-checklist").innerHTML = [ - ...exceptionsCustomInstances.url, - ...exceptionsCustomInstances.regex, - ] - .map( - x => `
- ${x} - -
-
` - ) - .join("\n") - - for (const x of [...exceptionsCustomInstances.url, ...exceptionsCustomInstances.regex]) { - document.getElementById(`clear-${x}`).addEventListener("click", async () => { - let index - index = exceptionsCustomInstances.url.indexOf(x) - if (index > -1) exceptionsCustomInstances.url.splice(index, 1) - else { - index = exceptionsCustomInstances.regex.indexOf(x) - if (index > -1) exceptionsCustomInstances.regex.splice(index, 1) - } - options = await utils.getOptions() - options.exceptions = exceptionsCustomInstances - browser.storage.local.set({ options }) - calcExceptionsCustomInstances() - }) - } -} -calcExceptionsCustomInstances() -document.getElementById("custom-exceptions-instance-form").addEventListener("submit", async event => { - event.preventDefault() - let val - if (instanceType == "url" && nameCustomInstanceInput.validity.valid) { - val = nameCustomInstanceInput.value - if (!exceptionsCustomInstances.url.includes(val)) exceptionsCustomInstances.url.push(val) - } else if (instanceType == "regex") { - val = nameCustomInstanceInput.value - if (val.trim() != "" && !exceptionsCustomInstances.regex.includes(val)) exceptionsCustomInstances.regex.push(val) - } - if (val) { - options = await utils.getOptions() - options.exceptions = exceptionsCustomInstances - browser.storage.local.set({ options }, () => (nameCustomInstanceInput.value = "")) - } - calcExceptionsCustomInstances() -}) diff --git a/src/pages/options/widgets/general.pug b/src/pages/options/widgets/general.pug deleted file mode 100644 index 1388584a..00000000 --- a/src/pages/options/widgets/general.pug +++ /dev/null @@ -1,88 +0,0 @@ -section(class="block-option" id="general_page") - div(class="block block-option") - h1(data-localise="__MSG_general__") General - hr - - div(class="block block-option") - label(data-localise="__MSG_theme__") Theme - select(id="theme" aria-label="select theme") - option(value="detect" data-localise="__MSG_auto__") Auto - option(value="light" data-localise="__MSG_light__") Light - option(value="dark" data-localise="__MSG_dark__") Dark - - div(class="block block-option") - label(data-localise="__MSG_fetchPublicInstances__") Fetch public instances - select(id="fetch-instances" aria-label="Select fetch public instances") - option(value="github") GitHub - option(value="codeberg") Codeberg - option(value="disable" data-localise="__MSG_disable__") Disable - - div(class="block block-option") - label(for='redirectOnlyInIncognito' data-localise="__MSG_redirectOnlyInIncognito__") Redirect Only in Incognito - input(id='redirectOnlyInIncognito' type="checkbox") - - div(class="block block-option") - label(for='bookmarksMenu' data-localise="__MSG_bookmarksMenu__") Bookmarks menu - input(id='bookmarksMenu' type="checkbox") - - div(class="block block-option") - label(data-localise="__MSG_excludeFromRedirecting__") Excluded from redirecting - - form(id="custom-exceptions-instance-form") - div(class="block block-option") - div(class="block" style="padding: 0") - input(id="exceptions-custom-instance" placeholder="https://www.google.com" type="url" aria-label="Add url exception input") - |  - select(id="exceptions-custom-instance-type") - option(value="url") URL - option(value="regex") Regex - |  - button(class="add" id="exceptions-add-instance" type="submit" aria-label="Add the url exception") - svg(xmlns="http://www.w3.org/2000/svg" height="20px" viewBox="0 0 24 24" width="20px" fill="currentColor") - path(d="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z") - - hr - - div(class="checklist" id="exceptions-custom-checklist") - - div(class="buttons") - label(class="button button-inline" id="import_settings_text" for="import-settings") - svg(xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="currentColor") - path(d="M19 19H5V5h7V3H5c-1.11 0-2 .9-2 2v14c0 1.1.89 2 2 2h14c1.1 0 2-.9 2-2v-7h-2v7zM14 3v2h3.59l-9.83 9.83 1.41 1.41L19 6.41V10h2V3h-7z") - |  - x(data-localise="__MSG_importSettings__") Import Settings - input(id="import-settings" type="file" accept=".json" style="display: none") - - |   - - a(class="button button-inline" id="export-settings") - svg(xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="currentColor") - path(d="M10.09 15.59L11.5 17l5-5-5-5-1.41 1.41L12.67 11H3v2h9.67l-2.58 2.59zM19 3H5c-1.11 0-2 .9-2 2v4h2V5h14v14H5v-4H3v4c0 1.1.89 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2z") - |  - x(data-localise="__MSG_exportSettings__") Export Settings - - |   - - button(class="button button-inline" id="export-settings-sync") - svg(xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="currentColor") - path(d="M10.09 15.59L11.5 17l5-5-5-5-1.41 1.41L12.67 11H3v2h9.67l-2.58 2.59zM19 3H5c-1.11 0-2 .9-2 2v4h2V5h14v14H5v-4H3v4c0 1.1.89 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2z") - |  - x() Export Settings to Sync - - |   - - button(class="button button-inline" id="import-settings-sync") - svg(xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="currentColor") - path(d="M19 19H5V5h7V3H5c-1.11 0-2 .9-2 2v14c0 1.1.89 2 2 2h14c1.1 0 2-.9 2-2v-7h-2v7zM14 3v2h3.59l-9.83 9.83 1.41 1.41L19 6.41V10h2V3h-7z") - |  - x(id="import_settings_sync_text") Import Settings from Sync - - |   - - button(class="button button-inline" id="reset-settings") - svg(xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 24 24" height="24px" viewBox="0 0 24 24" width="24px" fill="currentColor") - path(d="M12,5V2L8,6l4,4V7c3.31,0,6,2.69,6,6c0,2.97-2.17,5.43-5,5.91v2.02c3.95-0.49,7-3.85,7-7.93C20,8.58,16.42,5,12,5z") - path(d="M6,13c0-1.65,0.67-3.15,1.76-4.24L6.34,7.34C4.9,8.79,4,10.79,4,13c0,4.08,3.05,7.44,7,7.93v-2.02 C8.17,18.43,6,15.97,6,13z") - x(data-localise="__MSG_resetSettings__") Reset Settings - - script(type="module" src="./widgets/general.js") \ No newline at end of file diff --git a/src/pages/options/widgets/services.pug b/src/pages/options/widgets/services.pug deleted file mode 100644 index e08bb001..00000000 --- a/src/pages/options/widgets/services.pug +++ /dev/null @@ -1,83 +0,0 @@ -each val, service in services - section(class="block-option" id=service+"_page") - div(class="block block-option") - h1 - a(target="_blank" href=services[service].url)=services[service].name - - hr - - div(class="block block-option") - label(for=`${service}-enabled` data-localise="__MSG_enable__") Enable - input(id=`${service}-enabled` type="checkbox") - - div(class="block block-option") - label(for=service data-localise="__MSG_showInPopup__") Show in popup - input(id=service type="checkbox") - - div(id=service+"-opacity") - - div(class="block block-option") - label(for=`${service}-frontend`) - a(class="frontend_name" target="_blank" data-localise="__MSG_frontend__") Frontend - select(id=`${service}-frontend`) - each val, frontend in services[service].frontends - option(value=frontend)=services[service].frontends[frontend].name - - div(class="block block-option" id=service+"-instance-div") - label(for=`${service}-instance`) Instance Type - select(id=`${service}-instance`) - option(value="localhost") localhost - option(value="public") public instances - - div(class="block block-option") - label(for=`${service}-redirectType` data-localise="__MSG_redirectType__") Redirect Type - select(id=`${service}-redirectType`) - - - div(id=`${service}-embedFrontend-div` class="block block-option") - label(for=`${service}-embedFrontend` data-localise="__MSG_embedFrontend__") Embed Frontend - select(id=`${service}-embedFrontend`) - each val, frontend in services[service].frontends - if services[service].frontends[frontend].embeddable && services[service].frontends[frontend].instanceList - option(value=frontend)=services[service].frontends[frontend].name - - - div(class="block block-option") - label(for=`${service}-unsupportedUrls` data-localise="__MSG_unsupportedIframesHandling__") Unsupported iframes handling - select(id=`${service}-unsupportedUrls`) - option(value="bypass") bypass - option(value="block") block - - if (service == 'search') - div(class="block block-option") - label Set LibRedirect as Default Search Engine. For how to do in chromium browsers, click here. - - - each val, frontend in services[service].frontends - if services[service].frontends[frontend].instanceList - div(id=frontend dir="ltr") - hr - div(dir="auto" class="block block-option") - label(data-localise="__MSG_addYourFavoriteInstances__") Add your favorite instances - - form(class="custom-instance-form") - div(class="block block-option") - input(class="custom-instance" type="url" placeholder="https://instance.com" aria-label="Add instance input") - button(class="add add-instance" type="submit" aria-label="Add the instance") - 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") - - div(class="ping block") - button(class="button button-inline" id=`ping-${frontend}`) - svg(xmlns="http://www.w3.org/2000/svg" height="20px" viewBox="0 0 24 24" width="20px" fill="currentColor") - path(d="M10.45 15.5q.6.6 1.55.587.95-.012 1.4-.687L19 7l-8.4 5.6q-.675.45-.712 1.375-.038.925.562 1.525ZM12 4q1.475 0 2.838.412Q16.2 4.825 17.4 5.65l-1.9 1.2q-.825-.425-1.712-.637Q12.9 6 12 6 8.675 6 6.338 8.337 4 10.675 4 14q0 1.05.287 2.075Q4.575 17.1 5.1 18h13.8q.575-.95.838-1.975Q20 15 20 13.9q0-.9-.212-1.75-.213-.85-.638-1.65l1.2-1.9q.75 1.175 1.188 2.5.437 1.325.462 2.75.025 1.425-.325 2.725-.35 1.3-1.025 2.475-.275.45-.75.7-.475.25-1 .25H5.1q-.525 0-1-.25t-.75-.7q-.65-1.125-1-2.387Q2 15.4 2 14q0-2.075.788-3.888.787-1.812 2.15-3.175Q6.3 5.575 8.125 4.787 9.95 4 12 4Zm.175 7.825Z") - |  - x() Ping instances - - each val, network in networks - div(class=network) - div(class="checklist") - if (network == 'clearnet') - div(class="block block-option loading") Loading... \ No newline at end of file diff --git a/src/pages/src/App.svelte b/src/pages/src/App.svelte new file mode 100644 index 00000000..8cceb5d1 --- /dev/null +++ b/src/pages/src/App.svelte @@ -0,0 +1,118 @@ + + +{#if _options && _config} +
+ + {#if _page == "general"} + + {:else if _page == "services"} + + {/if} +
+{:else} +

Loading...

+{/if} + + diff --git a/src/pages/src/General/Exceptions.svelte b/src/pages/src/General/Exceptions.svelte new file mode 100644 index 00000000..e43afaf6 --- /dev/null +++ b/src/pages/src/General/Exceptions.svelte @@ -0,0 +1,108 @@ + + +
+ + + + +
+ { + if (e.key === "Enter") addException() + }} + /> + + + + + + + + + +
diff --git a/src/pages/src/Services/Instances.svelte b/src/pages/src/Services/Instances.svelte new file mode 100644 index 00000000..3c9b1ccf --- /dev/null +++ b/src/pages/src/Services/Instances.svelte @@ -0,0 +1,234 @@ + + +{#if serviceConf.frontends[serviceOptions.frontend].instanceList && redirects && blacklist} +
+
+
+ +
+ + + + + + + { + if (e.key === "Enter") addInstance() + }} + /> + + + + {#each _options[serviceOptions.frontend] as instance} + + + {instance} + {#if isCustomInstance(instance)} + custom + {/if} + {#if pingCache && pingCache[instance]} + {pingCache[instance].value} + {/if} + + + +
+ {/each} + + + {#if redirects !== "disabled" && blacklist !== "disabled"} + {#if redirects[serviceOptions.frontend] && redirects[serviceOptions.frontend]["clearnet"]} + {#each Object.entries(_config.networks) as [networkName, network]} + {#if redirects[serviceOptions.frontend] && redirects[serviceOptions.frontend][networkName]} + +
+ {#each redirects[serviceOptions.frontend][networkName] as instance} + + + {instance} + {#if blacklist.cloudflare.includes(instance)} + + cloudflare + + {/if} + {#if _options[serviceOptions.frontend].includes(instance)} + chosen + {/if} + {#if pingCache && pingCache[instance]} + {pingCache[instance].value} + {/if} + + + +
+ {/each} + {/if} + {/each} + {:else} + + {/if} + {/if} +
+{/if} + + diff --git a/src/pages/src/Services/RedirectType.svelte b/src/pages/src/Services/RedirectType.svelte new file mode 100644 index 00000000..92b8c6d1 --- /dev/null +++ b/src/pages/src/Services/RedirectType.svelte @@ -0,0 +1,84 @@ + + + { + serviceOptions.redirectType = e.target.options[e.target.options.selectedIndex].value + options.set(_options) + }} + {values} +/> + +{#if serviceConf.frontends[frontendName].desktopApp && serviceOptions.redirectType != "main_frame"} + { + serviceOptions.embedFrontend = e.target.options[e.target.options.selectedIndex].value + options.set(_options) + }} + values={embeddableFrontends} + /> +{/if} diff --git a/src/pages/src/Services/Services.svelte b/src/pages/src/Services/Services.svelte new file mode 100644 index 00000000..b5d9285c --- /dev/null +++ b/src/pages/src/Services/Services.svelte @@ -0,0 +1,119 @@ + + +
+ + + ({ + value: frontendId, + name: frontend.name, + })), + ]} + onChange={e => { + serviceOptions.frontend = e.target.options[e.target.options.selectedIndex].value + options.set(_options) + }} + /> + + + + + { + serviceOptions.unsupportedUrls = e.target.options[e.target.options.selectedIndex].value + options.set(_options) + }} + values={[ + { value: "bypass", name: "Bypass" }, + { value: "block", name: "Block" }, + ]} + /> + + {#if selectedService == "search"} +
+ Set LibRedirect as Default Search Engine. For how to do in chromium browsers, click + here. +
+ {/if} + + +
+
diff --git a/src/pages/src/Sidebar.svelte b/src/pages/src/Sidebar.svelte new file mode 100644 index 00000000..be59a731 --- /dev/null +++ b/src/pages/src/Sidebar.svelte @@ -0,0 +1,43 @@ + + + + + diff --git a/src/pages/src/components/Button.svelte b/src/pages/src/components/Button.svelte new file mode 100644 index 00000000..3405c2ea --- /dev/null +++ b/src/pages/src/components/Button.svelte @@ -0,0 +1,29 @@ + + + diff --git a/src/pages/src/components/Checkbox.svelte b/src/pages/src/components/Checkbox.svelte new file mode 100644 index 00000000..f5245045 --- /dev/null +++ b/src/pages/src/components/Checkbox.svelte @@ -0,0 +1,52 @@ + + + + + diff --git a/src/pages/src/components/Input.svelte b/src/pages/src/components/Input.svelte new file mode 100644 index 00000000..d963233c --- /dev/null +++ b/src/pages/src/components/Input.svelte @@ -0,0 +1,41 @@ + + + + + diff --git a/src/pages/src/components/Label.svelte b/src/pages/src/components/Label.svelte new file mode 100644 index 00000000..39930cd1 --- /dev/null +++ b/src/pages/src/components/Label.svelte @@ -0,0 +1,18 @@ + + + + + diff --git a/src/pages/src/components/Row.svelte b/src/pages/src/components/Row.svelte new file mode 100644 index 00000000..4f23cffa --- /dev/null +++ b/src/pages/src/components/Row.svelte @@ -0,0 +1,12 @@ +
+ +
+ + diff --git a/src/pages/src/components/RowCheckbox.svelte b/src/pages/src/components/RowCheckbox.svelte new file mode 100644 index 00000000..b7ccab93 --- /dev/null +++ b/src/pages/src/components/RowCheckbox.svelte @@ -0,0 +1,14 @@ + + + + + + diff --git a/src/pages/src/components/RowSelect.svelte b/src/pages/src/components/RowSelect.svelte new file mode 100644 index 00000000..d685803e --- /dev/null +++ b/src/pages/src/components/RowSelect.svelte @@ -0,0 +1,19 @@ + + + + + + {#each values as option} + + {/each} + + + diff --git a/src/pages/src/icons/AboutIcon.svelte b/src/pages/src/icons/AboutIcon.svelte new file mode 100644 index 00000000..e113dd68 --- /dev/null +++ b/src/pages/src/icons/AboutIcon.svelte @@ -0,0 +1,11 @@ + diff --git a/src/pages/src/icons/AddIcon.svelte b/src/pages/src/icons/AddIcon.svelte new file mode 100644 index 00000000..ab26f078 --- /dev/null +++ b/src/pages/src/icons/AddIcon.svelte @@ -0,0 +1,3 @@ + + + diff --git a/src/pages/src/icons/CloseIcon.svelte b/src/pages/src/icons/CloseIcon.svelte new file mode 100644 index 00000000..ddfb29cb --- /dev/null +++ b/src/pages/src/icons/CloseIcon.svelte @@ -0,0 +1,3 @@ + diff --git a/src/pages/src/icons/ExportIcon.svelte b/src/pages/src/icons/ExportIcon.svelte new file mode 100644 index 00000000..196726a8 --- /dev/null +++ b/src/pages/src/icons/ExportIcon.svelte @@ -0,0 +1,5 @@ + + + diff --git a/src/pages/src/icons/GeneralIcon.svelte b/src/pages/src/icons/GeneralIcon.svelte new file mode 100644 index 00000000..b9429021 --- /dev/null +++ b/src/pages/src/icons/GeneralIcon.svelte @@ -0,0 +1,13 @@ + + + diff --git a/src/pages/src/icons/ImportIcon.svelte b/src/pages/src/icons/ImportIcon.svelte new file mode 100644 index 00000000..f022b4f0 --- /dev/null +++ b/src/pages/src/icons/ImportIcon.svelte @@ -0,0 +1,5 @@ + + + \ No newline at end of file diff --git a/src/pages/src/icons/PingIcon.svelte b/src/pages/src/icons/PingIcon.svelte new file mode 100644 index 00000000..8fcfe27b --- /dev/null +++ b/src/pages/src/icons/PingIcon.svelte @@ -0,0 +1,5 @@ + + diff --git a/src/pages/src/icons/ResetIcon.svelte b/src/pages/src/icons/ResetIcon.svelte new file mode 100644 index 00000000..0900aef8 --- /dev/null +++ b/src/pages/src/icons/ResetIcon.svelte @@ -0,0 +1,15 @@ + + + + diff --git a/src/pages/src/icons/ServicesIcon.svelte b/src/pages/src/icons/ServicesIcon.svelte new file mode 100644 index 00000000..ec24259b --- /dev/null +++ b/src/pages/src/icons/ServicesIcon.svelte @@ -0,0 +1,11 @@ + diff --git a/src/pages/src/main.js b/src/pages/src/main.js new file mode 100644 index 00000000..c4012f4a --- /dev/null +++ b/src/pages/src/main.js @@ -0,0 +1,7 @@ +import App from "./App.svelte" + +const app = new App({ + target: document.body, +}) + +export default app diff --git a/src/pages/src/stores.js b/src/pages/src/stores.js new file mode 100644 index 00000000..782f6064 --- /dev/null +++ b/src/pages/src/stores.js @@ -0,0 +1,5 @@ +import { writable } from "svelte/store" + +export const options = writable(null) +export const config = writable(null) +export const page = writable("general") diff --git a/src/pages/widgets/head.pug b/src/pages/widgets/head.pug deleted file mode 100644 index 0c6e9fdb..00000000 --- a/src/pages/widgets/head.pug +++ /dev/null @@ -1,7 +0,0 @@ -head - meta(charset="utf-8") - meta(name="viewport" content="width=device-width, initial-scale=1") - link(rel="icon" type="image/x-icon" href="../../../assets/images/libredirect.svg") - link(href="../stylesheets/styles.css" rel="stylesheet") - title Settings - script(type="module" src="./init.js") \ No newline at end of file diff --git a/src/pages/widgets/links.pug b/src/pages/widgets/links.pug deleted file mode 100644 index 318c72f9..00000000 --- a/src/pages/widgets/links.pug +++ /dev/null @@ -1,22 +0,0 @@ -section(class="links" id="links") - div(class="title") - a(href="#general") - include /src/assets/images/general-icon.svg - span(data-localise="__MSG_general__") General - - each val, key in services - div(class="title" id=`${key}-link`) - a(href="#"+key) - if services[key].imageType == 'svgMono' - img(class='dark' src=`/assets/images/${key}-icon.svg`) - img(class='light' src=`/assets/images/${key}-icon-light.svg`) - else - img(src=`/assets/images/${key}-icon.${services[key].imageType}`) - span=services[key].name - - div(class="title") - a(target="_blank" href="https://libredirect.github.io") - img(class='dark' src="/assets/images/about-icon.svg") - img(class='light' src="/assets/images/about-icon-light.svg") - - span(data-localise="__MSG_about__") About -- cgit 1.4.1