From a6348cd60fbcbfd0f03f64522499df3d7d5edce6 Mon Sep 17 00:00:00 2001 From: rui hildt Date: Thu, 17 Oct 2024 14:57:29 +0200 Subject: [PATCH] Reload the Options tab when popup updates a proxy --- src/composables/useSocksProxy.ts | 11 +++++++++++ src/helpers/browserExtension.ts | 31 ++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/composables/useSocksProxy.ts b/src/composables/useSocksProxy.ts index d578b799..f0f74a45 100644 --- a/src/composables/useSocksProxy.ts +++ b/src/composables/useSocksProxy.ts @@ -6,6 +6,7 @@ import { ProxyInfoType, ProxyOperationArgs, } from '@/helpers/socksProxy.types'; +import { reloadOptions } from '@/helpers/browserExtension'; import { updateCurrentTabProxyBadge } from '@/helpers/proxyBadge'; import useActiveTab from '@/composables/useActiveTab'; @@ -39,15 +40,18 @@ const currentHostProxyDNSEnabled = computed(() => currentHostProxyDetails.value? const toggleGlobalProxy = () => { globalProxyDetails.value.socksEnabled = !globalProxyDetails.value.socksEnabled; updateCurrentTabProxyBadge(); + reloadOptions(); }; const toggleCurrentHostProxy = () => { hostProxiesDetails.value[activeTabHost.value].socksEnabled = !currentHostProxyEnabled.value; updateCurrentTabProxyBadge(); + reloadOptions(); }; const toggleCustomProxy = (host: string) => { hostProxiesDetails.value[host].socksEnabled = !hostProxiesDetails.value[host].socksEnabled; updateCurrentTabProxyBadge(); + reloadOptions(); }; const toggleCustomProxyDNS = (host: string) => { hostProxiesDetails.value[host].proxyDNS = !hostProxiesDetails.value[host].proxyDNS; @@ -95,6 +99,7 @@ const setGlobalProxy = ({ globalProxyDetails.value = newGlobalProxyDetails; updateConnection(); + reloadOptions(); }; const setCurrentHostProxy = ( @@ -126,6 +131,8 @@ const setCurrentHostProxy = ( hostProxies.value = { ...hostProxies.value, [host]: newHostProxy }; hostProxiesDetails.value = { ...hostProxiesDetails.value, [host]: newHostProxyDetails }; + + reloadOptions(); }; const removeCustomProxy = (host: string) => { @@ -133,22 +140,26 @@ const removeCustomProxy = (host: string) => { delete hostProxiesDetails.value[host]; updateConnection(); updateCurrentTabProxyBadge(); + reloadOptions(); }; const removeGlobalProxy = () => { globalProxy.value = {} as ProxyInfo; globalProxyDetails.value = {} as ProxyDetails; updateCurrentTabProxyBadge(); + reloadOptions(); }; const allowProxy = (host: string) => { excludedHosts.value = excludedHosts.value.filter((excluded) => excluded !== host); updateCurrentTabProxyBadge(); + reloadOptions(); }; const neverProxyHost = (host: string) => { excludedHosts.value = [...excludedHosts.value, host]; updateCurrentTabProxyBadge(); + reloadOptions(); }; const useSocksProxy = () => { diff --git a/src/helpers/browserExtension.ts b/src/helpers/browserExtension.ts index 44c5f9fe..c0246705 100644 --- a/src/helpers/browserExtension.ts +++ b/src/helpers/browserExtension.ts @@ -3,6 +3,12 @@ export enum Tab { PROXY = 'proxy', ABOUT = 'about', } + +export const { version } = browser.runtime.getManifest(); + +export const openPopup = () => { + browser.browserAction.openPopup(); +}; export const closePopup = () => { // The delay is added to stop a new empty browser window from opening // when installing the extension from the popup @@ -19,8 +25,27 @@ export const openOptions = async (tab?: Tab) => { browser.runtime.openOptionsPage(); }; -export const openPopup = () => { - browser.browserAction.openPopup(); +export const isPopupContext = () => { + const views = browser.extension.getViews({ type: 'popup' }); + return views.includes(window); }; -export const { version } = browser.runtime.getManifest(); +const findTabIdByUrl = async (url: string): Promise => { + const tabs = await browser.tabs.query({ url }); + if (tabs.length > 0 && tabs[0].id !== undefined) { + return tabs[0].id; + } + return undefined; +}; + +export const reloadOptions = async () => { + // Reload the options page if we're in the popup context + if (isPopupContext()) { + const optionsUrl = browser.runtime.getURL('dist/options/index.html'); + const optionsTabID = await findTabIdByUrl(optionsUrl); + + if (optionsTabID !== undefined) { + browser.tabs.reload(optionsTabID); + } + } +};