title: Chrome Extension: Passing a Message to the Content Script
body: <p>This question has had a massive edit on Monday May 12, 2014</p>
  
  <p>I've figured out how to send a message to the content script with a very basic set up, however when I try adding a few things like an event listener or a window, things stop working...</p>
  
  <p>The manifest.json file stays the same through all of the different versions of the code:</p>
  
  <pre><code>{
      "manifest_version" : 2,
      "name" : "Message Test",
      "version" : "1.0",
  
      "browser_action": { 
          "default_popup": "popup.html"
      },
  
      "content_scripts": [
          {
          "matches" : ["&lt;all_urls&gt;"],
          "js": ["content-script.js"]
          }
      ]    
  }
  </code></pre>
  
  <p>The popup.html file stays relatively the same through all of the different versions:</p>
  
  <pre><code>&lt;html&gt;
    &lt;head&gt;
      &lt;script src="message.js"&gt;&lt;/script&gt;
    &lt;/head&gt;
  &lt;body&gt;
    &lt;button type="button" id="Popup Window"&gt;Popup Window&lt;/button&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The content-script.js file stays relatively the same:</p>
  
  <pre><code>var Mymessage;
  
  chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
      if (message.greeting == "Can you hear me?"){
          Mymessage = message.theMessage;
      }
      else{
          sendResponse({});
      }
      alert(Mymessage);
  });
  </code></pre>
  
  <p>It's this last file that keeps having problems...
  message.js:</p>
  
  <pre><code>window.addEventListener('load', function () {
      var myWindow;
      function startFunction() {
          myWindow = window.open("https://www.google.com/", "myWindow", "width=500,height=500");
  
          function MyMessage (){
            chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
              var tab = tabs[0];
              chrome.tabs.sendMessage(tab.id, {
                greeting: "Can you hear me?",
                theMessage: "Chrome Extension Message"
              }, function(response){});
            }
          window.setTimeout(MyMessage , 3000);
      }
      var Process = document.getElementById('Popup Window');
      Process.addEventListener('click', startFunction);
  });
  </code></pre>
  
  <p>If I remove the function <strong>MyMessage</strong> and <strong>window.setTimeout(MyMessage, 3000);</strong> the popup button successfully opens a new window.  If I remove the rest of the file except for the code from within the MyMessage function, whenever I'm on a random page and click its extension icon, the alert in the content-script.js file will go off.  I'm trying to change it so the message wont be sent until the user clicks a button in the popup.html and only after the popup created a new window.</p>