获取页面事件,选项卡关闭,失去焦点? [英] Getting the page events, tab closed, lost focus?

查看:112
本文介绍了获取页面事件,选项卡关闭,失去焦点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题在于如何在特定标签上查询此类事件:


  • 用户已使用特定网址加载标签,例如:google.com (我认为最简单的方法是使用chrome.tabs.query,然后你可以通过url获得活动标签)

  • 用户关闭了标签 >

  • 选项卡处于非活动状态,如切换到其他选项卡 目前为止,我已经研究过检查指定URL操作的最简单方法,就是使用内容脚本,它可以侦听事件,然后将消息API的结果发送到后台脚本。 这些事件在特定的选项卡上。有不同的方法可能,但我建议使用 chrome.tabs。* 事件来监听有关所有选项卡的事件,然后手动执行过滤。



    [注意:您需要请求必要的清单中的 权限 ,例如 标签 闲置 主机匹配模式 等。



    < hr>


    用户已使用特定网址加载标签,例如: google.com


    < blockquote>

    为followig事件添加监听器:



    ...然后根据网址进行过滤。例如:

      var urlRegex = /https?:\/\/([^\.]+\。 ?)google.com/; 
    chrome.tabs.onUpdated.addListener(函数(tabId,info,tab){
    if(info.url&&url; urlRegex.test(info.url)){
    / * ID为`tabId`的标签已更新为`google.com`域中的URL
    *。让我们做一些事情...... * /
    ...
    }
    });







    用户已关闭

    为followig事件附加侦听器:



    ...然后根据网址进行过滤。例如:

      var urlRegex = ...; 
    chrome.tabs.onRemoved.addListener(function(tabId,info){
    chrome.tabs.get(tabId,function(tab){
    if(urlRegex.test(tab.url) ){
    / * ID为'tabId`的标签在
    * google.com`域中有一个网页,正在关闭。让我们做一些事情... * /
    ...
    }
    });
    });








    不幸的是,没有 onFocusLost 事件。您可以聆听 chrome.tabs.onActivated 事件并跟踪每个窗口中的活动选项卡。然后,当另一个标签被激活时,如果以前激活的是指向 google.com ,请执行一些操作。 (我不打算详细描述一种机制)。

    对于这种特殊情况,使用内容脚本和 通知您的背景页面 :当标签停用时:

    <$ p $ {b} / *在`content.js` * /
    window.addEventListener(blur,function(){
    chrome.runtime.sendMessage({text:focusLost });
    })

    / *在`background.js` * /
    chrome.runtime.onMessage.addListener(function(msg,sender){$ b $如果(msg.text ===focusLost){
    / * OMG - 用户切换到另一个标签:( * /
    }
    });







    检查用户是否闲置。

    您不能检查用户是否在特定选项卡上处于非活动状态,但总的来说,正如您所提到的,您可以使用 chrome.idle API,注册 chrome.idle.onStateChanged 的监听器 event。

      chrome.idle.onStateChanged.addListener(function(newState){
    if(newState ==活动){
    / *用户回来了。让我们做点什么...... * /
    } else {
    / *用户不在身边。让我们等待... * /
    }
    });


    The question is how to query such events on specific tab:

    • User has loaded the tab with specific URL like: google.com (I think the easiest way to do this is to use chrome.tabs.query and then you can get the active tab with url)
    • User has closed the tab
    • The tab is inactive, like switching to another tab
    • Check if the user is idle or not (There is also chrome api for idle).

    Currently as far I have researched the easiest way to check the specify URL actions is to use content scripts, which can listen for events and then send the results with message API to background script.

    解决方案

    You cannot register a listener for such events on a specific tab. There are different approaches possible, but I suggest using chrome.tabs.* events to listen for events regarding all tabs and then do the filtering manually.

    [Note: You will need to request the necessary permissions in your manifest, e.g. "tabs", "idle", the host match patterns etc.]


    User has loaded the tab with specific URL like: google.com

    Attach listeners for the followig events:

    ...and then filter based on the URL. E.g.:

    var urlRegex = /https?:\/\/([^\.]+\.)?google.com/;
    chrome.tabs.onUpdated.addListener(function(tabId, info, tab) {
        if (info.url && urlRegex.test(info.url)) {
            /* The tab with ID `tabId` has been updated to a URL
             * in the `google.com` domain. Let's do something... */
            ... 
        }
    });
    


    User has closed the tab.

    Attach listeners for the followig events:

    ...and then filter based on the URL. E.g.:

    var urlRegex = ...;
    chrome.tabs.onRemoved.addListener(function(tabId, info) {
        chrome.tabs.get(tabId, function(tab) {
            if (urlRegex.test(tab.url)) {
                /* The tab with ID `tabId`, with a web-page in the
                 * `google.com` domain, is being closed. Let's do something... */
                ... 
            }
        });
    });
    


    The tab is inactive, like switching to another tab.

    Unfortunately, there is no onFocusLost event. You could listen for the chrome.tabs.onActivated event and keep track of the active tab in each window. Then when another tab is activated, do something if the previously activated was pointing to google.com. (I am not going to describe a mechanism in detail).
    For this particular case, it might be simpler to use a content script and notify your background page when the tab is deactivated:

     /* In `content.js` */
     window.addEventListener("blur", function() {
         chrome.runtime.sendMessage({ text: "focusLost" });
     })
    
    /* In `background.js` */
    chrome.runtime.onMessage.addListener(function(msg, sender) {
        if (msg.text === "focusLost") {
            /* OMG - The user switched to another tab :( */
        }
    });
    


    Check if the user is idle or not.

    You cannot check if the user is inactive in regard of a specific tab, but in general. As you mentioned you can use the chrome.idle API, registering a listener for the chrome.idle.onStateChanged event.

    chrome.idle.onStateChanged.addListener(function(newState) {
        if (newState == "active") {
            /* The user came back. Let's do something... */
        } else {
            /* The user is not around. Let's wait... */
        }
    });
    

    这篇关于获取页面事件,选项卡关闭,失去焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆