0

I'm building my first chrome extension and i've hit a self-inflicted bug. I have in my popup.js file to launch the content script on the correct tab(I had trouble getting it to inject from the manifest file). This works all in well. My problem is when you close the extension and re-open it. The script now launches again and now I have two versions of the content.js script injected. Which this makes total sense. However, this is now causing duplicate actions. What I'm lost on is ensuring that the script only launches once. I can't seem to find a way to check if the script is already injected or not to evaluate if the popup.js file on load needs to do that. Here is my code below. Any help would be greatly appreciated.

//launch the content script on the correct tab
        chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {   
        chrome.tabs.executeScript(tabs[0].id, {
            file: "jquery-3.4.1.min.js"
            }, function(){ 
                chrome.tabs.executeScript(tabs[0].id, {
                file: "content.js"
                }, function(){})
            });
        });

Here is my attempted implementation of the linked post below:

(function() {
        if (window.hasRun === true)
            return true;  // Will ultimately be passed back to executeScript
        window.hasRun = true;
        // rest of code ... 
        chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {   
            chrome.tabs.executeScript(tabs[0].id, {
                file: "jquery-3.4.1.min.js"
                }, function(){ 
                    chrome.tabs.executeScript(tabs[0].id, {
                    file: "content.js"
                    }, function(){})
                }
            );
        });
        // No return value here, so the return value is "undefined" (without quotes).
    })()

Error received: enter image description here

Peter3
  • 2,549
  • 4
  • 20
  • 40
  • Possible duplicate of [Chrome extension: Checking if content script has been injected or not](https://stackoverflow.com/questions/34528785/chrome-extension-checking-if-content-script-has-been-injected-or-not) – wOxxOm Aug 30 '19 at 14:22
  • It is similar, but instead of from the background script, I was trying to do it from the popup.js. Open to ideas though :) – Peter3 Aug 30 '19 at 14:31
  • There's no difference and you can use the same code both in your popup script and the background script as you can see in the answers. – wOxxOm Aug 30 '19 at 14:35
  • This is where my lack of extension xp hurts me. How do I know when that background script will fire. I need it to on the correct URL – Peter3 Aug 30 '19 at 14:36
  • You don't need a background script at all. You can use the code shown in the answers in your popup script. – wOxxOm Aug 30 '19 at 14:38
  • I tested that out and found the script not executing at all. Perhaps I implemented it incorrectly. I appreciate the help so far – Peter3 Aug 30 '19 at 14:39
  • When you modify the content scripts you have to reload the extension on chrome://extensions page by clicking the reload icon on the card. – wOxxOm Aug 30 '19 at 14:41
  • I do, I end up with an error, I'll edit the idea above to show the attempted implementation with the error. The error is thrown on the line with (function().... – Peter3 Aug 30 '19 at 14:42
  • 1) The hasRun check should be inside the content script as explained in the answer, not in the popup script. 2) The executeScript part should be in inside the popup script. 3) Make sure you're running the popup by clicking the extension icon, not directly from disk because it'll be a normal file:// page, not an extension page. – wOxxOm Aug 30 '19 at 14:46
  • AHHH, Thank you, works like a charm. – Peter3 Aug 30 '19 at 14:57

0 Answers0