Safari 5 extension: how to determine when the tab of the current window has changed?

I have a Safari 5 extension containing a toolbar. Whenever the current tab changes, this toolbar needs to be updated. I would like to do something like this from my bar script:

safari.self.browserWindow.addEventListener("activeTab", tabChanged, false);

However, this does not work. I also tried several other event names:

  • activeTab
  • activeTabChanged
  • onActiveTab
  • onActiveTabChanged
  • tab
  • tabChanged
  • onTab
  • onTabChanged
  • SelectionChanged
  • onSelectionChanged

Does anyone know how to determine when the active tab changes?

Not that it’s not connected in any way, but it looks like I would do it in Chrome with:

 chrome.tabs.onSelectionChanged.addListener(tabChanged);
+2
source share
8

, . , , api. global.html popover.html

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

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

@imiaou: Apple, , : (.

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

, , Apple . , ( 1,5 , ).

+2

Safari API, Chrome, .

@Galt 99% , JavaScript dispatchMessage .

, , focus, , .

:

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

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

, , safari.application.activeBrowserWindow.activeTab

0

, , , :

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

URL-: 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);
0

, Apple API , Chrome. .

-1

Unlike chrome, which provides a special API for events such as window and tab changes, you can still do this with safari extensions.

You just need your nested javascript to set up event listeners for the events you want.

Then, if this information is needed globally or in other parts of the extension, you can pass the information in the messages using the postMessage command.

injected.js:

window.addEventListener("load", loaded, false); safari.self.tab.dispatchMessage("somethinghappened","load");

-1
source

Source: https://habr.com/ru/post/1756748/


All Articles