title: Chrome / Firefox extension - Content script not listening for messages
body: <p>I am developing an extension for both Chrome and Firefox, and I have come across a problem.</p>
  
  <p>Basically, I am trying to get a content script to listen to messages using <code>chrome.runtime.onMessage.addListener(...)</code>, however it doesn't seem to work.</p>
  
  <p>I tested it by sending a message from the content script. The background script (<code>ml.js</code>) had a listener that worked fine, but the lsitener in the content script just didn't get the message.</p>
  
  <p>You can view the code in <a href="https://gist.github.com/Druzion/30c94bf17d4ea3a9a5852e4598124419" rel="noreferrer">this Gist</a> (or below).</p>
  
  <p><code>manifest.json</code>:</p>
  
  <pre><code>{
      "manifest_version": 2,
      "name": "Messaging Extension",
      "version": "1.0.0",
      "background": {
          "scripts": ["ml.js"]
      },
      "content_scripts": [
          {
              "matches": ["*://*.google.co.uk/*"],
              "js": ["cs.js"],
              "run_at": "document_end"
          }
      ]
  }
  </code></pre>
  
  <p><code>ml.js</code>:</p>
  
  <pre><code>// When receive message...
  chrome.runtime.onMessage.addListener(function(message) {
      if (message.key) {
          console.log('ML: First message received')
          // Send another message
          chrome.runtime.sendMessage({
              'foo': 'bar'
          })
      }
  })
  </code></pre>
  
  <p><code>cs.js</code>:</p>
  
  <pre><code>// Send message to ml.js
  chrome.runtime.sendMessage({
      'key': 'value'
  })
  chrome.runtime.onMessage.addListener(function(message) {
      console.log('CS: Second message received')
  })
  </code></pre>
  
  <p>When tested in Firefox (by loading the add-on at about:debugging and then visiting Google), <code>cs.js</code> sent the message, and <code>ml.js</code> logged the message to the console, however <code>cs.js</code> didn't log the message.</p>
  
  <p>I'll appreciate some help, thanks!</p>