Skip to content

Commit 85e3ffa

Browse files
committed
Use the same method to get activeTab consistently
1 parent 3978b05 commit 85e3ffa

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

Diff for: src/helpers/proxyBadge.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { ProxyDetails } from '@/helpers/socksProxy.types';
33
import { checkDomain } from './domain';
44

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

8-
if (activeTab[0]) {
9-
await updateTabProxyBadge(activeTab[0], await getActiveProxyDetails());
8+
if (activeTab) {
9+
await updateTabProxyBadge(activeTab, await getActiveProxyDetails());
1010
}
1111
};
1212

Diff for: src/helpers/socksProxy.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@ const getGlobalProxyDetails = async (): Promise<ProxyDetails> => {
1313
};
1414

1515
export const getActiveTabDetails = async () => {
16-
const activeWindow = await browser.windows.getCurrent({ populate: true });
17-
const activeTab = activeWindow.tabs!.find((tab) => tab.active);
18-
const activeTabURL = new URL(activeTab!.url!);
19-
20-
return activeTabURL
21-
? {
22-
host: activeTabURL.hostname,
23-
protocol: activeTabURL.protocol,
24-
}
25-
: { host: '', protocol: '' };
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+
};
2628
};
2729

2830
export const getActiveProxyDetails = async () => {
@@ -31,8 +33,8 @@ export const getActiveProxyDetails = async () => {
3133

3234
if (hostProxiesDetails) {
3335
const hostProxiesDetailsParsed = JSON.parse(hostProxiesDetails);
34-
const activeTab = await browser.tabs.query({ active: true, currentWindow: true });
35-
const tabHost = new URL(activeTab[0].url!).hostname;
36+
const [activeTab] = await browser.tabs.query({ active: true, currentWindow: true });
37+
const tabHost = new URL(activeTab.url!).hostname;
3638
const { domain } = checkDomain(tabHost);
3739

3840
// Check subdomain proxy first

0 commit comments

Comments
 (0)