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