Events are not firing for `main_frame` in webRequest.onCompleted

Hi everyone,

I am relatively newer to browser extension development, and I am concerned I am using the wrong event handler or seeing a new bug. I am currently hooking chrome.webRequest.onCompleted for Firefox & Chrome tab requests for analytics purposes.

Normally refresh and new page visits trigger many events for all incoming data, but what I have found on Chrome and Firefox is that usually page visits and refreshes produce main_frame type. What I have been seeing only in Firefox is that refreshes occasionally do not refresh the main_frame though some resources seem to recycle. I believe this to have started with Firefox 122, but I do not have the spare system to try before it.

chrome.webRequest.onCompleted.addListener(function (details: chrome.webRequest.WebResponseCacheDetails) {
if (
	(details.initiator && details.initiator.startsWith("chrome-extension://")) || 
	(details.url && details.url.startsWith("chrome-extension://")) ||
	details.tabId < 0
){
	return;
}

if (details.type == "main_frame") {
	clearData(details.tabId);

	// Main Tab Request
	const instance = new Instance(details); // Data processing and stuff...
	const timer = setInterval(function() {
		instance.LogToConsole();
		clearInterval(timer);
	}, 2000);
} else if (
	details.type === "sub_frame" || 
	details.type === "stylesheet" || 
	details.type === "script" || 
	details.type === "image" || 
	details.type === "xmlhttprequest"
){
	// Tab Sub-Requests
	// Data processing and stuff...
}
}

It’s possible there is a bug in Firefox. If you’re interested in tracking down when Firefox’s behavior changed, you can use mozregression to run and test your add-on in older versions of Firefox.

Can you share more about what you’re trying to do? It’s not clear from this description what exactly you’re trying to monitor and measure. Depending on your needs, another API may be better suited for what you’re trying to accomplish.

The webRequest API is an abstraction over the browser’s network layer. Not all requests made by a page reach this layer. As such, a “request” in this API does not necessarily match the concept of a "request " made by a web page. For example, a page’s service worker could intercept a request and serve a response, meaning that the request never reaches the browser’s network layer. The webRequest API is best suited for situations where an extension wants to understand how the browser actually sends and receives HTTP requests.

You mentioned wanting to monitor “tab requests for analytics purposes.” This sounds to me like you may be looking to measure navigations – moving from one document to another in the current tab. If that’s correct, the webNavigation API may be better suited for your use case.