title: Close a tab after submitting a form
body: <h2>The goal of my addon is to do the following:</h2>
  
  <ol>
  <li>open a new tab to the specified url  </li>
  <li>insert content into a form  </li>
  <li>submit that form  </li>
  <li><p>close the opened tab</p>
  
  <p>The addon is able to do steps 1 and 2 just fine. However it runs into issues doing steps 3 and 4 in a proper sequence together.</p></li>
  </ol>
  
  <h2>Main.js</h2>
  
  <pre><code>var data = require("sdk/self").data;
  var tabs = require("sdk/tabs");
  
  console.log("Step 1: addon started");
  // Creates the button
  require("sdk/ui/button/action").ActionButton({
    id: "make-post",
    label: "Make post",
    icon: {
      "16": "./icon-16.png",
      "32": "./icon-32.png",
      "64": "./icon-64.png"
    },
    onClick: handleClick
  });
  
  function handleClick(state) {
  console.log("Step 9: handleClick started");
  tabs.open(sites.urls[0]);
  handleTab(0);
  }
  //this is our cargo.
  var Cargo = {
      Title: "Sample Title!",
      Content: new Date()
  };
  //this is in an array for a good reason. don't worry about it.
  var sites = {
  urls: ["http://pastebin.com/"],
  scripts: ["postPB.js"]
  };
  
  function handleTab(X) 
  {
  //I tried tabs.on('load'.. and that didn't fix the problem
      tabs.on('ready', function RunPostScript(tab) 
      {
          console.log("The tab is ready");
          worker = tab.attach(
          {
                contentScriptFile: sites.scripts[X],
                contentScriptOptions: 
                {
                  Cargo
                }
          });
          worker.port.once("myMessage", function handleMyMessage() 
          {
              console.log("Shuttin down!");
              tab.close();
              worker.destroy();
          });
          tabs.removeListener("ready", RunPostScript);
      });
  }  
  </code></pre>
  
  <h2>PostPB.js</h2>
  
  <pre><code>//the listener that should close the tab after submit
  document.addEventListener("submit", function(event) {
  console.log("Unloading now");
  self.port.emit("myMessage");
  }, false);
  
  //this inserts the post title
  var titlePort = function(x)
  {
      var zA = document.querySelector(".post_input");
      //this sets the paste to expire in 10 minutes
      document.querySelector("div.form_frame:nth-child(2) &gt; div:nth-child(2) &gt; select:nth-child(1) &gt; option:nth-child(1)").value="10M";
      zA.value = x;
      console.log("Title inserted");
  };
  //this inserts the post content
  var contentPort = function(x)
  {
      var zA = document.querySelector("#paste_code");
      zA.value = x;
      console.log("Content inserted");
  };
  
  //hits the submit button
  var ShipIt = function()
  {
      document.querySelector("#submit").click()
      //self.port.emit("myMessage");
  };
  
  //this excecutes all the functions in a specified sequence
  var Finalize = function()
  {
      titlePort(self.options.Cargo.Title);
      contentPort("Test: " + self.options.Cargo.Content + ". If this post is visible, the test was successful.");
      ShipIt();
  };
  
  Finalize();
  </code></pre>
  
  <h2>Problem</h2>
  
  <p>Whenever <code>postPB.js</code> submits the form, it sends a message to <code>Main.js</code> that closes the tab. However, <code>Main.js</code> closes the tab before the <code>submit</code> on the page can finish on the server side.  </p>
  
  <p>I did find <a href="https://stackoverflow.com/questions/8355316/how-to-close-browser-tab-after-submitting-a-form">a question for a very similar problem</a>. but they're using php, so I don't think their solution will work for me.</p>
  
  <h2>My Question to you</h2>
  
  <p>Is there a way to close the tab from <code>Main.js</code> after the form has been submitted on the server side?</p>