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

251
Views
Run Content Script on tabs opened from background script

So I have a small project where I have an extension that when I press its icon it opens some tabs using a background Script with a certain URLs that I will be updating

chrome.browserAction.onClicked.addListener(buttonClicked)
function buttonClicked (tab){
chrome.tabs.create({
    url:"https://google.com/h",
    active : false
  },callback)

}

now I need to to run a content script only on those tabs and I cant figure out how .. I have tried multiple things but cant seem to be able to do so

I am a new programmer so any advice of even guidelines would be appreciated

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

0

First things first, if you want to run content script on certain web-pages only than you can use Match Patterns (https://developer.chrome.com/docs/extensions/mv3/match_patterns/) in manifest.json file to run content script on matching URLs only. If that doesn't solve your problem than follow bellow approach:

When you create new tabs using background script you should append custom query parameters in the url, for example:

background.js

chrome.browserAction.onClicked.addListener(buttonClicked)
function buttonClicked (tab){
chrome.tabs.create({
    url:"https://google.com/h?customAction=1",
    active : false
  },callback)

then in the content script you can identify this query parameter by using window.location.href like this:

content.js

if (window.location.href.indexOf('customAction=1') >= 0) {
   // Call custom function from here
   someFunction();
}
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!