title: Chrome extension message passing from popup to content
body: <p>I'm trying to create a chrome extension to learn front-end technologies, and got stuck on message passing between popup and content script.</p>
  
  <p>The following is what I'm trying to do: 
   1. Let background page hold a global var. 
   2. When user clicking a button on the popup html, the global var is modified. Meanwhile, the popup sends the global var to the content scripts for all of the tabs.</p>
  
  <p>In the background.js I have:</p>
  
  <pre><code>var settings = {
      version: 1,
      enabled: false
  };
  </code></pre>
  
  <p>popup.js:</p>
  
  <pre><code>$(document).ready(function(){
      $("#switcher").click(function( event ) {
          event.preventDefault();
  
          var bg = chrome.extension.getBackgroundPage();
          var settings = bg.settings;
          settings.enabled = !settings.enabled;
  
          // send message to the content for all the tabs
          chrome.tabs.query({active: true}, function (tabs) {
              for (var i = 0; i &lt; tabs.length; ++i) {
                  console.log("sending message to tab " + i);
                  chrome.runtime.sendMessage(tabs[i].id, {enabled: settings.enabled}, function(response) {
                      console.log(response);
                  });
              }
          });
      });
  });
  </code></pre>
  
  <p>Finally, the content.js:</p>
  
  <pre><code>$(document).ready(function(){
      chrome.runtime.onMessage.addListener(
          function (request, sender, sendResponse) {
              console.log("request is: " + request.enabled);
  
              sendResponse("get it");
          }
      );
  });
  </code></pre>
  
  <p>I tried to debug it, but I found the 'sendMessage' function never got returned back.. and the 'onMessage' never got triggered. Did I miss something?</p>
  
  <p>My manifest file:</p>
  
  <pre><code>{
      "name": "__MSG_appName__",
      "version": "0.0.1",
      "manifest_version": 2,
      "description": "__MSG_appDescription__",
      "icons": {
          "16": "images/icon-16.png",
          "128": "images/icon-128.png"
      },
      "default_locale": "en",
      "permissions": [
          "contextMenus", "storage", "tabs"
      ],
      "background": {
          "scripts": [
              "scripts/background.js"
          ],
          "persistent": true
      },
      "browser_action": {
          "default_icon": {
              "19": "images/icon-19.png",
              "38": "images/icon-38.png"
          },
          "default_popup": "popup.html"
      },
      "content_scripts": [
          {
              "run_at":"document_start",
              "all_frames":true,
              "matches": ["*://*/*"],
              "js": ["bower_components/jquery/dist/jquery.min.js", "scripts/content.js"]
          }
      ],
      "web_accessible_resources": ["bower_components/jquery/dist/jquery.min.map"]
  }
  </code></pre>