Safari 5 Extension: How can I detect when a window

2020-02-29 11:14发布

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

8条回答
叛逆
2楼-- · 2020-02-29 11:41

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;
    }
}
查看更多
Luminary・发光体
3楼-- · 2020-02-29 11:43

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

查看更多
手持菜刀,她持情操
4楼-- · 2020-02-29 11:50

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);
查看更多
smile是对你的礼貌
5楼-- · 2020-02-29 11:51

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

查看更多
戒情不戒烟
6楼-- · 2020-02-29 11:51

It seems Apple doesn't provide much API for us manipulating tabs like Chrome does. Currently, there is no way to detect tab event.

查看更多
老娘就宠你
7楼-- · 2020-02-29 11:59

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);
}
查看更多
登录 后发表回答