Safari 5 扩展:如何检测窗口的当前选项卡何时发生更改?
我有一个包含工具栏的 Safari 5 扩展。每当当前选项卡发生更改时,该工具栏都应该更新。我想从我的酒吧脚本中做这样的事情:
safari.self.browserWindow.addEventListener("activeTab", tabChanged, false);
但是,这似乎不起作用。我也尝试了许多其他事件名称:
- activeTab
- activeTabChanged
- onActiveTab
- onActiveTabChanged
- tab
- tabChanged
- onTab
- onTabChanged
- SelectionChanged
- onSelectionChanged
有谁知道如何检测活动选项卡何时更改?
这并不是说这有任何关系,但看起来我会在 Chrome 中这样做:
chrome.tabs.onSelectionChanged.addListener(tabChanged);
I have a Safari 5 extension that contains a toolbar. Whenever the current tab changes, that toolbar should be updated. I would like to do something like this from my bar's script:
safari.self.browserWindow.addEventListener("activeTab", tabChanged, false);
However, that doesn't seem to work. I have tried a number of other event names as well:
- activeTab
- activeTabChanged
- onActiveTab
- onActiveTabChanged
- tab
- tabChanged
- onTab
- onTabChanged
- selectionChanged
- onSelectionChanged
Does anybody know how to detect when the active tab changes?
Not that this is in any way related, but it looks like I would do this in Chrome with:
chrome.tabs.onSelectionChanged.addListener(tabChanged);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
Safari 5.1 有几个新的扩展事件,包括当窗口或选项卡获得焦点时触发的“activate”事件。
https://developer.apple.com/documentation/safariextensions/safariactivateevent
Safari 5.1 has several new events for extensions, including an "activate" event that fires when a window or tab is focused.
https://developer.apple.com/documentation/safariextensions/safariactivateevent
这就是您正在寻找的活动。我不确定,但我认为这是扩展 api 的新添加。您可以放入
global.html
或popover.html
That's the event you are looking for. I'm not sure, but i think is a new addition to the extensions api. You can put in
global.html
or in thepopover.html
我同意 @imiaou 的回答:从苹果的文档来看,似乎没有办法做到这一点:(。
因为我需要检测我的扩展程序的选项卡更改(我从 Chrome 移植过来),我做了以下基于轮询的解决方法,它似乎工作正常(在我的全局页面中):
我对不断轮询浏览器感到不满意,但在苹果添加对这些选项卡事件的支持之前,我看不到其他解决方法。如果您的扩展程序可以承受相对宽松的选项卡切换事件触发延迟,那么这可能是目前合理的解决方法(1.5 秒。最大延迟对于我的扩展程序来说是可以接受的,并且感觉不会减慢浏览器速度)。
I agree with @imiaou 's answer: from looking at Apple's docs there doesn't seem to be a way to do this :(.
Since I needed to detect tab changes for my extension (which I'm porting over from Chrome), I did the following polling-based workaround which seems to be working fine (in my global page):
I'm unhappy with constantly polling the browser, but I see no other way around this until Apple adds support for these tab-events. If your extension can live with a relatively relaxed tab-switch event trigger latency then this could be a reasonable workaround for now (1.5 sec. max latency is acceptable for my extension, and it doesn't feel like its slowing down the browser).
虽然Safari没有Chrome特定的选项卡相关API,但它确实对这个问题有完美的解决方案。
@Galt 已经完成了 99%,其想法是向注入的 JavaScript 添加事件侦听器,并将该信息
dispatchMessage
发送到您的扩展。您正在寻找的事件处理程序名为
focus
,每次选择选项卡或窗口时都会触发该事件处理程序。在注入的代码中:
然后,您可以使用与 safari.application.activeBrowserWindow.activeTab 相关的数据更新扩展程序的 UI
While Safari doesn't have Chrome's specific tab-related API, it does have a perfect solution to this problem.
@Galt was 99% of the way there, with the idea to add an event listener to your injected JavaScript and to
dispatchMessage
that information to your extension.The event handler you're looking for is named
focus
, and gets fired every time a tab or window gets selected.In your injected code:
You can then update your extension's UI, with the data relevant to
safari.application.activeBrowserWindow.activeTab
我发现这个方法比焦点事件效果更好,它可以在后台页面管理:
I found this method works better than focus event, it can be managed in the background page:
此代码将有助于跟踪 URL 的更改:-
在 side 函数中编写此代码 Inject.js
This code will help to trace the change in URL :-
Write this code Inject.js , in side function
Apple 似乎没有像 Chrome 那样为我们提供太多操作选项卡的 API。
目前,还没有办法检测选项卡事件。
It seems Apple doesn't provide much API for us manipulating tabs like Chrome does.
Currently, there is no way to detect tab event.
与 chrome 不同的是,chrome 为窗口和选项卡更改等事件提供了特殊的 API,您仍然可以使用 safari 扩展来完成此操作。
您只需让注入的 JavaScript 为您想要的事件设置事件侦听器即可。
然后,如果扩展的全局或其他部分需要该信息,您可以使用 postMessage 命令在消息中传递该信息。
Injected.js:
window.addEventListener("load", returned, false);
safari.self.tab.dispatchMessage("发生了什么事","加载");
Unlike chrome which provides a special API for events like window and tab changes, you can still do it with safari extensions.
You simply have to have your injected javascript set up event listeners for the events that you want.
Then if that info is needed by global or other parts of the extension, you can pass the info in messages using the postMessage command.
injected.js:
window.addEventListener("load", loaded, false);
safari.self.tab.dispatchMessage("somethinghappened","load");