title: Chrome extension - "Port error: Could not establish connection. Receiving end does not exist"
body: <p>Trying to communicate between my background.js and contentscript.js</p>
  
  <p><strong>manifest.json</strong></p>
  
  <pre><code>{
      "name": "xxx",
      "version": "0.1",
      "manifest_version": 2,
  
      "permissions": [
          "tabs"
      ],
  
      "description": "xxx",
      "icons": { "16": "icon16.png",
                 "48": "icon48.png",
                 "128": "icon128.png" },
  
      "browser_action": {
          "default_title" : "xxx",
          "default_icon": "icon16.png",
          "default_popup": "popup.html"
      },
  
      "background": {
          "scripts": ["background.js"]
      },
  
      "content_scripts" : [
      {
          "matches" : [ "http://*/*" ],
          "js" : [ "contentscript.js", "jquery.js" ]
      }
    ]
  }
  </code></pre>
  
  <p><strong>background.js</strong></p>
  
  <pre><code>var listePdt = {};
  var selectedPdt = null;
  var selectedId = null;
  
  function updatePdt(tabId)
  {
      chrome.tabs.sendMessage(tabId, {}, function(pdt) {
              chrome.pageAction.show(tabId);
      });
  }
  
  chrome.tabs.onUpdated.addListener(function(tabId, change, tab) {
      if(change.status == "complete")
      updatePdt(tabId);
  });
  
  chrome.tabs.onSelectionChanged.addListener(function(tabId, info) {
      selectedId = tabId;
      // and other things
  });
  
  chrome.tabs.getSelected(null, function(tab) {
      updatePdt(tab.id);
  });
  </code></pre>
  
  <p><strong>contentscript.js</strong></p>
  
  <pre><code>if(window == top)
  {
      chrome.extension.onMessage.addListener(
          function(req, sender, sendResponse) {
              sendResponse(findPdt());
          }
      );
  }
  
  var findPdt = function() {
  
      // operations on a string
  
      return "string";
  }
  </code></pre>
  
  <p>But I get the following error "Port error: Could not establish connection. Receiving end does not exist" in console for generated background page ... dont understand why .</p>
  
  <p>Any help plz ?</p>