Last month, this bug-fix made its way to Chrome stable channel release.
This theoretically allows the lazy registration of non-blocking webRequest
event listeners even when the service-worker is put into inactive state, like:
chrome.webRequest.onBeforeSendHeaders(myHandler);
/**
* @arg {chrome.webRequest.WebRequestDetails} details
*/
function myHandler(details)
{
// processing request's details …
}
The extension I am working on listens to requests sent from any tab of the active window and spots specific requests based on some criteria. To avoid unnecessary work, the extension exposes a on/off button within the popup page which, when clicked, will send the appropriate signal via chrome.runtime.sendMessage
to the web-worker which in turn will attach/detach a given handler for webRequest.onBeforeSendHeaders event. As a consequence, the registration doesn't occur at the service-worker top level but within a message response handler.
So basically, the request listener is turned off by default and it can be started/stopped when needed.
Currently this works but only requests fired during the first few seconds are being intercepted, after the start signal has been triggered (10sec on average).
If I click the start button again, the same scenario takes place: a listener is getting attached and new requests are being intercepted but only for few seconds.
Now if the service-worker inspect view (console) is opened, requests interceptions are never stopped and all works as expected ...
It looks like the event listener is being teared down once the service-worker is put into inactive state, which doesn't happen when the inspect view is open because it probably keeps the service-worker alive.
Wasn't the bug-fix supposed to address this issue in the first place?
Unfortunately I could not think of any alternative/workaround.
Has anybody faced this scenario lately?