From cf8216da008128bc70bf547abcf7290e9a760c66 Mon Sep 17 00:00:00 2001 From: ManeraKai Date: Sun, 21 Jul 2024 21:22:09 +0300 Subject: Formatting wiht prettier --- src/pages/messages/no_instance.html | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) (limited to 'src/pages/messages') diff --git a/src/pages/messages/no_instance.html b/src/pages/messages/no_instance.html index 76ec19cf..358ff506 100644 --- a/src/pages/messages/no_instance.html +++ b/src/pages/messages/no_instance.html @@ -1,26 +1,24 @@ - + - - - - - - + + + + + No instances found - + - +
-

You have no instance selected for this frontend

+

You have no instance selected for this frontend

- - - \ No newline at end of file + + -- cgit 1.4.1 From dfa6c8e570e3030c382ad045156a0b12fd3ba368 Mon Sep 17 00:00:00 2001 From: ManeraKai Date: Sun, 25 Aug 2024 21:31:24 +0300 Subject: Fixing and improving redirect core --- src/assets/javascripts/services.js | 29 ++++++--- src/pages/background/background.js | 97 +++++++++++++--------------- src/pages/messages/no_instance.html | 2 +- src/pages/popup_src/Buttons.svelte | 8 +-- src/pages/popup_src/components/Switch.svelte | 2 +- 5 files changed, 70 insertions(+), 68 deletions(-) (limited to 'src/pages/messages') diff --git a/src/assets/javascripts/services.js b/src/assets/javascripts/services.js index d3afd437..e93b637c 100644 --- a/src/assets/javascripts/services.js +++ b/src/assets/javascripts/services.js @@ -55,9 +55,9 @@ function regexArray(service, url, config, options, frontend) { * @param {URL} originUrl * @param {boolean} forceRedirection */ -async function redirectAsync(url, type, originUrl, documentUrl, forceRedirection) { +async function redirectAsync(url, type, originUrl, documentUrl, incognito, forceRedirection) { await init() - return redirect(url, type, originUrl, documentUrl, forceRedirection) + return redirect(url, type, originUrl, documentUrl, incognito, forceRedirection) } /** @@ -569,21 +569,22 @@ function rewrite(url, originUrl, frontend, randomInstance) { * @param {URL} url * @param {string} type * @param {URL} originUrl + * @param {URL} documentUrl + * @param {boolean} incognito * @param {boolean} forceRedirection * @returns {string | undefined} */ -function redirect(url, type, originUrl, documentUrl, forceRedirection, incognito) { +function redirect(url, type, originUrl, documentUrl, incognito, forceRedirection) { if (type != "main_frame" && type != "sub_frame" && type != "image") return let randomInstance let frontend if (!forceRedirection && options.redirectOnlyInIncognito == true && !incognito) return for (const service in config.services) { if (!forceRedirection && !options[service].enabled) continue + if (!forceRedirection && options[service].redirectOnlyInIncognito == true && !incognito) continue frontend = options[service].frontend - if (options[service].redirectOnlyInIncognito == true && !incognito) continue - if ( config.services[service].frontends[frontend].desktopApp && type != "main_frame" && @@ -613,11 +614,11 @@ function redirect(url, type, originUrl, documentUrl, forceRedirection, incognito let instanceList = options[frontend] if (instanceList === undefined) break - if (instanceList.length === 0) return null + if (instanceList.length === 0) return "https://libredirect.invalid" if (originUrl && instanceList.includes(originUrl.origin)) { - if (type != "main_frame") return null - else return "BYPASSTAB" + if (type == "main_frame") return "BYPASSTAB" + else return null } randomInstance = utils.getRandomInstance(instanceList) @@ -937,10 +938,18 @@ function isException(url) { for (let item of exceptions.url) { item = new URL(item) item = item.href.replace(/^http:\/\//, "https://") - if (item == url.href) return true + if (item == url.href) { + return true + } + } + } + if (exceptions.regex) { + for (const item of exceptions.regex) { + if (new RegExp(item).test(url.href)) { + return true + } } } - if (exceptions.regex) for (const item of exceptions.regex) if (new RegExp(item).test(url.href)) return true } return false } diff --git a/src/pages/background/background.js b/src/pages/background/background.js index 16ca38d7..f41cb55e 100644 --- a/src/pages/background/background.js +++ b/src/pages/background/background.js @@ -19,58 +19,71 @@ browser.runtime.onInstalled.addListener(async details => { } }) +// true to redirect, false to bypass let tabIdRedirects = {} // true == Always redirect, false == Never redirect, null/undefined == follow options for services browser.webRequest.onBeforeRequest.addListener( details => { + const old_href = details.url const url = new URL(details.url) - const old_href = url.href if (new RegExp(/^chrome-extension:\/{2}.*\/instances\/.*.json$/).test(url.href) && details.type == "xmlhttprequest") - return - let originUrl - try { - if (details.originUrl) originUrl = new URL(details.originUrl) - } catch { return null - } - let documentUrl - try { if (details.documentUrl) documentUrl = new URL(details.documentUrl) } - catch (error) { return null } + + // if url is previously bypassed if (tabIdRedirects[details.tabId] == false) return null - let newUrl = servicesHelper.redirect(url, details.type, originUrl, documentUrl, tabIdRedirects[details.tabId], details.incognito) + // Bypass embeds from excepted urls if ( details.frameAncestors && - details.frameAncestors.length > 0 && + details.frameAncestors.length >= 1 && servicesHelper.isException(new URL(details.frameAncestors[0].url)) ) - newUrl = null + return null if (servicesHelper.isException(url)) { - if (details.type == "main_frame") newUrl = "BYPASSTAB" - else return null + if (details.type == "main_frame") { + console.log(`Bypassing ${details.tabId} ${url}`) + tabIdRedirects[details.tabId] = false + } + return null + } + + let originUrl + let documentUrl + try { + if (details.originUrl) originUrl = new URL(details.originUrl) + if (details.documentUrl) documentUrl = new URL(details.documentUrl) + } catch { + return null } + const newUrl = servicesHelper.redirect( + url, + details.type, + originUrl, + documentUrl, + details.incognito, + tabIdRedirects[details.tabId] + ) + if (!newUrl) { - const match = url.href.match(/^https?:\/{2}.*\.libredirect\.invalid.*/) + const match = url.href.match(/^https?:\/{2}(.*\.)?libredirect\.invalid.*/) if (match) { - browser.tabs.update({ - url: browser.runtime.getURL(`/pages/messages/no_instance.html`), - }) + browser.tabs.update({ url: browser.runtime.getURL(`/pages/messages/no_instance.html`) }) } } + if (newUrl === "CANCEL") { + console.log(`Cancelling ${url}`) + return { cancel: true } + } + if (newUrl === "BYPASSTAB") { + console.log(`Bypassing ${details.tabId} ${url}`) + tabIdRedirects[details.tabId] = false + return null + } if (newUrl) { - if (newUrl === "CANCEL") { - console.log(`Canceled ${url}`) - return { cancel: true } - } - if (newUrl === "BYPASSTAB") { - console.log(`Bypassed ${details.tabId} ${url}`) - if (tabIdRedirects[details.tabId] != false) tabIdRedirects[details.tabId] = false - return null - } console.log("Redirecting", old_href, "=>", newUrl) return { redirectUrl: newUrl } } @@ -105,7 +118,7 @@ browser.runtime.getPlatformInfo(r => { browser.tabs.query({ active: true, currentWindow: true }, async tabs => { if (tabs[0].url) { const url = new URL(tabs[0].url) - const newUrl = servicesHelper.redirect(url, "main_frame", null, null, true) + const newUrl = servicesHelper.redirect(url, "main_frame", null, null, false, true) if (newUrl) { browser.tabs.update(tabs[0].id, { url: newUrl }, () => { tabIdRedirects[tabs[0].id] = true @@ -211,7 +224,7 @@ browser.runtime.getPlatformInfo(r => { browser.tabs.query({ active: true, currentWindow: true }, async tabs => { if (tabs[0].url) { const url = new URL(tabs[0].url) - const newUrl = servicesHelper.redirect(url, "main_frame", null, null, true) + const newUrl = servicesHelper.redirect(url, "main_frame", null, null, false, true) if (newUrl) { browser.tabs.update(tabs[0].id, { url: newUrl }, () => { tabIdRedirects[tabs[0].id] = true @@ -228,7 +241,7 @@ browser.runtime.getPlatformInfo(r => { case "redirectLink": case "redirectLinkInNewTab": { const url = new URL(info.linkUrl) - const newUrl = servicesHelper.redirect(url, "main_frame", null, null, true) + const newUrl = servicesHelper.redirect(url, "main_frame", null, null, false, true) if (newUrl) { if (info.menuItemId == "redirectLink") browser.tabs.update({ url: newUrl }) else browser.tabs.create({ url: newUrl }) @@ -276,7 +289,7 @@ browser.runtime.getPlatformInfo(r => { case "redirectBookmarkInNewTab": browser.bookmarks.get(info.bookmarkId, bookmarks => { const url = new URL(bookmarks[0].url) - const newUrl = servicesHelper.redirect(url, "main_frame", null, null, true) + const newUrl = servicesHelper.redirect(url, "main_frame", null, null, false, true) if (newUrl) { if (info.menuItemId == "redirectBookmark") browser.tabs.update({ url: newUrl }) else browser.tabs.create({ url: newUrl }) @@ -316,23 +329,3 @@ browser.runtime.getPlatformInfo(r => { }) } }) - -browser.runtime.onMessage.addListener((request, sender, sendResponse) => { - if (request == "reverseTab") { - browser.tabs.query({ active: true, currentWindow: true }, async tabs => { - if (tabs[0].url) { - const url = new URL(tabs[0].url) - const newUrl = await servicesHelper.reverse(url) - if (newUrl) browser.tabs.update(tabs[0].id, { url: newUrl }, () => (tabIdRedirects[tabs[0].id] = false)) - } - }) - } else if (request == "redirectTab") { - browser.tabs.query({ active: true, currentWindow: true }, async tabs => { - if (tabs[0].url) { - const url = new URL(tabs[0].url) - const newUrl = servicesHelper.redirect(url, "main_frame", null, null, true) - if (newUrl) browser.tabs.update(tabs[0].id, { url: newUrl }, () => (tabIdRedirects[tabs[0].id] = true)) - } - }) - } -}) diff --git a/src/pages/messages/no_instance.html b/src/pages/messages/no_instance.html index 358ff506..55e5fac7 100644 --- a/src/pages/messages/no_instance.html +++ b/src/pages/messages/no_instance.html @@ -18,7 +18,7 @@
-

You have no instance selected for this frontend

+

LibRedirect: You have no instance selected for this frontend

diff --git a/src/pages/popup_src/Buttons.svelte b/src/pages/popup_src/Buttons.svelte index e53a64b7..c0644ff7 100644 --- a/src/pages/popup_src/Buttons.svelte +++ b/src/pages/popup_src/Buttons.svelte @@ -33,7 +33,7 @@ url = new URL(tabs[0].url) servicesHelper.switchInstance(url).then(r => (switchInstance = r)) servicesHelper.reverse(url).then(r => (redirectToOriginal = r)) - servicesHelper.redirectAsync(url, "main_frame", null, true).then(r => (redirect = r)) + servicesHelper.redirectAsync(url, "main_frame", null, null, false, true).then(r => (redirect = r)) servicesHelper.computeService(url).then(r => (currentService = r)) } }) @@ -44,7 +44,7 @@ { - browser.runtime.sendMessage("redirectTab", () => { + browser.tabs.update({ url: redirect }, () => { window.close() }) }} @@ -58,7 +58,7 @@ - browser.tabs.update({ url: await servicesHelper.switchInstance(url) }, () => { + browser.tabs.update({ url: switchInstance }, () => { window.close() })} > @@ -75,7 +75,7 @@ - browser.runtime.sendMessage("reverseTab", () => { + browser.tabs.update({ url: redirectToOriginal }, () => { window.close() })} > diff --git a/src/pages/popup_src/components/Switch.svelte b/src/pages/popup_src/components/Switch.svelte index f76e74be..98f765b7 100644 --- a/src/pages/popup_src/components/Switch.svelte +++ b/src/pages/popup_src/components/Switch.svelte @@ -30,7 +30,7 @@ class="interactive margin margin_{document.body.dir}" on:keydown={null} on:click={() => - browser.tabs.create({ url: browser.runtime.getURL(_config.services[serviceKey].url) }, () => { + browser.tabs.create({ url: _config.services[serviceKey].url }, () => { window.close() })} > -- cgit 1.4.1 From 97d390737e7e953fe1e6559e41963c547b31afa0 Mon Sep 17 00:00:00 2001 From: ManeraKai Date: Thu, 29 Aug 2024 13:36:21 +0300 Subject: Improving popup messages --- .gitignore | 3 +- package.json | 2 +- rollup.config.js | 4 + src/assets/javascripts/services.js | 27 +++-- src/assets/javascripts/utils.js | 53 +++++++++- src/config.json | 2 +- src/pages/background/background.js | 21 +++- src/pages/components/Button.svelte | 10 +- src/pages/messages/index.html | 14 +++ src/pages/messages/no_instance.html | 24 ----- src/pages/messages_src/App.svelte | 132 ++++++++++++++++++++++++ src/pages/messages_src/main.js | 7 ++ src/pages/messages_src/stores.js | 5 + src/pages/options_src/App.svelte | 44 +------- src/pages/options_src/Services/Instances.svelte | 13 +-- src/pages/popup_src/App.svelte | 42 +------- 16 files changed, 266 insertions(+), 137 deletions(-) create mode 100644 src/pages/messages/index.html delete mode 100644 src/pages/messages/no_instance.html create mode 100644 src/pages/messages_src/App.svelte create mode 100644 src/pages/messages_src/main.js create mode 100644 src/pages/messages_src/stores.js (limited to 'src/pages/messages') diff --git a/.gitignore b/.gitignore index f9768f05..c10438e0 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ package-lock.json .vscode pnpm-lock.yaml src/pages/options/build -src/pages/popup/build \ No newline at end of file +src/pages/popup/build +src/pages/messages/build \ No newline at end of file diff --git a/package.json b/package.json index 7b9fbd11..83705623 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "build": "web-ext build -i pages/options_src -i pages/popup_src pages/icons -i pages/popup_src -i pages/components", "build_chromium": "brave-browser --pack-extension=src/ --pack-extension-key=src.pem", "test": "web-ext lint", - "html": "rollup -c --config-popup && rollup -c --config-options" + "html": "rollup -c --config-popup && rollup -c --config-options && rollup -c --config-messages" }, "repository": { "type": "git", diff --git a/rollup.config.js b/rollup.config.js index 5a2ce362..0bad0c97 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -14,6 +14,10 @@ if (process.argv.includes("--config-options")) { input = "src/pages/popup_src/main.js" output = "src/pages/popup/build/bundle.js" } +else if (process.argv.includes("--config-messages")) { + input = "src/pages/messages_src/main.js" + output = "src/pages/messages/build/bundle.js" +} export default { input, diff --git a/src/assets/javascripts/services.js b/src/assets/javascripts/services.js index ee48a1b9..d1c12375 100644 --- a/src/assets/javascripts/services.js +++ b/src/assets/javascripts/services.js @@ -610,18 +610,20 @@ function redirect(url, type, originUrl, documentUrl, incognito, forceRedirection } let instanceList = options[frontend] - if (instanceList === undefined) break - if (instanceList.length === 0) return "https://libredirect.invalid" + if (instanceList === undefined) break // should not happen if settings are correct + + if (config.services[service].frontends[frontend].localhost && options[service].instance == "localhost") { + randomInstance = `http://${frontend}.localhost:8080` + } else if (instanceList.length === 0) { + return `https://no-instance.libredirect.invalid?frontend=${encodeURIComponent(frontend)}&url=${encodeURIComponent(url.href)}` + } else { + randomInstance = utils.getRandomInstance(instanceList) + } if (originUrl && instanceList.includes(originUrl.origin)) { if (type == "main_frame") return "BYPASSTAB" else return null } - - randomInstance = utils.getRandomInstance(instanceList) - if (config.services[service].frontends[frontend].localhost && options[service].instance == "localhost") { - randomInstance = `http://${frontend}.localhost:8080` - } break } if (!frontend) return @@ -636,7 +638,7 @@ function redirect(url, type, originUrl, documentUrl, incognito, forceRedirection * @param {URL} documentUrl * @param {boolean} incognito * @param {boolean} forceRedirection - * @returns {string | undefined} + * @returns {Promise} */ async function redirectAsync(url, type, originUrl, documentUrl, incognito, forceRedirection) { await init() @@ -645,9 +647,8 @@ async function redirectAsync(url, type, originUrl, documentUrl, incognito, force /** * @param {URL} url - * @param {*} returnFrontend */ -function computeService(url, returnFrontend) { +function computeService(url) { return new Promise(async resolve => { const config = await utils.getConfig() const options = await utils.getOptions() @@ -658,9 +659,7 @@ function computeService(url, returnFrontend) { } else { for (const frontend in config.services[service].frontends) { if (all(service, frontend, options, config).includes(utils.protocolHost(url))) { - if (returnFrontend) resolve([service, frontend, utils.protocolHost(url)]) - else resolve(service) - return + return resolve(service) } } } @@ -768,7 +767,7 @@ async function reverse(url) { } const defaultInstances = { - invidious: ["https://inv.vern.cc"], + // invidious: ["https://inv.vern.cc"], materialious: ["https://app.materialio.us"], viewtube: ["https://viewtube.io"], piped: ["https://pipedapi-libre.kavin.rocks"], diff --git a/src/assets/javascripts/utils.js b/src/assets/javascripts/utils.js index 36271a8a..e5b8ba46 100644 --- a/src/assets/javascripts/utils.js +++ b/src/assets/javascripts/utils.js @@ -24,7 +24,7 @@ function getNextInstance(currentInstanceUrl, instances) { * @param {URL} url */ function protocolHost(url) { - url.pathname = url.pathname.replace(/\/$/, ''); + url.pathname = url.pathname.replace(/\/$/, "") if (url.username && url.password) return `${url.protocol}//${url.username}:${url.password}@${url.host}${url.pathname}` // workaround @@ -211,6 +211,55 @@ function convertMapCentre(url) { return { zoom, lon, lat } } +export function randomInstances(clearnet, n) { + let instances = [] + if (n > clearnet.length) n = clearnet.length + for (let i = 0; i < n; i++) { + const randomNumber = Math.floor(Math.random() * clearnet.length) + const randomInstance = clearnet[randomNumber] + instances.push(randomInstance) + } + return instances +} +export function style(options, window) { + const vars = cssVariables(options, window) + return `--text: ${vars.text}; + --bg-main: ${vars.bgMain}; + --bg-secondary: ${vars.bgSecondary}; + --active: ${vars.active}; + --danger: ${vars.danger}; + --light-grey: ${vars.lightGrey};` +} + +function cssVariables(options, window) { + const dark = { + text: "#fff", + bgMain: "#121212", + bgSecondary: "#202020", + active: "#fbc117", + danger: "#f04141", + lightGrey: "#c3c3c3", + } + + const light = { + text: "black", + bgMain: "white", + bgSecondary: "#e4e4e4", + active: "#fb9817", + danger: "#f04141", + lightGrey: "#c3c3c3", + } + if (options.theme == "dark") { + return dark + } else if (options.theme == "light") { + return light + } else if (window.matchMedia("(prefers-color-scheme: dark)").matches) { + return dark + } else { + return light + } +} + export default { getRandomInstance, getNextInstance, @@ -225,4 +274,6 @@ export default { getQuery, prefsEncoded, convertMapCentre, + randomInstances, + style, } diff --git a/src/config.json b/src/config.json index 6ef3dbe6..045d7f20 100644 --- a/src/config.json +++ b/src/config.json @@ -143,7 +143,7 @@ ], "name": "YouTube", "options": { - "enabled": false, + "enabled": true, "redirectType": "main_frame", "frontend": "invidious", "embedFrontend": "invidious", diff --git a/src/pages/background/background.js b/src/pages/background/background.js index f41cb55e..db7ad087 100644 --- a/src/pages/background/background.js +++ b/src/pages/background/background.js @@ -67,10 +67,25 @@ browser.webRequest.onBeforeRequest.addListener( tabIdRedirects[details.tabId] ) + if (newUrl && newUrl.startsWith("https://no-instance.libredirect.invalid")) { + const url = new URL(newUrl) + const frontend = url.searchParams.get("frontend") + const oldUrl = new URL(url.searchParams.get("url")) + + browser.tabs.update({ + url: browser.runtime.getURL( + `/pages/messages/index.html?message=no_instance&url=${encodeURIComponent(oldUrl)}&frontend=${encodeURIComponent(frontend)}` + ), + }) + } + if (!newUrl) { - const match = url.href.match(/^https?:\/{2}(.*\.)?libredirect\.invalid.*/) - if (match) { - browser.tabs.update({ url: browser.runtime.getURL(`/pages/messages/no_instance.html`) }) + if (url.href.match(/^https?:\/{2}(.*\.)?libredirect\.invalid.*/)) { + browser.tabs.update({ + url: browser.runtime.getURL( + `/pages/messages/index.html?message=disabled&url=${encodeURIComponent(url.href)}` + ), + }) } } diff --git a/src/pages/components/Button.svelte b/src/pages/components/Button.svelte index 8836b47e..6ae2ba61 100644 --- a/src/pages/components/Button.svelte +++ b/src/pages/components/Button.svelte @@ -19,11 +19,17 @@ padding: 10px; } - button:hover { + button:hover:enabled { color: var(--active); } - button:active { + button:active:enabled { transform: translateY(1px); } + + button:disabled { + cursor: not-allowed; + opacity: 0.5; + } + diff --git a/src/pages/messages/index.html b/src/pages/messages/index.html new file mode 100644 index 00000000..8701c152 --- /dev/null +++ b/src/pages/messages/index.html @@ -0,0 +1,14 @@ + + + + + + + Settings + + + + + + + diff --git a/src/pages/messages/no_instance.html b/src/pages/messages/no_instance.html deleted file mode 100644 index 55e5fac7..00000000 --- a/src/pages/messages/no_instance.html +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - - No instances found - - - - -
-

LibRedirect: You have no instance selected for this frontend

-
- - diff --git a/src/pages/messages_src/App.svelte b/src/pages/messages_src/App.svelte new file mode 100644 index 00000000..b08a143b --- /dev/null +++ b/src/pages/messages_src/App.svelte @@ -0,0 +1,132 @@ + + +{#if _options && _config} +
+ {#if window.location.search.includes("message=disabled")} +
+

You disabled redirections for this service

+ +
+ {:else if window.location.search.includes("message=no_instance")} +
+

You have no instance selected for this frontend

+ +
+ {/if} +
+{:else} +

Loading...

+{/if} + + diff --git a/src/pages/messages_src/main.js b/src/pages/messages_src/main.js new file mode 100644 index 00000000..c4012f4a --- /dev/null +++ b/src/pages/messages_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/messages_src/stores.js b/src/pages/messages_src/stores.js new file mode 100644 index 00000000..782f6064 --- /dev/null +++ b/src/pages/messages_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/options_src/App.svelte b/src/pages/options_src/App.svelte index 565aacef..1c4830bf 100644 --- a/src/pages/options_src/App.svelte +++ b/src/pages/options_src/App.svelte @@ -2,7 +2,7 @@ const browser = window.browser || window.chrome import General from "./General/General.svelte" - import url from './url' + import url from "./url" import utils from "../../assets/javascripts/utils.js" import { onDestroy } from "svelte" import servicesHelper from "../../assets/javascripts/services.js" @@ -37,51 +37,15 @@ config.set(await utils.getConfig()) }) - const dark = { - text: "#fff", - bgMain: "#121212", - bgSecondary: "#202020", - active: "#fbc117", - danger: "#f04141", - lightGrey: "#c3c3c3", - } - const light = { - text: "black", - bgMain: "white", - bgSecondary: "#e4e4e4", - active: "#fb9817", - danger: "#f04141", - lightGrey: "#c3c3c3", - } - let cssVariables - $: if (_options) { - if (_options.theme == "dark") { - cssVariables = dark - } else if (_options.theme == "light") { - cssVariables = light - } else if (window.matchMedia("(prefers-color-scheme: dark)").matches) { - cssVariables = dark - } else { - cssVariables = light - } - } + let style + $: if (_options) style = utils.style(_options, window) const dir = ["ar", "iw", "ku", "fa", "ur"].includes(browser.i18n.getUILanguage()) ? "rtl" : "ltr" document.body.dir = dir {#if _options && _config} -
+
{#if !$url.hash || $url.hash == "#general"} diff --git a/src/pages/options_src/Services/Instances.svelte b/src/pages/options_src/Services/Instances.svelte index 32425b80..4e5d1e7d 100644 --- a/src/pages/options_src/Services/Instances.svelte +++ b/src/pages/options_src/Services/Instances.svelte @@ -64,16 +64,9 @@ pingCache[instance] = colorTime(time) } } - function randomInstances(n) { - let instances = [] - for (let i = 0; i < n; i++) { - instances.push(redirects[selectedFrontend]["clearnet"][Math.floor(Math.random() * allInstances.length)]) - } - return instances - } async function autoPickInstance() { - const instances = randomInstances(5) + const instances = utils.randomInstances(redirects[selectedFrontend]["clearnet"], 5) const myInstancesCache = [] for (const instance of instances) { pingCache[instance] = { color: "lightblue", value: "pinging..." } @@ -81,9 +74,7 @@ pingCache[instance] = colorTime(time) myInstancesCache.push([instance, time]) } - myInstancesCache.sort(function (a, b) { - return a[1] - b[1] - }) + myInstancesCache.sort((a, b) => a[1] - b[1]) _options[selectedFrontend].push(myInstancesCache[0][0]) options.set(_options) diff --git a/src/pages/popup_src/App.svelte b/src/pages/popup_src/App.svelte index 8f958a3e..f6699312 100644 --- a/src/pages/popup_src/App.svelte +++ b/src/pages/popup_src/App.svelte @@ -38,48 +38,12 @@ let _page page.subscribe(val => (_page = val)) - const dark = { - text: "#fff", - bgMain: "#121212", - bgSecondary: "#202020", - active: "#fbc117", - danger: "#f04141", - lightGrey: "#c3c3c3", - } - const light = { - text: "black", - bgMain: "white", - bgSecondary: "#e4e4e4", - active: "#fb9817", - danger: "#f04141", - lightGrey: "#c3c3c3", - } - let cssVariables - $: if (_options) { - if (_options.theme == "dark") { - cssVariables = dark - } else if (_options.theme == "light") { - cssVariables = light - } else if (window.matchMedia("(prefers-color-scheme: dark)").matches) { - cssVariables = dark - } else { - cssVariables = light - } - } + let style + $: if (_options) style = utils.style(_options, window) {#if _options && _config} -
+
{:else} -- cgit 1.4.1