title: "Attempting to use a disconnected port object" while sending message from content to popup in Chrome extension
body: <p>So, I need to pass messages from popup to content and vice versa. Currently here is what I am doing now, but this does not work how it's supposed to work:</p>
  
  <p>manifest.json:</p>
  
  <pre><code>{
    // Required
      "manifest_version": 2,
      "name": "Extension name",
      "version": "0.1",
      "content_scripts": [
      {
          "matches": ["*://*/*"],
          "js": ["content.js"]
      }
      ],
      "browser_action" : {
          "default_title" : "Extension name",
          "default_popup" : "popup.html"
      },
      "permissions": [
          "tabs",
          "*://*/*",
          "contextMenus"
      ]
  
  }
  </code></pre>
  
  <p>popup.js</p>
  
  <pre><code>var port = "";
  window.onload = function() 
  {
      document.getElementById("captureImage").onclick = captureImage;
  }
  
  
  function captureImage(isBig = true)
  {
      chrome.tabs.query({'active': true}, function (tabs) {
          port = chrome.tabs.connect(tabs[0].id, {name: "Besada"});
          port.onMessage.addListener(function(msg) {
              alert(msg.message);
          });
          port.postMessage({message: "getCoords"});
      });
  }
  </code></pre>
  
  <p>and content.js</p>
  
  <pre><code>chrome.runtime.onConnect.addListener(function(port){
      port.onMessage.addListener(function(msg) {
  
      });
  
      port.postMessage({message: "hello!"});  // this message I receive
  
      document.onmouseup = function(e) {
          port.postMessage({message: "hello!"}); // this message produces an error
      };
  });
  </code></pre>
  
  <p>What I need is message to be sent when user releases the left mouse button. Here is what happens: when I am clicking a button in my <code>popup.html</code>, the alert with the text "Hello" shows, but when I am clicking anywhere in the page after that, I am getting an error: <code>Uncaught Error: Attempting to use a disconnected port object</code></p>
  
  <p>What am I doing wrong and how can I fix this?</p>
  
  <p>My guess the port is disconnecting when popup closes when I click on page, but I'm not really sure about this.</p>