title: Chrome extension: Checking if content script has been injected or not
body: <p>I'm developing a Chrome extension. Instead of using <code>manifest.json</code> to match content script for all URLs, I lazily inject the content script by calling <code>chrome.tabs.executeScript</code> when user do click the extension icon.</p>
  
  <p>What I'm trying is to avoid executing the script more than once. So I have following code in my content script:</p>
  
  <pre class="lang-js prettyprint-override"><code>if (!window.ALREADY_INJECTED_FLAG) {
      window.ALREADY_INJECTED_FLAG = true
      init() // &lt;- All side effects go here
  }
  </code></pre>
  
  <p><strong>Question #1</strong>, is this safe enough to naively call <code>chrome.tabs.executeScript</code> every time the extension icon got clicked? In other words, is this idempotent?</p>
  
  <p><strong>Question #2</strong>, is there a similar method for <code>chrome.tabs.insertCSS</code>?</p>
  
  <p>It seems impossible to check the content script inject status in the backgroud script since it can not access the DOM of web page. I've tried a ping/pong method for checking any content script instance is alive. But this introduces an overhead and complexity of designing the ping-timeout.</p>
  
  <p><strong>Question #3</strong>, any better method for background script to check the inject status of content script, so I can just prevent calling <code>chrome.tabs.executeScript</code> every time when user clicked the icon?</p>
  
  <p>Thanks in advance!</p>