Skip to content

Commit

Permalink
Reload tabs based on global proxy actions (see commit details)
Browse files Browse the repository at this point in the history
- add
- remove
- toggle
- toggle DNS

Tabs that have an URL that is either excluded or custom will not be reloaded
  • Loading branch information
ruihildt committed Nov 25, 2024
1 parent 18d4a85 commit 4b794dd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/composables/useSocksProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProxyInfo> = {
port: 1080,
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -105,6 +112,7 @@ const setGlobalProxy = ({

updateConnection();
reloadOptions();
reloadGlobalProxiedTabs(combinedHosts.value);
};

const setCurrentHostProxy = (
Expand Down Expand Up @@ -155,6 +163,7 @@ const removeGlobalProxy = () => {
globalProxyDetails.value = {} as ProxyDetails;
updateCurrentTabProxyBadge();
reloadOptions();
reloadGlobalProxiedTabs(combinedHosts.value);
};

const allowProxy = (host: string) => {
Expand Down
22 changes: 22 additions & 0 deletions src/helpers/tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

0 comments on commit 4b794dd

Please sign in to comment.