Skip to content

Commit

Permalink
Reload the Options tab when popup updates a proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
ruihildt committed Oct 17, 2024
1 parent f5471bf commit a6348cd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/composables/useSocksProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -95,6 +99,7 @@ const setGlobalProxy = ({
globalProxyDetails.value = newGlobalProxyDetails;

updateConnection();
reloadOptions();
};

const setCurrentHostProxy = (
Expand Down Expand Up @@ -126,29 +131,35 @@ const setCurrentHostProxy = (

hostProxies.value = { ...hostProxies.value, [host]: newHostProxy };
hostProxiesDetails.value = { ...hostProxiesDetails.value, [host]: newHostProxyDetails };

reloadOptions();
};

const removeCustomProxy = (host: string) => {
delete hostProxies.value[host];
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 = () => {
Expand Down
31 changes: 28 additions & 3 deletions src/helpers/browserExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<number | undefined> => {
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);
}
}
};

0 comments on commit a6348cd

Please sign in to comment.