关闭 Firefox 时发送 XMLHttpRequest

发布于 2024-11-04 06:51:44 字数 279 浏览 0 评论 0原文

我有一个问题。我想从我的 Firefox 扩展发送 XML HTTP 请求 (XHR),以便我的服务器知道 Firefox 何时关闭。

现在我发现的问题是:

onunload:当 Firefox 关闭时,xmlhttprequest 被中止,阻止它接收任何请求

onbeforeunload:它似乎只在 Firefox 关闭时启动重新启动,并没有关闭!

onclose:与onunload相同的问题。

那我该怎么办呢?

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 xmlhttprequest 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少钕鈤記 2024-11-11 06:51:44

正如 Neil 所说,同步 XHR 请求将允许请求完全完成。但是,如果您计划将插件发布到官方 Mozilla 存储库,则同步 XHR 请求将成为拒绝的理由 (来源)。

您可能还想查看一些观察者通知,特别是处理应用程序关闭的通知。另外,如果您尝试开发浏览器模型,您可能也会喜欢 user-interaction-inactive 观察者通知。 ;)

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. ;)

葮薆情 2024-11-11 06:51:44

对于任何可能偶然发现的人。我对此有一个非常简单的解决方案。
我正在开发一个基于套接字(类似)的项目,我也遇到了同样的问题。
这是我的解决方案:

function freezeScreen(ms){

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

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

};

这将强制浏览器窗口冻结给定的毫秒数。

提交异步请求后,立即调用 freezeScreen(例如 50 毫秒)。用户可能不会注意到它,并且它为客户端提供了足够的时间在客户端关闭之前发送请求。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文