From 95d9197dbb72e248038ddd535f45e4601401e066 Mon Sep 17 00:00:00 2001 From: rui hildt Date: Fri, 24 Nov 2023 14:22:55 +0100 Subject: [PATCH] Update proxy badge on every tab update --- src/background/main.ts | 6 ++-- src/composables/useExtBadge.ts | 13 -------- src/composables/useSocksProxy.ts | 6 ++-- src/helpers/browserAction.ts | 50 +++++++++++++++++++++++++++++++ src/helpers/pageAction.ts | 51 -------------------------------- src/helpers/socksProxy.ts | 2 +- 6 files changed, 56 insertions(+), 72 deletions(-) delete mode 100644 src/composables/useExtBadge.ts create mode 100644 src/helpers/browserAction.ts delete mode 100644 src/helpers/pageAction.ts diff --git a/src/background/main.ts b/src/background/main.ts index c8a3f5f9..aca28d69 100644 --- a/src/background/main.ts +++ b/src/background/main.ts @@ -2,7 +2,7 @@ import { onMessage } from 'webext-bridge/background'; import { addExtListeners } from '@/helpers/extensions'; import { DataAccount, initLeta, letaLogin, letaLogout } from '@/helpers/leta'; -import { initPageAction } from '@/helpers/pageAction'; +import { initBrowserAction } from '@/helpers/browserAction'; import { initProxyRequests } from '@/helpers/socksProxy'; // only on dev mode @@ -17,8 +17,8 @@ addExtListeners(); // Autologin to Leta on startup and add listeners initLeta(); -// Update pageAction in tabs and add listeners -initPageAction(); +// Update browserAction for tabs and add listeners +initBrowserAction(); // Add Leta Login message listeners onMessage('leta-login', async ({ data }) => { diff --git a/src/composables/useExtBadge.ts b/src/composables/useExtBadge.ts deleted file mode 100644 index 007d9cd1..00000000 --- a/src/composables/useExtBadge.ts +++ /dev/null @@ -1,13 +0,0 @@ -const setExtBadge = (text: string) => { - browser.browserAction.setBadgeBackgroundColor({ color: '#ffd524' }); - browser.browserAction.setBadgeTextColor({ color: 'black' }); - browser.browserAction.setBadgeText({ text }); -}; - -const useExtBadge = () => { - return { - setExtBadge, - }; -}; - -export default useExtBadge; diff --git a/src/composables/useSocksProxy.ts b/src/composables/useSocksProxy.ts index 5956db23..88a05c51 100644 --- a/src/composables/useSocksProxy.ts +++ b/src/composables/useSocksProxy.ts @@ -10,10 +10,8 @@ import getSocksIpForProtocol from './utils/getSocksIpForProtocol'; import useStore from '@/composables/useStore'; import useConnection from '@/composables/useConnection'; -import useExtBadge from '@/composables/useExtBadge'; const { connection, updateConnection } = useConnection(); -const { setExtBadge } = useExtBadge(); const { globalProxyDetails, globalProxy, customProxies, customProxiesDetails } = useStore(); const globalProxyEnabled = computed(() => globalProxyDetails.value.socksEnabled); @@ -28,7 +26,7 @@ const disableGlobalProxy = () => { ...globalProxyDetails.value, socksEnabled: false, }; - setExtBadge(''); + // setExtBadge(''); updateConnection(); }; @@ -37,7 +35,7 @@ const enableGlobalProxy = () => { ...globalProxyDetails.value, socksEnabled: true, }; - setExtBadge('P'); + // setExtBadge('P'); updateConnection(); }; diff --git a/src/helpers/browserAction.ts b/src/helpers/browserAction.ts new file mode 100644 index 00000000..da6f6bc7 --- /dev/null +++ b/src/helpers/browserAction.ts @@ -0,0 +1,50 @@ +import { getglobalProxyDetails } from '@/helpers/socksProxy'; +import { ProxyDetails } from './socksProxy.types'; + +export const initBrowserAction = () => { + // Each time a tab is updated, reset the browserAction for that tab + browser.tabs.onUpdated.addListener(updatedTabListener); + + updateTabsProxyBadges(); +}; + +// Update a tab browserAction badge & title +const updateTabProxyBadge = async (tabId: number, globalProxyDetails: ProxyDetails) => { + const tooltip = `Server: ${globalProxyDetails.server}\nProtocol: ${globalProxyDetails.protocol}\nProxy DNS: ${globalProxyDetails.proxyDNS}`; + + if (globalProxyDetails.socksEnabled) { + browser.browserAction.setTitle({ tabId, title: tooltip }); + setTabExtBadge(tabId); + } else { + browser.pageAction.setTitle({ tabId, title: 'Proxy not in use' }); + setTabExtBadge(tabId, false); + } +}; + +// Update state of the proxy badge & title, for all tabs +const updateTabsProxyBadges = async () => { + const tabs = browser.tabs.query({}); + const globalProxyDetails = await getglobalProxyDetails(); + + tabs.then((tabs) => { + for (const tab of tabs) { + updateTabProxyBadge(tab.id!, globalProxyDetails); + } + }); +}; + +const updatedTabListener = async (tabId: number) => { + const globalProxyDetails = await getglobalProxyDetails(); + + updateTabProxyBadge(tabId, globalProxyDetails); +}; + +const setTabExtBadge = (tabId: number, proxy = true) => { + if (proxy) { + browser.browserAction.setBadgeText({ text: 'P', tabId }); + browser.browserAction.setBadgeBackgroundColor({ color: '#ffd524', tabId }); + browser.browserAction.setBadgeTextColor({ color: 'black', tabId }); + } else { + browser.browserAction.setBadgeText({ text: '', tabId }); + } +}; diff --git a/src/helpers/pageAction.ts b/src/helpers/pageAction.ts deleted file mode 100644 index 60289c0a..00000000 --- a/src/helpers/pageAction.ts +++ /dev/null @@ -1,51 +0,0 @@ -import { getglobalProxyDetails } from '@/helpers/socksProxy'; -import { ProxyDetails } from './socksProxy.types'; - -export const initPageAction = () => { - // Each time a tab is updated, reset the page action for that tab. - browser.tabs.onUpdated.addListener(updatedTabListener); - - updateTabsProxyIcons(); -}; - -// Update a tab page action icon & title -const updateTabProxyIcon = async (tabId: number, globalProxyDetails: ProxyDetails) => { - const tooltip = `Server: ${globalProxyDetails.server}\nProtocol: ${globalProxyDetails.protocol}\nProxy DNS: ${globalProxyDetails.proxyDNS}`; - - if (globalProxyDetails.socksEnabled) { - browser.pageAction.setTitle({ tabId, title: tooltip }); - browser.pageAction.setIcon({ - path: {}, // This will reset the icon to the default from the manifest - tabId, - }); - } else { - browser.pageAction.setTitle({ tabId, title: 'Proxy not in use' }); - browser.pageAction.setIcon({ - path: { - 16: '/assets/icons/route-blocked-w.svg', - 48: '/assets/icons/route-blocked-w.svg', - 96: '/assets/icons/route-blocked-w.svg', - }, - tabId, - }); - } - await browser.pageAction.show(tabId); -}; - -// Update state of the proxy icon for all tabs -const updateTabsProxyIcons = async () => { - const tabs = browser.tabs.query({}); - const globalProxyDetails = await getglobalProxyDetails(); - - tabs.then((tabs) => { - for (const tab of tabs) { - updateTabProxyIcon(tab.id!, globalProxyDetails); - } - }); -}; - -const updatedTabListener = async (tabId: number) => { - const globalProxyDetails = await getglobalProxyDetails(); - - updateTabProxyIcon(tabId, globalProxyDetails); -}; diff --git a/src/helpers/socksProxy.ts b/src/helpers/socksProxy.ts index 056aaebf..86645eef 100644 --- a/src/helpers/socksProxy.ts +++ b/src/helpers/socksProxy.ts @@ -22,7 +22,7 @@ const handleProxyRequest = async (details: browser.proxy._OnRequestDetails) => { const { excludedHosts } = await browser.storage.local.get('excludedHosts'); const { customProxies } = await browser.storage.local.get('customProxies'); - // TODO implement these features + // TODO implement random proxy const { randomProxyActive } = await browser.storage.local.get('randomProxyActive'); const globalConfigParsed = JSON.parse(globalProxy);