title: Detecting if code is being run as a Chrome Extension
body: <p>I am working with some code that needs to be run as a page, and if it's being run as a <a href="https://developer.chrome.com/docs/extensions/mv2/getstarted" rel="noreferrer">Chrome Extension</a>, I want to be able to do additional things. What I'm using is:</p>
  <pre><code>&lt;script&gt;
  if (chrome &amp;&amp; chrome.extension) {
      // extension stuff
  }
  &lt;/script&gt;
  </code></pre>
  <p>This seems like a good <a href="https://developer.chrome.com/docs/extensions/reference/extension" rel="noreferrer">capability</a> <a href="http://dev.opera.com/articles/view/using-capability-detection/" rel="noreferrer">detection</a>. Using user agent string causes me trouble because it's the same no matter the context (web page vs. extension).</p>
  <p>Question: are there other more reliable techniques for detecting if a piece of code is running inside a Chrome extension?</p>
  <p><strong>Update:</strong> I'm wondering if there's something I can put into my <a href="https://developer.chrome.com/docs/extensions/mv2/manifest" rel="noreferrer"><code>manifest.json</code></a> file that I can then read back. Note, that the extension I'm working on is not intended as a persistent thing that runs all the time, it's a content application that runs in a single window or browser tab and has no need to interact with other windows or tabs or anything else.</p>