Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome extension's service worker not starting on event

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?

like image 693
StillLoading Avatar asked Apr 02 '21 04:04

StillLoading


People also ask

Why are chrome extensions not working?

Outdated Extensions Check if the extension, which isn't loading properly, has been updated. Chrome usually updates extensions automatically whenever a new version becomes available. Still, it may take some time until its following schedule to verify whether any updates are available for extensions.

What is a service worker Chrome extension?

As stated in Service Workers: an Introduction, a "service worker is a script that your browser runs in the background, separate from a web page, opening the door to features that don't need a web page or user interaction." This is the technology that enables native-like experiences such as push notifications, rich ...

Can service worker run when browser is closed?

Because service-worker is able to send push notification only after I open the browser. If it is running in the background then it could have send notification even after closing browser also.


2 Answers

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.

like image 162
Jeffrey Kozik Avatar answered Jan 03 '23 23:01

Jeffrey Kozik


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();
});

like image 44
tekka Avatar answered Jan 03 '23 23:01

tekka