From 4b794dd568eca5c9f0f7100217672bb6b4ad8686 Mon Sep 17 00:00:00 2001 From: rui hildt Date: Mon, 25 Nov 2024 17:14:44 +0100 Subject: [PATCH] Reload tabs based on global proxy actions (see commit details) - add - remove - toggle - toggle DNS Tabs that have an URL that is either excluded or custom will not be reloaded --- src/composables/useSocksProxy.ts | 11 ++++++++++- src/helpers/tabs.ts | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/composables/useSocksProxy.ts b/src/composables/useSocksProxy.ts index a6c6bb6a..5b2c1cbe 100644 --- a/src/composables/useSocksProxy.ts +++ b/src/composables/useSocksProxy.ts @@ -12,7 +12,7 @@ import { updateCurrentTabProxyBadge } from '@/helpers/proxyBadge'; import useActiveTab from '@/composables/useActiveTab'; import useConnection from '@/composables/useConnection'; import useStore from '@/composables/useStore'; -import { reloadMatchingTabs } from '@/helpers/tabs'; +import { reloadGlobalProxiedTabs, reloadMatchingTabs } from '@/helpers/tabs'; const baseConfig: Partial = { port: 1080, @@ -38,10 +38,16 @@ const currentHostProxyEnabled = computed( const globalProxyDNSEnabled = computed(() => globalProxy.value?.proxyDNS ?? false); const currentHostProxyDNSEnabled = computed(() => currentHostProxyDetails.value?.proxyDNS ?? false); +const combinedHosts = computed(() => { + const allHosts = [...Object.keys(hostProxiesDetails.value), ...excludedHosts.value]; + return [...new Set(allHosts)].sort((a, b) => a.localeCompare(b)); +}); + const toggleGlobalProxy = () => { globalProxyDetails.value.socksEnabled = !globalProxyDetails.value.socksEnabled; updateCurrentTabProxyBadge(); reloadOptions(); + reloadGlobalProxiedTabs(combinedHosts.value); }; const toggleCurrentHostProxy = () => { hostProxiesDetails.value[activeTabHost.value].socksEnabled = !currentHostProxyEnabled.value; @@ -67,6 +73,7 @@ const toggleGlobalProxyDNS = () => { globalProxyDetails.value.proxyDNS = updatedGlobalProxyDNS; globalProxy.value.proxyDNS = updatedGlobalProxyDNS; updateCurrentTabProxyBadge(); + reloadGlobalProxiedTabs(combinedHosts.value); }; const toggleCurrentHostProxyDNS = () => { const updatedCurrentHostProxyDNS = !currentHostProxyDetails.value.proxyDNS; @@ -105,6 +112,7 @@ const setGlobalProxy = ({ updateConnection(); reloadOptions(); + reloadGlobalProxiedTabs(combinedHosts.value); }; const setCurrentHostProxy = ( @@ -155,6 +163,7 @@ const removeGlobalProxy = () => { globalProxyDetails.value = {} as ProxyDetails; updateCurrentTabProxyBadge(); reloadOptions(); + reloadGlobalProxiedTabs(combinedHosts.value); }; const allowProxy = (host: string) => { diff --git a/src/helpers/tabs.ts b/src/helpers/tabs.ts index 344d088a..b6b75c5d 100644 --- a/src/helpers/tabs.ts +++ b/src/helpers/tabs.ts @@ -8,3 +8,25 @@ export const reloadMatchingTabs = async (url: string) => { } }); }; + +export const reloadGlobalProxiedTabs = async (excludedURLs: string[]) => { + const tabs = await browser.tabs.query({ url: '*://*/*' }); + + const tabsToReload = tabs.filter((tab) => { + if (!tab.url) { + return false; + } + return !isExcludedURL(tab.url, excludedURLs); + }); + + tabsToReload.forEach((tab) => { + if (tab.id) { + browser.tabs.reload(tab.id); + } + }); +}; + +const isExcludedURL = (url: string, excludedURLs: string[]): boolean => { + const { hostname } = new URL(url); + return excludedURLs.some((excludedUrl) => hostname === excludedUrl); +};