I am developing a Chrome extension. Instead of using manifest.jsonto match the contents of the script for all URLs, I lazily insert the contents of the script, invoking chrome.tabs.executeScriptwhen the user clicks the extension icon.
I try to avoid running the script more than once. Therefore, I have the following code in my script content:
if (!window.ALREADY_INJECTED_FLAG) {
window.ALREADY_INJECTED_FLAG = true
init()
}
Question No. 1 , is it safe enough for a naive call chrome.tabs.executeScriptevery time you click on the extension icon? In other words, is it idempotent?
Question # 2 , is there a similar method for chrome.tabs.insertCSS?
It seems impossible to check the contents of the script to insert the status into the backgroud script, since it cannot access the DOM of the web page. I tried using the ping / pong method to check the contents of the script instance. But this creates the overhead and complexity of designing a ping timeout.
Question # 3 , is there any better method for a background script to check the input status of the contents of a script, so can I just prohibit the call chrome.tabs.executeScriptevery time the user clicks the icon?
Thanks in advance!
source
share