How can I use XMLHttpRequest from main.js in a Firefox addon sdk extension. (or something similar) [duplicate]

牧云@^-^@ 提交于 2020-01-02 05:44:07

问题


I have a Firefox extension that needs to check for the onUnload event. Basically I want to send a message to my server when the user disables the extension.

What I tried doing, was to send a message to one of my content scripts, which would then call XMLHttpRequest. This works fine for any other event the extension triggers, but it would appear that the content scripts get unloaded before the message can even get passed.

main.js

Here is the code from the main.js script:

exports.onUnload = function(reason) {
    //unloadWorker comes from a PageMod 'onAttach: function(worker){}'
    //That is called every time a page loads, so it will a recent worker.
    if(unloadWorker != null) { 
        unloadWorker.port.emit("sendOnUnloadEvent", settings, reason);
    }
};

content script

Here is the code from the content script that I attach to every page that gets loaded.

self.port.on("sendOnUnloadEvent", function(settings, reason) {
    console.log("sending on unload event to servers");
    settings.subid = reason;
    if(reason != "shutdown") {
        sendEvent(("on_unload"), settings);
    }
});

Send event code

Finally, here is the send event code just for a reference for how I originally planned on using XMLHttpRequest:

sendEvent = function(eventName, settings) {

    if (!eventName) {
        eventName = "ping"; 
    }
    //Not the actual URL, but you get the idea.
    var url = 'http://example.com/sendData/?variables=value&var2=value2'

    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {

    }

    xhr.open("GET", url, true);
    xhr.send();
}

Is there anyway to use XMLHttpRequest from main.js?

Or perhaps a way to trigger the onUnload event, but have it trigger before the extension actually get unloaded? (Like a beforeOnUnload type event)


回答1:


The preferred way to make network requests from main.js is to use the Request object in module sdk/request.

However, Request can only make asynchronous requests, which means that the object will be out of scope when the function ends and the request will not occur.

Instead, you can use sdk/net/xhr to be able to use XMLHttpRequest and make a synchronous GET request on unload like so:

const {XMLHttpRequest} = require("sdk/net/xhr");
exports.onUnload = function(reason) {
    var url = 'http://mysite.com/sendData/?variables=value&var2=value2'
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, false);
    xhr.send(null);
});

But do note that sdk/net/xhr is marked as unstable and synchronous requests are blocking and frowned upon.




回答2:


My solution is slightly different than user2958125's, so I'll post it below.

const {Cc, Ci} = require("chrome");
var xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
//var xhr = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Components.interfaces.nsIXMLHttpRequest);

xhr.onreadystatechange = function() {

}

xhr.open("GET", url, true);
xhr.send();



回答3:


function reqListener () {
  console.log(this.responseText);
}

const {XMLHttpRequest} = require("sdk/net/xhr");
var url = "http://ya.ru";
var xhr = new XMLHttpRequest();
xhr.onload = reqListener;
xhr.open('GET', url, false);
xhr.send(null);


来源:https://stackoverflow.com/questions/19818321/how-can-i-use-xmlhttprequest-from-main-js-in-a-firefox-addon-sdk-extension-or

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!