Safari 5 扩展:如何检测窗口的当前选项卡何时发生更改?

发布于 2024-09-15 20:07:47 字数 597 浏览 9 评论 0原文

我有一个包含工具栏的 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 技术交流群。

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

发布评论

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

评论(8

奶茶白久 2024-09-22 20:07:47

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

哥,最终变帅啦 2024-09-22 20:07:47

这就是您正在寻找的活动。我不确定,但我认为这是扩展 api 的新添加。您可以放入 global.htmlpopover.html

safari.application.addEventListener("activate", activateHandler, true);

function activateHandler(event) {
    safari.application.activeBrowserWindow.activeTab.page.dispatchMessage('someId', false);
}

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 the popover.html

safari.application.addEventListener("activate", activateHandler, true);

function activateHandler(event) {
    safari.application.activeBrowserWindow.activeTab.page.dispatchMessage('someId', false);
}
疯了 2024-09-22 20:07:47

我同意 @imiaou 的回答:从苹果的文档来看,似乎没有办法做到这一点:(。

因为我需要检测我的扩展程序的选项卡更改(我从 Chrome 移植过来),我做了以下基于轮询的解决方法,它似乎工作正常(在我的全局页面中):

var prevActiveTab;

setInterval("poorMansOnTabChange()", 1500); //poll every 1.5 sec

function poorMansOnTabChange() {
    var curTab = safari.application.activeBrowserWindow.activeTab;
    if (curTab != prevActiveTab) {
        prevActiveTab= curTab;
        console.log("active tab changed!");
        //do work here
    }
}

我对不断轮询浏览器感到不满意,但在苹果添加对这些选项卡事件的支持之前,我看不到其他解决方法。如果您的扩展程序可以承受相对宽松的选项卡切换事件触发延迟,那么这可能是目前合理的解决方法(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):

var prevActiveTab;

setInterval("poorMansOnTabChange()", 1500); //poll every 1.5 sec

function poorMansOnTabChange() {
    var curTab = safari.application.activeBrowserWindow.activeTab;
    if (curTab != prevActiveTab) {
        prevActiveTab= curTab;
        console.log("active tab changed!");
        //do work here
    }
}

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

尹雨沫 2024-09-22 20:07:47

虽然Safari没有Chrome特定的选项卡相关API,但它确实对这个问题有完美的解决方案。

@Galt 已经完成了 99%,其想法是向注入的 JavaScript 添加事件侦听器,并将该信息 dispatchMessage 发送到您的扩展。

您正在寻找的事件处理程序名为 focus,每次选择选项卡或窗口时都会触发该事件处理程序。

在注入的代码中:

var tabInFocus = function( event )
{
 safari.self.tab.dispatchMessage("tabFocusSwitched","");
}

window.addEventListener("focus", tabInFocus, false);

然后,您可以使用与 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:

var tabInFocus = function( event )
{
 safari.self.tab.dispatchMessage("tabFocusSwitched","");
}

window.addEventListener("focus", tabInFocus, false);

You can then update your extension's UI, with the data relevant to safari.application.activeBrowserWindow.activeTab

↙温凉少女 2024-09-22 20:07:47

我发现这个方法比焦点事件效果更好,它可以在后台页面管理:

safari.application.addEventListener("validate", PopUp.validateCommand, false);

var PopUp = {

    activeTab : null,

    // commands are validated before being excecuted
    validateCommand : function(aEvent) {

        // this is a hack for detecting tab switches, safari does not have a dedicated API like Chrome 
        if(PopUp.activeTab !== null){
            if(PopUp.activeTab !== safari.application.activeBrowserWindow.activeTab){
                $.each(safari.application.browserWindows, function(aIndex, aWindow) {
                    $.each(aWindow.tabs, function(aIndex, aTab) {
                        //  message all tabs about the focus switch event
                        if (aTab !== safari.application.activeBrowserWindow.activeTab && aTab.page) {
                            aTab.page.dispatchMessage("tabUnfocused");
                        }else{
                            aTab.page.dispatchMessage("tabFocused");
                        }
                    });
                });
            }
        }
        // set the new active tab
        PopUp.activeTab = safari.application.activeBrowserWindow.activeTab;
    }
}

I found this method works better than focus event, it can be managed in the background page:

safari.application.addEventListener("validate", PopUp.validateCommand, false);

var PopUp = {

    activeTab : null,

    // commands are validated before being excecuted
    validateCommand : function(aEvent) {

        // this is a hack for detecting tab switches, safari does not have a dedicated API like Chrome 
        if(PopUp.activeTab !== null){
            if(PopUp.activeTab !== safari.application.activeBrowserWindow.activeTab){
                $.each(safari.application.browserWindows, function(aIndex, aWindow) {
                    $.each(aWindow.tabs, function(aIndex, aTab) {
                        //  message all tabs about the focus switch event
                        if (aTab !== safari.application.activeBrowserWindow.activeTab && aTab.page) {
                            aTab.page.dispatchMessage("tabUnfocused");
                        }else{
                            aTab.page.dispatchMessage("tabFocused");
                        }
                    });
                });
            }
        }
        // set the new active tab
        PopUp.activeTab = safari.application.activeBrowserWindow.activeTab;
    }
}
心碎的声音 2024-09-22 20:07:47

此代码将有助于跟踪 URL 的更改:-
在 side 函数中编写此代码 Inject.js


function trackURL() {

    alert("beforeNavigate "+safari.application.activeBrowserWindow.activeTab.url);
        setTimeout(function() {
            alert("afterNavigate "+safari.application.activeBrowserWindow.activeTab.url);

            }, 500);
    }
    safari.application.addEventListener("beforeNavigate", trackURL, true);

This code will help to trace the change in URL :-
Write this code Inject.js , in side function


function trackURL() {

    alert("beforeNavigate "+safari.application.activeBrowserWindow.activeTab.url);
        setTimeout(function() {
            alert("afterNavigate "+safari.application.activeBrowserWindow.activeTab.url);

            }, 500);
    }
    safari.application.addEventListener("beforeNavigate", trackURL, true);
不即不离 2024-09-22 20:07:47

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.

高跟鞋的旋律 2024-09-22 20:07:47

与 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");

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