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

前端 未结 8 1549
青春惊慌失措
青春惊慌失措 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;
        }
    }
    
    0 讨论(0)
  • 2021-01-03 14:19

    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);
    }
    
    0 讨论(0)
提交回复
热议问题