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

发布于 2024-10-10 19:01:09 字数 728 浏览 4 评论 0原文

我在 Apple 文档

我尝试过:

injected.js

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

,但我找不到将消息从注入脚本内发送到全局页面的方法。 消息和代理仅以相反的方式提及。

I cannot find something like "closeTab" event in the Apple Documentation.

I've tried:

injected.js

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

but I can't find a way to send a message from within an injected script to the global page. Messages and Proxies mentions only other way around.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

抚笙 2024-10-17 19:01:09

对于 Safari 5.1 及以上版本,他们添加了 标签关闭您可以以通常的方式监听和处理的事件

For Safari 5.1 onwards, they added a tab close event that you can listen for and handle in the usual way.

君勿笑 2024-10-17 19:01:09

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

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

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

至于如何捕获“选项卡关闭”事件...您的猜测和我的一样好。我现在实际上正在尝试自己寻找答案。

You can dispatch a message from an injected script to a global page like so:
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);

As far as how to capture "tab closed" events... your guess is as good as mine. I am actually trying to find that answer myself currently.

眼中杀气 2024-10-17 19:01:09

例如,在后台使用“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();

    }
}

use the "validate" event in background to detect tab switches, for example

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

    }
}
无力看清 2024-10-17 19:01:09

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

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

You can add listeners directly to the events, but so far I haven't found the correct one to detect when a tab is closed.

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!!"); }
晌融 2024-10-17 19:01:09

到目前为止,我还没有找到一种方法让选项卡告诉全局页面它已经(或即将)关闭。一种糟糕的解决方法是在全局页面上设置一个计时器,该计时器将定期检查是否有任何选项卡已关闭。当活动窗口中的选项卡关闭时,将记录以下简单的代码:

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

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

So far I haven't found a way for a tab to tell the global page that it's been (or is about to be) closed. One crappy workaround is to set up a timer on the global page that will check periodically whether any tabs have been closed. The following simplistic code will log when a tab is closed in the active window:

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

This example is pretty useless, as it doesn't provide any information about the tab that was closed, and it will yield a false positive when a tab is merely moved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文