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

前端 未结 8 1594
青春惊慌失措
青春惊慌失措 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条回答
  •  萌比男神i
    2021-01-03 14:00

    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

提交回复
热议问题