title: Chrome extension onConnect listener fires more than once from different tabs
body: <p>I'm creating an extension that works with this structure of .js files:</p>
  
  <p>Page A (content script) -> B (background script) -> Page C (content script)</p>
  
  <p>On page A, there are links that, when clicked, send a message to B. The idea of B is to open up a new tab and load page C; when C is fully loaded, it'll send a message back to B, which will send some data to C to fill in a form.</p>
  
  <p>This all works, but after putting in some logging I realized that my listeners in B are actually firing twice if more than one link is clicked from page A. Here's what the code looks like:</p>
  
  <p>Page A:</p>
  
  <pre><code>$(document).ready(function () {
    $('.click-test').click(function() {
      chrome.runtime.sendMessage({formData: here}, function(response) {
      });
    });
  });
  </code></pre>
  
  <p>B:</p>
  
  <pre><code>chrome.runtime.onMessage.addListener(
  
    function(request, sender, sendResponse) {
      chrome.runtime.onConnect.addListener(function(port) {
        console.assert(port.name == "formStatus");
        port.onMessage.addListener(function(msg) {
          if (msg.status == "formReady") {
              chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
                chrome.tabs.sendMessage(tabs[0].id, {formData: here}, function(response) {
                  // The below is being called twice incorrectly if two links from page A are clicked
                  console.log("sent message for tab ID " + tabs[0].id);
                });
              });
          }
        });
      });
      chrome.tabs.create({url: requestFormUrl});
  });
  </code></pre>
  
  <p>Page C:</p>
  
  <pre><code>$(document).ready(function() {
    chrome.runtime.onMessage.addListener(
      function(request, sender, sendResponse) {
        // Fill in form with request data    
    });
    var port = chrome.runtime.connect({name: "formStatus"});
    port.postMessage({status: "formReady"});
  });
  </code></pre>
  
  <p>When the first link on page A is clicked, things work fine. When the second link is clicked, the onConnect listener in B fires twice: first with the same data as the first click, then with the correct data corresponding to the second click.</p>
  
  <p>Is there some way to disable B's listener as soon as it fires once? Or, have I just made this code way too complicated?</p>