提交表单后关闭标签页

时间:2015-03-12 17:31:09

标签: javascript tabs firefox-addon firefox-addon-sdk onsubmit

我的插件的目标是执行以下操作:

  1. 打开指定网址的新标签页
  2. 将内容插入表单
  3. 提交该表格
  4. 关闭已打开的标签页

    插件能够完成第1步和第2步。但是,它会以适当的顺序一起执行第3步和第4步的问题。

  5. 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();
    

    问题

    每当postPB.js提交表单时,它都会向Main.js发送一条消息,以关闭该标签。但是,Main.js关闭选项卡,然后页面上的submit可以在服务器端完成。

    我找到了a question for a very similar problem。但他们使用的是php,所以我不认为他们的解决方案对我有用。

    我的问题

    在服务器端提交表单后,有没有办法从Main.js关闭标签?

0 个答案:

没有答案