Safari 5 Extension: How can I detect when a window's current tab has changed?

前端 未结 8 1547
青春惊慌失措
青春惊慌失措 2021-01-03 13:17

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

8条回答
  •  隐瞒了意图╮
    2021-01-03 14:15

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

提交回复
热议问题