Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

187
Views
Run web extension's service worker on browser open (Chrome)

I am building a web extension (Chrome) that checks if the external's API changed. This should be happening periodically (e.g. every 10 mins) and in the background. The plan is to have a service worker that would fire these requests, and replace extension's icon when a change in the response was detected. I have a working code that does exactly that, but I am unable to persist the service worker, and make it run on browser load (i.e. moment when the window opens). I managed to use message API, but that requires the user to click the extension button to open it and only then the extension would continuously run in the background.

This is my service worker code:

const browser = chrome || browser;

self.addEventListener("message", async (event) => {
    if (event.data && event.data.type === 'CHECK_FOR_NEW_RESOURCES') {
        communicationPort = event.ports[0];

        const compareStructures = setInterval(async () => {
            const currentStructure = await getStructure(event.data.baseURL + 'webservice/rest/server.php', event.data.token);
            const {curr, newResources } = findDifferences(event.data.structure.base, currentStructure);
            if(newResources > 0) {
                communicationPort.postMessage({different: true, structure: curr, newResources,
                    time: new Date().toISOString()});
                browser.action.setIcon({ path: { '48': event.data.newResourcesIcon } });

                clearInterval(compareStructures);
            } else {
                communicationPort.postMessage({ different: false, time: new Date().toISOString() });
                browser.action.setIcon({ path: { '48': event.data.noNewResourcesIcon } });
            }
        }, 900000);
    }
});

const getStructure = async (url, token) => {
// Uses fetch() to get the resources
   ...
};

const findDifferences = (newStructure, oldStructure) => {
   ...
};

If it is not possible, what are the viable options to achieve my desired design? Could reverting to manifest ver. 2 help?

Hopefully my description makes sense, and I think it is possible, as I have seen extensions send notifications when the browser is opened.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!