From b457332a5831aa9f23da929d2d5530e5600a6828 Mon Sep 17 00:00:00 2001 From: Alex Goodkind Date: Sat, 18 Oct 2025 21:20:14 -0700 Subject: [PATCH 1/2] Fix Chrome prefetch main_frame & outermost_frame --- src/background.js | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/src/background.js b/src/background.js index bdbffd7..d11910b 100644 --- a/src/background.js +++ b/src/background.js @@ -108,6 +108,42 @@ function parseUrl(url) { return { domain: domain, ssl: ssl, ws: ws, origin: u.origin }; } +/** + * Gets the domain of a tab from source of truth + * that is, the domain of the URL that is currently displayed in the URL bar. + * @param {number} tabId - The tab ID to get the URL of. + * @returns {Promise} - The domain of the tab. + */ +async function getTrueTabDomain(tabId) { + try { + const tabInfo = await chrome.tabs.get(tabId); + const domain = parseUrl(tabInfo.url).domain; + debugLog("getTrueTabDomain success", domain); + return domain; + } catch (error) { + debugLog("getTrueTabDomain error", error); + return null; + } +} + +/** + * Checks if the true tab domain matches the source of truth + * @param {Object} details - The details object from the webRequest event + * @returns {Promise} - True if the true tab domain matches the source of truth, false otherwise + */ +async function isMainTabUrl(details) { + if (details.type != "main_frame" && details.type != "outermost_frame") { + return false; + } + try { + const trueTabDomain = await getTrueTabDomain(details.tabId); + return trueTabDomain === parseUrl(details.url).domain; + } catch (error) { + debugLog("isMainTabUrl error", error); + return false; + } +} + function updateNAT64(domain, addr) { if (!(IPV4_ONLY_DOMAINS.has(domain) && addr)) { return; @@ -945,7 +981,7 @@ chrome.webRequest.onBeforeRequest.addListener(wrap(async (details) => { const tabId = details.tabId; const tabInfos = []; if (tabId > 0) { - if (details.type == "main_frame" || details.type == "outermost_frame") { + if (await isMainTabUrl(details)) { const parsed = parseUrl(details.url); tabMap.remove(tabId); const tabInfo = tabMap.lookupOrNew(tabId); @@ -991,8 +1027,7 @@ chrome.webRequest.onBeforeRequest.addListener(wrap(async (details) => { // As of 2022, this can be tested by visiting http://maps.google.com/ chrome.webRequest.onBeforeRedirect.addListener(wrap(async (details) => { await storageReady; - if (!(details.type == "main_frame" || - details.type == "outermost_frame")) { + if (!(await isMainTabUrl(details))) { return; } const requestInfo = requestMap[details.requestId]; From bc6e97bddd929c96da28ee0c0b068e053c6ef397 Mon Sep 17 00:00:00 2001 From: Alex Goodkind Date: Sat, 18 Oct 2025 21:21:55 -0700 Subject: [PATCH 2/2] indent --- src/background.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/background.js b/src/background.js index d11910b..607f194 100644 --- a/src/background.js +++ b/src/background.js @@ -118,7 +118,7 @@ async function getTrueTabDomain(tabId) { try { const tabInfo = await chrome.tabs.get(tabId); const domain = parseUrl(tabInfo.url).domain; - debugLog("getTrueTabDomain success", domain); + debugLog("getTrueTabDomain success", domain); return domain; } catch (error) { debugLog("getTrueTabDomain error", error);