One way that I can come up with now (simple and simple) is to use html5webstorage . Since you are using this code from your background or pop-up page, everything will be fine.
if(!localStorage.getItem("isAlreadyInjected")){ localStorage['isAlreadyInjected'] = "true"; chrome.browserAction.onClicked.addListener(function callback(tab){chrome.tabs.executeScript(null, {file: "content-script.js"});});}
So, for the first time, when the "isAlreadyInjected" storage value does not exist, a listener will be added. After that, even when the browser closes and reopens, this value will remain saved and therefore the listener will not be added to the extension.
UPDATE
Since your background page loads only once at the beginning, it may contain a variable that will not be reinitialized with a mouse click. Therefore, you can use this variable to do your job!
background.js
var isAlreadyInjected =false; function isInjected(){ if(!isAlreadyInjected ){ isAlreadyInjected=true; return false; } else return true; }
popup.js
var bgpage=chrome.extension.getBackgroundPage(); if(!bgpage.isInjected()){ chrome.browserAction.onClicked.addListener(function callback(tab) {chrome.tabs.executeScript(null, {file: "content-script.js"});}); }
or
var bgpage=chrome.extension.getBackgroundPage(); chrome.browserAction.onClicked.addListener(function callback(tab) { if(!bgpage.isInjected()){ chrome.tabs.executeScript(null, {file: "content-script.js"}); }});
source share