在 Safari 扩展中捕获关闭选项卡事件
我在 Apple 文档。
我尝试过:
injected.js
window.addEventListener("unload", function(){
// Probably closed.
// Now I need to tell it to the global page.
}, false);
,但我找不到将消息从注入脚本内发送到全局页面的方法。 消息和代理仅以相反的方式提及。
I cannot find something like "closeTab" event in the Apple Documentation.
I've tried:
injected.js
window.addEventListener("unload", function(){
// Probably closed.
// Now I need to tell it to the global page.
}, false);
but I can't find a way to send a message from within an injected script to the global page. Messages and Proxies mentions only other way around.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于 Safari 5.1 及以上版本,他们添加了 标签关闭您可以以通常的方式监听和处理的事件。
For Safari 5.1 onwards, they added a tab close event that you can listen for and handle in the usual way.
您可以将消息从注入的脚本分派到全局页面,如下所示:
safari.self.tab.dispatchMessage("messageName", messageData);
(您的全局页面必须有一些东西来捕获这些事件):
至于如何捕获“选项卡关闭”事件...您的猜测和我的一样好。我现在实际上正在尝试自己寻找答案。
You can dispatch a message from an injected script to a global page like so:
safari.self.tab.dispatchMessage("messageName", messageData);
(Your global page must have something to capture these events):
As far as how to capture "tab closed" events... your guess is as good as mine. I am actually trying to find that answer myself currently.
例如,在后台使用“validate”事件来检测选项卡开关
use the "validate" event in background to detect tab switches, for example
您可以直接向事件添加侦听器,但到目前为止,我还没有找到正确的侦听器来检测选项卡何时关闭。
You can add listeners directly to the events, but so far I haven't found the correct one to detect when a tab is closed.
到目前为止,我还没有找到一种方法让选项卡告诉全局页面它已经(或即将)关闭。一种糟糕的解决方法是在全局页面上设置一个计时器,该计时器将定期检查是否有任何选项卡已关闭。当活动窗口中的选项卡关闭时,将记录以下简单的代码:
此示例非常无用,因为它不提供有关已关闭的选项卡的任何信息,并且仅在移动选项卡时会产生误报。
So far I haven't found a way for a tab to tell the global page that it's been (or is about to be) closed. One crappy workaround is to set up a timer on the global page that will check periodically whether any tabs have been closed. The following simplistic code will log when a tab is closed in the active window:
This example is pretty useless, as it doesn't provide any information about the tab that was closed, and it will yield a false positive when a tab is merely moved.