开发者

Send out XMLHttpRequest on closing Firefox

I have a problem. I would like to send out an XML HTTP Request (XHR) from my Firefox extension so that my server will know when Firefox has been closed.

Now the problems I found are these:

onunload: the xmlht开发者_高级运维tprequest is aborted when Firefox is closed, preventing it from receiving any request

onbeforeunload: it seems to only be initiated when Firefox is restarted, and not closed!

onclose: the same problem as onunload.

So what should I do?


Like Neil said, a synchronous XHR request would allow the request to complete fully. However if you plan to publish your addon to official Mozilla repository, synchronous XHR requests are grounds for rejection (source).

You may also want to check out some of the Observer Notification, specifically the ones dealing with application shutdown. Also, if you're trying to develop a model of the browser, you may like the user-interaction-inactive observer notification as well. ;)


For anyone who may stumble upon. I have a very simple solution to this. I am working on a socket(-like) based project and the same problem also occurred to me. Here is my solution:

function freezeScreen(ms){

    var s=(new Date).getTime();

    while(((new Date).getTime())-s<ms){}

};

This will force the browser window to freeze for the given amount of ms.

Just after you submit your asynchronous request make a call to freezeScreen with say 50ms. The user probably wont notice it and it gives the client enough time to send out the request before the client shuts down.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜