3

Appleのドキュメントで「closeTab」イベントのようなものが見つかりません。

私はもう試した:

注入された.js

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

しかし、挿入されたスクリプト内からグローバルページにメッセージを送信する方法が見つかりません。メッセージとプロキシは、他の方法についてのみ言及しています。

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

これまでのところ、タブが閉じられた (または閉じられようとしている) ことをグローバル ページに伝える方法を見つけられませんでした。厄介な回避策の 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);

この例は、閉じられたタブに関する情報を提供しないため、まったく役に立たず、タブが単に移動された場合に誤検知が発生します。

于 2011-04-29T19:44:41.717 に答える
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 に答える