I have a Firefox extension that needs to be checked for the onUnload event. Basically I want to send a message to my server when the user disables the extension.
What I tried to do was send a message to one of my content scripts, which XMLHttpRequest then calls. This works great for any other event that triggers the extension, but it looks like content scripts will be unloaded before the message can even go through.
main.js
Here is the code from main.js script:
exports.onUnload = function(reason) {
if(unloadWorker != null) {
unloadWorker.port.emit("sendOnUnloadEvent", settings, reason);
}
};
content script
Here is the code from the contents of the script that I attach to each page that loads.
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
, , XMLHttpRequest:
sendEvent = function(eventName, settings) {
if (!eventName) {
eventName = "ping";
}
var url = 'http://example.com/sendData/?variables=value&var2=value2'
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
}
xhr.open("GET", url, true);
xhr.send();
}
XMLHttpRequest main.js?
, , onUnload, , ? ( beforeOnUnload)