title: Chrome Extension js: Sharing functions between background.js and popup.js
body: <p>Suppose I have a JavaScript function <code>foo()</code> which I want to execute in both the background and in <code>popup.html</code>.</p>
  
  <p>For example: it is executed every hour in the background of my Chrome extension, but can also be activated by the user from the popup menu (<code>popup.html</code>) on a button click.</p>
  
  <p>I currently have a <code>global.js</code> script which defines <code>foo()</code> and when I include calls to <code>foo()</code> in my <code>popup.js</code> document, they execute without issue. (If I include both scripts in <code>popup.html</code>)</p>
  
  <p>However, when I try to access <code>foo()</code> inside <code>background.js</code>, the calls do not execute (even if <code>global.js</code> is included in the "background" "manifest.json" extension file:</p>
  
  <pre><code>"background": {
      "persistent": true,
      "scripts": ["background.js", "global.js"]
  },
  </code></pre>
  
  <p>Is there a convenient way to share functions between <code>background.js</code> and <code>popup.js</code> (without copying the entire function into each)?</p>