|
| 1 | +import { checkDomain } from './domain'; |
| 2 | +import { ProxyDetails } from './socksProxy.types'; |
| 3 | + |
1 | 4 | export const reloadMatchingTabs = async (url: string) => {
|
2 | 5 | const urlPattern = `*://*.${url}/*`;
|
3 | 6 |
|
@@ -30,3 +33,56 @@ const isExcludedURL = (url: string, excludedURLs: string[]): boolean => {
|
30 | 33 | const { hostname } = new URL(url);
|
31 | 34 | return excludedURLs.some((excludedUrl) => hostname === excludedUrl);
|
32 | 35 | };
|
| 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