inject script works multiple times

554 views
Skip to first unread message

Vivek Sagar

unread,
Apr 17, 2023, 2:04:04 PM4/17/23
to Chromium Extensions
i have created a extension that can work on some site so on that site i want to inject script when the user click on extension icon popup does not show instead injected script into the website which change the appearance etc. But the problem is that if i click multiple times the script get injected multiple times 
this is the code:

               
               
chrome.action.onClicked.addListener(function (){
   
    chrome.tabs.query({active: true,currentWindow: true}, function(tabs) {
        var tabURL = tabs[0].url;
        var activeScript;
        if (tabURL?.startsWith("https://www.youtube.com/")){
            chrome.scripting.executeScript({
                target: {tabId: tabs[0].id},
                files: ['content.js']
               
            }).then(activeScript = "true");
        }
    });        
           
})


Simeon Vincent

unread,
Apr 17, 2023, 2:23:30 PM4/17/23
to Vivek Sagar, Chromium Extensions
Yep, the way your code is set it every time you click the browser action the content script will be injected into the page. If this is not your desired behavior, you can address it by either changing your script injection logic or changing the script itself. 

Tracking script injection
In order to prevent repeat injections, you will need to add additional logic to track whether the script has been injected into a given tab. To do this, you would need to track both when your scripts are injected and when the tab's content changes in order to know when to re-inject. For your purposes at the moment this is probably more work than necessary. 

Modify the content script
A more direct approach is to tweak your content script so that it will quit early if it has already run. Here's a very simple before and after example.

content.js before

console.log("Content script has run!");

content.js after

if (typeof contentjsRan !== "undefined") {
console.log("Content script has run!");
}
var contentjsRan = true;


Simeon - @dotproto


--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/a82851fa-43f5-4f20-abfb-af6c10619ca2n%40chromium.org.
Message has been deleted

wOxxOm

unread,
Apr 18, 2023, 10:22:42 AM4/18/23
to Chromium Extensions, wOxxOm, Simeon Vincent, Chromium Extensions, Vivek Sagar
[Reposting with the fixed code]

The check should use an explicit literal value not just a typeof, otherwise it'll fail on a page that has an element with id=contentjsRan attribute, which per the specification creates an implicit global variable.

Here's a reliable check:

if (window.contentjsRan === true) {
// console.log("Content script has run!");
} else {
window.contentjsRan = true;
// do the work here
}

P.S. It is reliable only when used in the default (ISOLATED) world and not when used with `world: 'MAIN'` where the global environment can be changed by the web page.

On Tuesday, April 18, 2023 at 5:21:12 PM UTC+3 wOxxOm wrote:
The check should use an explicit literal value not just a typeof, otherwise it'll fail on a page that has an element with id=contentjsRan attribute, which per the specification creates an implicit global variable.

Here's a reliable check:

if (window.contentjsRan !== true) {
console.log("Content script has run!");
}
window.contentjsRan = true;

P.S. It is reliable only when used in the default (ISOLATED) world and not when used with `world: 'MAIN'` where the global environment can be changed by the web page.
Reply all
Reply to author
Forward
0 new messages