Skip to content

Commit 0606043

Browse files
committed
Refactor active tab handling
1 parent e8803bd commit 0606043

File tree

5 files changed

+62
-52
lines changed

5 files changed

+62
-52
lines changed

src/composables/useActiveTab.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ref } from 'vue';
2-
import { getActiveTabDetails } from '@/helpers/socksProxy';
2+
import { getActiveTabDetails } from '@/helpers/tabs';
33

44
const activeTabHost = ref('');
55
const isBrowserPage = ref(false);

src/helpers/proxyBadge.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { getActiveProxyDetails, isLocalOrReservedIP } from '@/helpers/socksProxy';
1+
import { isLocalOrReservedIP } from '@/helpers/socksProxy';
22
import { ProxyDetails } from '@/helpers/socksProxy.types';
33
import { checkDomain } from './domain';
4+
import { getActiveProxyDetails, getActiveTab } from './tabs';
45

56
export const updateCurrentTabProxyBadge = async () => {
6-
const [activeTab] = await browser.tabs.query({ active: true, currentWindow: true });
7+
const activeTab = await getActiveTab();
78

89
if (activeTab) {
910
await updateTabProxyBadge(activeTab, await getActiveProxyDetails());

src/helpers/proxyListeners.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { getProxyPermissions } from '@/helpers/permissions';
22
import { updateCurrentTabProxyBadge, updateTabProxyBadge } from '@/helpers/proxyBadge';
3-
import { getActiveProxyDetails, handleProxyRequest } from '@/helpers/socksProxy';
3+
import { handleProxyRequest } from '@/helpers/socksProxy';
4+
import { getActiveProxyDetails } from './tabs';
45

56
export const initProxyListeners = () => {
67
// Will init listeners on extension start if permissions are granted

src/helpers/socksProxy.ts

-48
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,6 @@ import ipaddr from 'ipaddr.js';
33
import { RequestDetails, ProxyDetails, ProxyInfoMap } from '@/helpers/socksProxy.types';
44
import { checkDomain } from './domain';
55

6-
const getGlobalProxyDetails = async (): Promise<ProxyDetails> => {
7-
const response = await browser.storage.local.get('globalProxyDetails');
8-
9-
if ('globalProxyDetails' in response) {
10-
return JSON.parse(response.globalProxyDetails);
11-
}
12-
return { socksEnabled: false };
13-
};
14-
15-
export const getActiveTabDetails = async () => {
16-
const [activeTab] = await browser.tabs.query({ active: true, currentWindow: true });
17-
18-
// activeTab will be null if tabs permission has not been granted
19-
if (!activeTab?.url) {
20-
return { host: '', protocol: '' };
21-
}
22-
23-
const activeTabURL = new URL(activeTab.url);
24-
return {
25-
host: activeTabURL.hostname,
26-
protocol: activeTabURL.protocol,
27-
};
28-
};
29-
30-
export const getActiveProxyDetails = async () => {
31-
const globalProxyDetails = await getGlobalProxyDetails();
32-
const { hostProxiesDetails } = await browser.storage.local.get('hostProxiesDetails');
33-
34-
if (hostProxiesDetails) {
35-
const hostProxiesDetailsParsed = JSON.parse(hostProxiesDetails);
36-
const [activeTab] = await browser.tabs.query({ active: true, currentWindow: true });
37-
const tabHost = new URL(activeTab.url!).hostname;
38-
const { domain } = checkDomain(tabHost);
39-
40-
// Check subdomain proxy first
41-
if (hostProxiesDetailsParsed[tabHost]?.socksEnabled) {
42-
return hostProxiesDetailsParsed[tabHost];
43-
}
44-
45-
// Then check domain proxy
46-
if (hostProxiesDetailsParsed[domain]?.socksEnabled) {
47-
return hostProxiesDetailsParsed[domain];
48-
}
49-
}
50-
51-
return globalProxyDetails;
52-
};
53-
546
// TODO decide what how to handle fallback proxy (if proxy is invalid, it will fallback to Firefox proxy if configured)
557
// https://bugzilla.mozilla.org/show_bug.cgi?id=1750561
568

src/helpers/tabs.ts

+56
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { checkDomain } from './domain';
2+
import { ProxyDetails } from './socksProxy.types';
3+
14
export const reloadMatchingTabs = async (url: string) => {
25
const urlPattern = `*://*.${url}/*`;
36

@@ -30,3 +33,56 @@ const isExcludedURL = (url: string, excludedURLs: string[]): boolean => {
3033
const { hostname } = new URL(url);
3134
return excludedURLs.some((excludedUrl) => hostname === excludedUrl);
3235
};
36+
37+
export const getActiveTabDetails = async () => {
38+
const activeTab = await getActiveTab();
39+
40+
// activeTab will be null if tabs permission has not been granted
41+
if (!activeTab?.url) {
42+
return { host: '', protocol: '' };
43+
}
44+
45+
const activeTabURL = new URL(activeTab.url);
46+
return {
47+
host: activeTabURL.hostname,
48+
protocol: activeTabURL.protocol,
49+
};
50+
};
51+
52+
export const getActiveProxyDetails = async () => {
53+
const globalProxyDetails = await getGlobalProxyDetails();
54+
const { hostProxiesDetails } = await browser.storage.local.get('hostProxiesDetails');
55+
56+
if (hostProxiesDetails) {
57+
const hostProxiesDetailsParsed = JSON.parse(hostProxiesDetails);
58+
const activeTab = await getActiveTab();
59+
const tabHost = new URL(activeTab.url!).hostname;
60+
const { domain } = checkDomain(tabHost);
61+
62+
// Check subdomain proxy first
63+
if (hostProxiesDetailsParsed[tabHost]?.socksEnabled) {
64+
return hostProxiesDetailsParsed[tabHost];
65+
}
66+
67+
// Then check domain proxy
68+
if (hostProxiesDetailsParsed[domain]?.socksEnabled) {
69+
return hostProxiesDetailsParsed[domain];
70+
}
71+
}
72+
73+
return globalProxyDetails;
74+
};
75+
76+
const getGlobalProxyDetails = async (): Promise<ProxyDetails> => {
77+
const response = await browser.storage.local.get('globalProxyDetails');
78+
79+
if ('globalProxyDetails' in response) {
80+
return JSON.parse(response.globalProxyDetails);
81+
}
82+
return { socksEnabled: false };
83+
};
84+
85+
export const getActiveTab = async () => {
86+
const [activeTab] = await browser.tabs.query({ active: true, currentWindow: true });
87+
return activeTab;
88+
};

0 commit comments

Comments
 (0)