Fix Chrome prefetch main_frame & outermost_frame
Changes
Added two new functions:
-
getTrueTabDomain(tabId)- Fetches the actual domain from the tab's URL (source of truth from the address bar) -
isMainTabUrl(details)- Validates if a request is truly for the main frame by comparing the request's domain against the tab's actual domain
Modified two locations:
-
chrome.webRequest.onBeforeRequest- Replaced simple type check withawait isMainTabUrl(details) -
chrome.webRequest.onBeforeRedirect- Replaced simple type check withawait isMainTabUrl(details)
What this fixes
When Chrome prefetches links, it sends requests with type: "main_frame", which previously caused mainDomain to be overwritten incorrectly. The fix validates that the request's domain matches the tab's actual URL before treating it as a main frame navigation, preventing prefetch requests from corrupting the extension's state.
Before:
https://github.com/user-attachments/assets/eefd8d31-d122-44d2-9552-e5def210c026
After:
https://github.com/user-attachments/assets/aacb1ea4-b19d-4aeb-bf36-e77693b84a7a
@pmarks-net This fixes https://github.com/pmarks-net/ipvfoo/issues/71
I found a preliminary solution. It's called from the same places as await isMainTabUrl in your PR.
function isProperMainFrame(details) {
if (details.type == "main_frame" || details.type == "outermost_frame") {
if (details.documentId) {
// It seems that a main_frame request with a documentId refers to some kind
// of prefetch, rather than a top-level navigation to a new URL.
console.log("Preloaded main_frame?", details.url);
return false;
}
return true;
}
return false;
}
Demo: https://www.google.com/search?q=real+estate
background.js:951 Preloaded main_frame? https://www.realtor.com/...
background.js:951 Preloaded main_frame? https://www.zillow.com/...
IPvFoo shows "www.realtor.com 2001:4860:4802:34::9d" and "www.zillow.com 2001:4860:4802:34::9d", which suggests that the requests are being proxied by Google. I should probably add a P/prefetch icon next to the address, because otherwise the results could be misleading. Neither of those sites actually support IPv6.