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

147
Views
Chrome Extension V3 - Service Worker Confusion

Never made a Chrome extension before. I'm confused about the service workers. All I want to do is when a user clicks on the extension icon, is to run some JavaScript that manipulates the DOM. I do have my background.js script identified as a service worker.

    "background": {
        "service_worker": "./background.js"
    },

What I don't understand (after watching a bunch of Youtube videos) is how my background.js file should be structured? I just want it to load my content.js file. I have some mocked up example below to give an idea.

chrome.tabs.[some api event]((tabId, changeInfo, tab) => {
    if (changeInfo.status === 'complete') {
        chrome.scripting.executeScript({
            target: { tabId: tabId },
            files: ["./content.js"
        })
    }
});

I have no idea what to do in the service worker file? Do I just register the service worker and then execute my content.js file? Not even sure which event would be the correct one to use in the API to just run my content.js? Any input or links to pages to help me understand this further would be greatly appreciated.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need chrome.action API and its onClicked event + executeScript, not chrome.tabs.

manifest.json:

  "action": {"default_title": "Click me"},
  "background": {"service_worker": "background.js"},
  "permissions": ["scripting", "activeTab"],

background.js (registered automatically, no need to do it explicitly).

chrome.action.onClicked.addListener(tab => {
  chrome.scripting.executeScript({
    target: {tabId: tab.id},
    files: ['content.js'],
  });
});

content.js:

// here you can access the web page DOM
console.log('Yay');
about 4 years ago · Juan Pablo Isaza Report
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!