Capturing closed tab events in Safari extension

I cannot find something like closeTab event in Apple Documentation .

I tried:

injected.js

window.addEventListener("unload", function(){
  // Probably closed.
  // Now I need to tell it to the global page.
}, false);

but I cannot find a way to send a message from the entered script to the global page. Messages and proxies are only mentioned differently.

+3
source share
5 answers

For Safari 5.1, they added an event closing tab , which you can listen to and handle in the usual way.

+3
source

You can send a message with the entered script to the global page as follows: safari.self.tab.dispatchMessage("messageName", messageData);

(Your global page must have something to capture these events):

// register for message callbacks from the injected script
safari.application.addEventListener("message", respondToMessageFunction, false);

... , . .

+2

"validate" ,

var popupBackground = {

    initialised : false,
    activeTab : null,
    numberOfTabs : null,


    _init : function() { 
        var that = this;

        // on browser initialise reset data
        localStorage.clear();

        // this initialises the popup dialogue
        localStorage["popupOpened"] = false;



        // register listeners for application messaging
        safari.application.addEventListener("command", function(event){
            that.handleCommand(event);
        }, false);

        safari.application.addEventListener("validate",function(event){
            that.validateCommand(event);
        }, false);

        safari.application.addEventListener("message", function(event){
            that.handleMessage(event);
        }, false);

    },


    _getActiveTab : function(){
        return safari.application.activeBrowserWindow.activeTab;
    },


    // commands are validated before being excecuted
    validateCommand : function(aEvent) {
        var that = this;

        // all commands should have the identifier specified in Extension Builder
        if (aEvent.command === "togglePopup") {
            // check that there is an active tab    
            if (!aEvent.target.browserWindow.activeTab.url) {
                aEvent.target.disabled = true;
            } else {
                aEvent.target.disabled = false;
            }
        }


        // this is a hack for detecting tab switches, safari does not have a dedicated API like Chrome 
        if(this.activeTab !== null){
            if(this.activeTab !== this._getActiveTab()){
                $.each(safari.application.browserWindows, function(aIndex, aWindow) {
                    $.each(aWindow.tabs, function(aIndex, aTab) {
                        if(typeof aTab.page !== "undefined"){
                            //  message all tabs about the focus switch event
                            if (aTab !== that._getActiveTab()) {
                                aTab.page.dispatchMessage("tabUnfocused");
                                // set the popup status (incase tab closed with open popup)
                                localStorage["popupOpened"] = false;
                            }else{
                                aTab.page.dispatchMessage("tabFocused");
                            }
                        }
                    });
                });
            }
        }
        // set the new active tab
        this.activeTab = this._getActiveTab();

    }
}
+1

, , , .

window.onload = function ( event ) {
   safari.self.tab.dispatchMessage("onLoad","it alive!!"); }

window.onfocus = function ( event ) {
   safari.self.tab.dispatchMessage("onFocus","it alive!!"); }

window.onblur = function ( event ) {
   safari.self.tab.dispatchMessage("onBlur","it alive!!"); }

window.onunload = function ( event ) {
   safari.self.tab.dispatchMessage("onUnload","it alive!!"); }

window.ondrop = function ( event ) {
   safari.self.tab.dispatchMessage("onDrop","it alive!!"); }

window.onpagehide = function ( event ) {
   safari.self.tab.dispatchMessage("onPagehide","it alive!!"); }

window.onpageshow = function ( event ) {
   safari.self.tab.dispatchMessage("onPageshow","it alive!!"); }

window.onbeforeunload = function ( event ) {
   safari.self.tab.dispatchMessage("onBeforeunload","it alive!!"); }

window.onchange = function ( event ) {
   safari.self.tab.dispatchMessage("onChange","it alive!!"); }

window.onemptied = function ( event ) {
   safari.self.tab.dispatchMessage("onEmptied","it alive!!"); }

window.onopen = function ( event ) {
   safari.self.tab.dispatchMessage("onOpen","it alive!!"); }

window.onended = function ( event ) {
   safari.self.tab.dispatchMessage("onEnded","it alive!!"); }

window.onerror = function ( event ) {
   safari.self.tab.dispatchMessage("onError","it alive!!"); }
0

, , ( ) . , , - . , :

var tabs = safari.application.activeBrowserWindow.tabs;
var myTimer = setInterval(function (){
    for (var i = 0; i < tabs.length; i++) {
        if (app.activeBrowserWindow.tabs[i] !== tabs[i]) {
            console.log('A tab was closed.');
        }
    }
    tabs = safari.application.activeBrowserWindow.tabs;
}, 1000);

, .

0

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


All Articles