<分区>
我有一个 Firefox 扩展需要检查 onUnload 事件。基本上我想在用户禁用扩展程序时向我的服务器发送一条消息。
我尝试做的是向我的内容脚本之一发送一条消息,然后它会调用 XMLHttpRequest。这适用于扩展程序触发的任何其他事件,但看起来内容脚本甚至在消息传递之前就已卸载。
主.js
这是来自 main.js 脚本的代码:
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);
}
};
内容脚本
这是我附加到每个加载页面的内容脚本中的代码。
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);
}
});
发送事件代码
最后,这里是发送事件代码,仅供引用,我最初计划如何使用 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();
}
有没有办法使用 main.js 中的 XMLHttpRequest?
或者也许是一种触发 onUnload 事件的方法,但它是在扩展实际卸载之前触发的吗? (就像 beforeOnUnload 类型的事件)