title: How to access page variables from Chrome extension background script
body: <p>With a content script you can inject a script tag into the DOM in order to access variables in the original page (<a href="https://stackoverflow.com/questions/3955803/page-variables-in-content-script">as explained in this question</a>).</p>
  
  <p>I want to avoid injecting my code into every page and instead only do that when the user clicks on the extension icon.</p>
  
  <p>When I tried using the same code as for the content script the values were undefined, although the script was inserted correctly.</p>
  
  <p>Is this possible? Otherwise is using a content script and communicating with it the preferred solution?</p>
  
  <p>Here is the code I'm using:</p>
  
  <pre><code>var scr = document.createElement("script");
  scr.type="text/javascript";
  scr.innerHTML = "setInterval('console.log(window.testVar)', 1000)"
  document.body.appendChild(scr)
  </code></pre>
  
  <hr>
  
  <p>Manifest excerpt: </p>
  
  <pre><code> "permissions": [
      "tabs",
      "http://*/*", "https://*/*"
    ],
    "background": {
      "scripts": ["inject.js"]
    },
  </code></pre>