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