在 window.onbeforeunload 中等待消息发送?
我正在摆弄 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?
问题是所有消息保存一个 (
canLoad
) 都是异步的。在我看来,canLoad
本身就有点像黑客,但当我出于某种原因迫切需要同步消息传递时,我有效地使用了它。不久前我问了一个类似的问题,这可能有助于说明canLoad
解决方案/黑客。The problem is that all messages save one (
canLoad
) are asynchronous. IMO, thecanLoad
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 thecanLoad
solution/hack.