diff --git a/src/assets/javascripts/services.js b/src/assets/javascripts/services.js index 1c84eff..a0ba011 100644 --- a/src/assets/javascripts/services.js +++ b/src/assets/javascripts/services.js @@ -669,7 +669,9 @@ function computeService(url) { export function computeFrontend(url) { for (const service in config.services) { for (const frontend in config.services[service].frontends) { - if (all(service, frontend, options, config).findIndex(instance => url.href.startsWith(instance)) >= 0) { + const instances = all(service, frontend, options, config) + const i = instances.findIndex(instance => url.href.startsWith(instance)) + if (i >= 0) { return { service, frontend } } } diff --git a/src/pages/background/background.js b/src/pages/background/background.js index 029686a..4b46ea8 100644 --- a/src/pages/background/background.js +++ b/src/pages/background/background.js @@ -117,30 +117,6 @@ browser.webRequest.onBeforeRequest.addListener( ["blocking"] ) -// browser.webRequest.onHeadersReceived.addListener( -// details => { -// if (details.statusCode >= 501 || details.statusCode == 429 || details.statusCode == 403) { -// const url = new URL(details.url) -// const r = servicesHelper.computeFrontend(url) -// if (!r) return -// const { service, frontend } = r -// const params = new URLSearchParams({ -// message: "server_error", -// code: details.statusCode, -// url: url.href, -// frontend: frontend, -// service: service, -// }) -// setTimeout(() => { -// browser.tabs.update({ -// url: browser.runtime.getURL(`/pages/messages/index.html?${params.toString()}`), -// }) -// }, 2000) -// } -// }, -// { urls: [""] } -// ) - browser.tabs.onRemoved.addListener(tabId => { if (tabIdRedirects[tabId] != undefined) { delete tabIdRedirects[tabId] diff --git a/src/pages/messages_src/App.svelte b/src/pages/messages_src/App.svelte index 1c5170d..9f464ac 100644 --- a/src/pages/messages_src/App.svelte +++ b/src/pages/messages_src/App.svelte @@ -80,39 +80,6 @@ const newUrl = await servicesHelper.redirectAsync(oldUrl, "main_frame", null, null, false, true) browser.tabs.update({ url: newUrl }) } - - async function switchInstance() { - const newUrl = await servicesHelper.switchInstance(oldUrl) - browser.tabs.update({ url: newUrl }) - } - - async function removeInstance() { - const service = await servicesHelper.computeService(oldUrl) - const frontend = params.get("frontend") - const i = _options[frontend].findIndex(instance => oldUrl.href.startsWith(instance)) - _options[frontend].splice(i, 1) - options.set(_options) - const newUrl = await servicesHelper.switchInstance(oldUrl, service) - browser.tabs.update({ url: newUrl }) - } - - async function removeAndAutoPickInstance() { - const service = await servicesHelper.computeService(oldUrl) - - const frontend = params.get("frontend") - const i = _options[frontend].findIndex(instance => oldUrl.href.startsWith(instance)) - _options[frontend].splice(i, 1) - options.set(_options) - await autoPick() - const newUrl = await servicesHelper.switchInstance(oldUrl, service) - browser.tabs.update({ url: newUrl }) - } - - async function addAutoPickInstance() { - await autoPick() - const newUrl = await servicesHelper.switchInstance(oldUrl) - browser.tabs.update({ url: newUrl }) - } {#if _options && _config} @@ -124,34 +91,6 @@ {browser.i18n.getMessage("enable") || "Enable"} - {:else if params.get("message") == "server_error"} - -
-

Your selected instance gave out an error: {params.get("code")}

- {#if _options[params.get("frontend")].length > 1} - - - {:else} - - - {/if} -
{:else if params.get("message") == "no_instance"}

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 ab5682d..d2fc543 100644 --- a/src/pages/popup_src/Buttons.svelte +++ b/src/pages/popup_src/Buttons.svelte @@ -10,11 +10,14 @@ import SettingsIcon from "../icons/SettingsIcon.svelte" import { options, config } from "./stores" import { onDestroy } from "svelte" - import servicesHelper from "../../assets/javascripts/services" + import servicesHelper, { computeFrontend } from "../../assets/javascripts/services" import Switch from "./components/Switch.svelte" + import AutoPickIcon from "../icons/AutoPickIcon.svelte" + import utils from "../../assets/javascripts/utils" let _options let _config + let autoPicking = false const unsubscribeOptions = options.subscribe(val => (_options = val)) const unsubscribeConfig = config.subscribe(val => (_config = val)) @@ -28,6 +31,8 @@ let redirectToOriginal let redirect let currentService + let frontend + let service browser.tabs.query({ active: true, currentWindow: true }, async tabs => { if (tabs[0].url) { url = new URL(tabs[0].url) @@ -35,8 +40,51 @@ servicesHelper.reverse(url).then(r => (redirectToOriginal = r)) servicesHelper.redirectAsync(url, "main_frame", null, null, false, true).then(r => (redirect = r)) servicesHelper.computeService(url).then(r => (currentService = r)) + const computed = servicesHelper.computeFrontend(url) + if (computed) { + ;({ service, frontend } = computed) + } } }) + + async function removeInstance() { + const i = _options[frontend].findIndex(instance => url.href.startsWith(instance)) + _options[frontend].splice(i, 1) + options.set(_options) + const newUrl = await servicesHelper.switchInstance(url, service) + browser.tabs.update({ url: newUrl }) + } + + async function autoPick() { + autoPicking = true + const redirects = await utils.getList(_options) + const instances = utils.randomInstances(redirects[frontend]["clearnet"], 5) + const pings = await Promise.all([ + ...instances.map(async instance => { + return [instance, await utils.ping(instance)] + }), + ]) + pings.sort((a, b) => a[1] - b[1]) + _options[frontend].push(pings[0][0]) + options.set(_options) + autoPicking = false + } + + async function addAutoPickInstance() { + await autoPick() + const newUrl = await servicesHelper.switchInstance(url) + browser.tabs.update({ url: newUrl }) + } + + async function removeAndAutoPickInstance() { + const i = _options[frontend].findIndex(instance => url.href.startsWith(instance)) + _options[frontend].splice(i, 1) + options.set(_options) + await autoPick() + const newUrl = await servicesHelper.switchInstance(url, service) + browser.tabs.update({ url: newUrl }) + } + $: console.log("autoPicking", autoPicking)
@@ -56,17 +104,44 @@ {/if} - {#if switchInstance} - - browser.tabs.update({ url: switchInstance }, () => { - window.close() - })} - > - - - + {#if service && frontend} + {#if _options[frontend].length > 1} + {#if switchInstance} + + browser.tabs.update({ url: switchInstance }, () => { + window.close() + })} + > + + + + {/if} + + + + + {:else} + + + + + + + + + {/if} {/if} {#if redirectToOriginal} @@ -130,6 +205,20 @@ transform: translateY(1px); } + :global(.disabled) { + cursor: not-allowed; + opacity: 0.5; + } + + :global(.disabled:hover) { + color: var(--text); + cursor: not-allowed; + } + + :global(.disabled:active) { + transform: none; + } + :global(img, svg) { margin-right: 5px; margin-left: 0; diff --git a/src/pages/popup_src/components/Row.svelte b/src/pages/popup_src/components/Row.svelte index a4d78f0..064942d 100644 --- a/src/pages/popup_src/components/Row.svelte +++ b/src/pages/popup_src/components/Row.svelte @@ -1,13 +1,12 @@
- -
- - - \ No newline at end of file + +
+ +