title: Chrome extension communicate between injected script and popup.js
body: <p>I am trying to write a chrome extension in which I inject serveral different scripts which may potentially need to share data.  I am trying to use message passing as described <a href="http://I%20am%20trying%20to%20write%20a%20chrome%20extension%20in%20which%20I%20inject%20serveral%20different%20scripts%20which%20may%20potentially%20need%20to%20share%20data.%20%20I%20am%20trying%20to%20use%20message%20passing%20as%20described%20here:%20https://stackoverflow.com/questions/33063774/communication-from-an-injected-script-to-the-content-script-with-a-response/33064000#" rel="nofollow noreferrer">here</a>.  </p>
  
  <p>I can send a message from one of the injected scripts to another, but I can't seem to be able to send anything back to the extension.</p>
  
  <p>Code in the extension popup.js:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function injectLoad() {
      chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
          // query the active tab, which will be only one tab
          //and inject the script in it
          chrome.tabs.executeScript(tabs[0].id, {file: "load.js"});
      });
  }
  
  function  initPopup(  ) {
      console.log( "starting" );
      window.addEventListener( "something", function( evt ) {
        alert( "got " + evt.detail );
      }, false );
  }
  
  document.getElementById( 'loadit' ).addEventListener( 'click', injectLoad );
  
  initPopup();</code></pre>
  </div>
  </div>
  </p>
  
  <p>Code in the injected script:
  <div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function loadit() {
  
      window.dispatchEvent(new CustomEvent("something", {data: 'whatever'}));
  }
  
  loadit(); </code></pre>
  </div>
  </div>
  </p>
  
  <p>I never seem to get the event triggered.  Thanks</p>