1

The goal of my addon is to do the following:

  1. open a new tab to the specified url
  2. insert content into a form
  3. submit that form
  4. close the opened tab

    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.

Main.js

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);
    });
}  

PostPB.js

//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) > div:nth-child(2) > select:nth-child(1) > 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();

Problem

Whenever postPB.js submits the form, it sends a message to Main.js that closes the tab. However, Main.js closes the tab before the submit on the page can finish on the server side.

I did find a question for a very similar problem. but they're using php, so I don't think their solution will work for me.

My Question to you

Is there a way to close the tab from Main.js after the form has been submitted on the server side?

Community
  • 1
  • 1
wimworks
  • 283
  • 3
  • 15
  • do you actually need to fill out the form? can't you just submit the data via XHR? – the8472 Mar 12 '15 at 19:45
  • I have never used XHR before. [This page on it](https://developer.mozilla.org/en-US/Add-ons/SDK/Low-Level_APIs/net_xhr) looks pretty discouraging. But if I were to use this, how would I make sure the tab closes after the data is submitted? – wimworks Mar 12 '15 at 20:13
  • If you use XHR you woudn't have to open a tab. However XHR could be daunting especially if your submitting image or other data. I would actually create an `iframe` in `Services.appShell.hiddenDOMWinow` (just do `var iframe = Services.appShell.hiddenDOMWinow.document.createElemen('iframe')` and use that) then load that page there, mod the form, submit it, destroy the `iframe`. If I had to avoid XHR. – Noitidart Mar 12 '15 at 21:03
  • 1
    You might have to link me to some tutorial pages so I can understand half of what you just said. It sounds useful, but I'm very unfamiliar with XHR and iframes. – wimworks Mar 12 '15 at 21:59
  • Ill post an example of how to load pages in hiddenDOMWindow below a little later :) – Noitidart Mar 13 '15 at 01:12

0 Answers0