简体   繁体   English

Firefox扩展中的XMLHttpRequest

[英]XMLHttpRequest from Firefox Extension

I'm using XMLHttpRequest to exchange data between server and Firefox extension I'm developing. 我正在使用XMLHttpRequest在正在开发的服务器和Firefox扩展之间交换数据。 Unfortunately, those requests seem somehow connected with the currently open page - if I try to issue request while the current tab is closing, it will fail with an error. 不幸的是,这些请求似乎以某种方式与当前打开的页面相关联-如果我尝试在当前选项卡关闭时发出请求,它将失败并显示错误。 How can I make my requests originate from the extension itself, independently of what's going on in tabs? 如何使我的请求源自扩展本身,而与选项卡中的情况无关?

EDIT: Here is the code that reproduces this problem. 编辑:这是重现此问题的代码。 It's run as the main extension body (I based my design on the "Hello world" tutorial from http://kb.mozillazine.org/Getting_started_with_extension_development , so no Add-on SDK). 它作为主要扩展主体运行(我的设计基于http://kb.mozillazine.org/Getting_started_with_extension_development的“ Hello world”教程,因此没有附加SDK)。 This means that it's executed in the same place as the code from "overlay.js" in above tutorial. 这意味着它与上面教程中“ overlay.js”中的代码在同一位置执行。

function createXMLHttpRequest() {
    return Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"]
       .createInstance(Components.interfaces.nsIXMLHttpRequest);    
}

function issueRequest() {
    var req = createXMLHttpRequest();
    req.open("GET", "http://google.com", true);
    req.addEventListener("load", function(event) {
        alert("SUCCES");
    });
    req.addEventListener("error", function(event) {
        alert("ERROR");
    });
    req.send();
};

window.addEventListener("DOMContentLoaded",  function(event) {
    issueRequest();

    var doc = event.originalTarget;
    var win = doc.defaultView;

    win.addEventListener("unload", function(event) {
        issueRequest();
    });
});

This results in "SUCCESS" after opening of new tab, and "ERROR" after closing it. 打开新标签页后,结果为“成功”,关闭新标签页后,结果为“错误”。 I would prefer to have two SUCCESSES. 我希望有两个成功。

If that script is running in a browser window overlay then you attached your DOMContentLoaded handler to the wrong node - you will only get notified when the browser window itself loads. 如果该脚本在浏览器窗口叠加层中运行,则您将DOMContentLoaded处理程序附加到错误的节点上-仅在加载浏览器窗口本身时才会收到通知。 Consequently, your unload handler waits for the browser window to the closed, you probably intended to wait for a tab to be closed. 因此,您的unload处理程序等待浏览器窗口关闭,您可能打算等待选项卡关闭。 The correct code would look like this: 正确的代码如下所示:

// Wait for the browser window to load before doing anything
window.addEventListener("load", function() {
  // Attach a listener to the tabbrowser to get notified about new pages
  window.gBrowser.addEventListener("DOMContentLoaded", function(event) {
    issueRequest();

    var doc = event.originalTarget;
    var win = doc.defaultView;

    win.addEventListener("unload", function(event) {
      issueRequest();
    });
  }, false);
}, false)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM