Reputation: 121
I am creating an extension using manifest V3 with a service worker that is set to listen for "webRequest.onBeforeRequest"
chrome.webRequest.onBeforeRequest.addListener(function(requestDetails) { . . . }, { urls: ['<all_urls>'] }, ['requestBody']);
On my personal machine (windows), the extension works as expected and the service worker starts whenever there is a web request in any of the tabs.
However, on our work machines (mac), the web requests do not automatically start the service worker. If I manually start it, it receives the events and functions properly, but as soon as it is stopped due to being idle, it does not re-start on the next web request. If I go to "chrome://serviceworker-internals/", I can see that the service worker is still registered, but is stopped.
My only way of keep the extension functioning has been to keep the developer tools page for the service worker open (via inspecting it), so that it continues to work by simply never stopping.
Is there something specific that I need to to do for mac OS or are there any settings or policies that can limit what events a stopped service-worker can listen for?
Upvotes: 12
Views: 3789
Reputation: 1
I faced the same problem. Inserting code like the following seems to fix the problem. If I didn't set the callback function, it didn't work.
//content_script.js
var wakeup = function(){
setTimeout(function(){
chrome.runtime.sendMessage('ping', function(response){
console.log(response);
});
wakeup();
}, 1000);
}
wakeup();
//background.js
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
if( request == "ping" ){
console.log(request);
sendResponse("pong");
return;
}
sendResponse();
});
Upvotes: 1
Reputation: 549
Would have put this in the comments, but don't have enough reputation to comment. I tried to solve this by having a setTimeout loop every 10 seconds (which is also something Gregory Conrad suggested in the comments) but unfortunately this didn't work the service worker still went inactive. In the comments someone said this is a known bug, but it seems like it might not be a bug, it could be intentional: https://www.eff.org/deeplinks/2021/12/chrome-users-beware-manifest-v3-deceitful-and-threatening. As the link says, service workers make it harder for browser extensions such as AdBlock to work as effectively, something which would benefit Google as they run a major internet advertising network.
Upvotes: 4