0

I would like to know why and how to fix:

My code is being injected a few times while being o specific website, can't find why.

It should be injected only when visiting a specific url (it can be different for example tickets/*****).

manifest.json

{
"manifest_version": 2,
"name": "ext",
"version": "1.619",
"description": "Does things:)",

"background": {
    "scripts": ["background.js"]
},

"content_scripts": [
    {
        "matches": ["https://www.example.com/agent/tickets/*"],
        "js": ["foreground.js"]
    }
],

"permissions": [
    "tabs",
    "activeTab",
    "http://*/",
    "https://*/"
]
}

background.js

chrome.tabs.onUpdated.addListener(function(id, info, tab){
    /*if (tab.status !== "complete"){
        return;
    }*/
    if(tab.url.indexOf("tickets") != -1){
       console.log("injected");
       chrome.tabs.executeScript(tab.id, {"file": "foreground.js"});
    }
});
  • Based on a quick read of your code every time the tab is updated you perform a new execution of the same content script. But you seem to be targeting one site in your content script but you have added permissions for _all_ sites. So that's probably where the problem is. – Andy Nov 11 '21 at 20:21
  • Remove `background` and `permissions` from manifest.json and `background.js`. – wOxxOm Nov 11 '21 at 20:24
  • @wOxxOm If i remove background and permissions, foreground script doesn't work. : – codeKzerer Nov 11 '21 at 20:29
  • It means your `matches` is incorrect, which can happen on a SPA site like youtube or twitter. For those you need `"matches": "https://www.example.com/*"` and then check location.href inside as shown [here](https://stackoverflow.com/a/39508954). – wOxxOm Nov 11 '21 at 20:36

0 Answers0