2011-01-03 9 views
3

Apple Documentationに「closeTab」イベントのようなものが見つかりません。Safari拡張機能で閉じるタブイベントをキャッチ

私が試してみた:

injected.js

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

を、私はグローバル・ページに注入されたスクリプト内からメッセージを送信する方法を見つけることができません。 Messages and Proxiesには他の方法しか言及されていません。

答えて

2

あなたはそうのようなグローバル・ページに注入されたスクリプトからのメッセージをディスパッチすることができます safari.self.tab.dispatchMessage("messageName", messageData);

を(あなたのグローバル・ページには、これらのイベントをキャプチャするために何かを持っている必要があります):限りどのよう

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

"タブクローズド"イベントをキャプチャする...あなたの推測は私のものと同じくらい良いです。私は実際に自分自身の答えを現在見つけようとしています。

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!!"); } 
0

これまでのところ、タブがグローバルページに、それが閉じられている(または閉じようとしている)ことを伝える方法が見つかりませんでした。 1つの厄介な回避策は、すべてのタブが閉じられているかどうか定期的にチェックするグローバルページにタイマーを設定することです。以下の単純なコードでは、タブがアクティブウィンドウで閉じられたときにログに記録します。

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

それは閉じたタブについての情報を提供していません、それは偽を得られますように、この例では、かなり役に立ちませんタブが単に動かされたときは正になります。

1

タブの切り替えを検出するために、バックグラウンドで「検証」イベントを使用し、例のSafari以降5.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

のために、彼らはあなたが聞くと通常の方法で処理することができますtab close eventを追加しました。

関連する問題