title: Sending message from popup to content script - Chrome Extension
body: <p>I want to update the html in popup.html when I open it through the browser action button. The popup.js should send a message to the content script running on the current tab, and should receive a response and update the html. However the content script does not receive any message, therefore not sending a proper response.</p>
  
  <p>Content.js</p>
  
  <pre class="lang-js prettyprint-override"><code>var text = "hello";
  chrome.runtime.onMessage.addListener(
      function(message, sender, sendResponse) {
          switch(message.type) {
              case "getText":
                  sendResponse(text);
              break;
          }
      }
  );
  </code></pre>
  
  <p>Popup.js</p>
  
  <pre class="lang-js prettyprint-override"><code>chrome.tabs.getCurrent(function(tab){
      chrome.tabs.sendMessage(tab.id, {type:"getText"}, function(response){
          alert(response)
          $("#text").text(response);
      });
  });
  </code></pre>
  
  <p>Manifest.json</p>
  
  <pre class="lang-json prettyprint-override"><code>{
    "manifest_version": 2,
    "name": "It's Just A Name",
    "description": "This extension is able to",
    "version": "1.0",
    "permissions" : ["tabs"],
    "browser_action": {
      "default_icon": "icon.png",
      "default_popup": "popup.html",
      "default_title": "Click here!"
    },
    "content_scripts": [
    {
      "matches": ["https://*/*"],
      "js": ["jquery.min.js","content.js"]
    }]
  }
  </code></pre>
  
  <p>Popup.html</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;!doctype html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;Title&lt;/title&gt;
          &lt;style&gt;
              body {
                  font-family: "Segoe UI", "Lucida Grande", Tahoma, sans-serif;
                  font-size: 100%;
              }
              #status {
                  white-space: pre;
                  text-overflow: ellipsis;
                  overflow: hidden;
                  max-width: 400px;
              }
          &lt;/style&gt;
          &lt;script src="popup.js"&gt;&lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;p id="text"&gt;&lt;/p&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>