在Safari扩展中捕获关闭选项卡事件

时间:2011-01-03 19:14:47

标签: safari safari-extension

我在Apple Documentation中找不到类似“closeTab”的事件。

我试过了:

injected.js

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

但是我找不到从注入的脚本中向全局页面发送消息的方法。 Messages and Proxies仅提及其他方式。

5 个答案:

答案 0 :(得分:3)

对于Safari 5.1以上版本,他们添加了tab close event,您可以通常的方式收听和处理。

答案 1 :(得分:2)

您可以将注入的脚本中的消息发送到全局页面,如下所示: safari.self.tab.dispatchMessage("messageName", messageData);

(您的全局网页必须能够捕获这些事件):

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

至于如何捕捉“标签关闭”事件...你的猜测和我的一样好。我实际上是在试图找到答案。

答案 2 :(得分:1)

在后台使用“验证”事件来检测制表符开关,例如

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();

    }
}

答案 3 :(得分:0)

您可以直接向事件添加侦听器,但到目前为止,我还没有找到正确的选项来检测选项卡何时关闭。

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

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

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

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

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

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

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

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

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

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

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

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

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

答案 4 :(得分: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);

此示例非常无用,因为它不提供有关已关闭的选项卡的任何信息,并且仅在移动选项卡时会产生误报。