3

我在Apple 文档中找不到类似“closeTab”事件的内容。

我试过了:

注入的.js

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

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

4

5 回答 5

3

对于 Safari 5.1 及更高版本,他们添加了一个选项卡关闭事件,您可以按照通常的方式收听和处理该事件。

于 2012-07-03T23:17:36.553 回答
2

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

(你的全局页面必须有一些东西来捕捉这些事件):

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

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

于 2011-02-23T18:15:23.620 回答
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();

    }
}
于 2011-07-16T20:25:51.333 回答
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!!"); }
于 2011-04-15T22:23:10.570 回答
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);

这个例子没什么用,因为它没有提供任何关于关闭的选项卡的信息,并且当一个选项卡被移动时它会产生误报。

于 2011-04-29T19:44:41.717 回答