在 window.onbeforeunload 中等待消息发送?

发布于 2024-09-14 08:05:57 字数 417 浏览 2 评论 0原文

我正在摆弄 Safari 扩展,我希望我的扩展能够响应用户打开或关闭选项卡。

我有一个扩展注入一个脚本,该脚本在新选项卡打开时运行,并通知全局页面有一个新选项卡打开。我希望脚本在选项卡关闭时也通知全局页面。为此,我让注入的脚本设置 window.onbeforeunload 函数来调用 safari.self.tab.dispatchMessage。这可行,但问题是选项卡关闭并且 Javascript 在消息通过之前被终止。例如,如果我在 dispatchMessage 之后放置一个 alert 以防止选项卡在用户单击“确定”之前关闭,则消息会正常运行,但这显然不是非常好的用户体验。

有没有办法在选项卡关闭之前购买一点额外的时间,或者是否有更好的功能可以覆盖此功能或其他功能?

I'm messing around with Safari extensions, and I'd like my extension to respond to the user opening or closing tabs.

I have the extension injecting a script that runs when the new tab opens and notifies the global page that there's a new tab open. I want the script to also notify the global page when the tab closes. To do this, I'm having the injected script set the window.onbeforeunload function to call safari.self.tab.dispatchMessage. This works, but the problem is the tab closes and the Javascript gets killed before the message goes through. If, for example, I put an alert after the dispatchMessage to prevent the tab from closing until the user clicks OK, the message goes through fine, but this obviously isn't a very good user experience.

Is there a way to buy a little extra time right before the tab closes, or is there a better function to override for this or something?

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

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

发布评论

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

评论(1

痴意少年 2024-09-21 08:05:57

问题是所有消息保存一个 (canLoad) 都是异步的。在我看来,canLoad 本身就有点像黑客,但当我出于某种原因迫切需要同步消息传递时,我有效地使用了它。不久前我问了一个类似的问题,这可能有助于说明canLoad解决方案/黑客。

The problem is that all messages save one (canLoad) are asynchronous. IMO, the canLoad is a bit of a hack itself, but I've used it effectively when I desperately need synchronous messaging for some reason. I asked an analogous question a while back that may help illustrate the canLoad solution/hack.

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