Firefox 扩展 - 用户交互非活动通知仅触发一次

发布于 2024-12-20 13:50:19 字数 793 浏览 5 评论 0原文

我正在开发一个 Firefox 扩展,当浏览器处于非活动状态(user-interaction-inactive)时需要收到通知。我为此注册了一个观察者,如下面的代码所示。

我的问题是观察者只通知我的分机一次。这意味着,当 Firefox 应用程序启动并且我将其他窗口带到最前面时,它会在短时间内通知我。但是,当我再次将 Firefox 带到最前面,关闭生成的警报(如下所示),然后使用其他应用程序一段时间后,此事件再也不会触发。

任何有关此问题的帮助将不胜感激。

 observer = WWWUP_EventHandlers.WWWUP_Observer;

    observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
    observerService.addObserver(observer, "user-interaction-inactive", false); 

WWWUP_EventHandlers = {

  WWWUP_Observer : {
    observe : function(subject, topic, data){
      if(topic.toLowerCase() == "user-interaction-inactive"){
        alert("User isn't interacting with the browser");    
      }
    }
  }
};  

I am developing a firefox extension which needs to be notified when the browser is inactive (user-interaction-inactive). I registered an observer for this as given in the code below.

My problem is that the observer notifies my extension only once. Meaning, when the Firefox application starts and I bring some other window to the forefront, it will notify me after a short period of time. But when I bring Firefox to the forefront again, close the alert generated (as shown below), then use some other application for some time, this event never fires again.

Any assistance regarding this will be highly appreciated.

 observer = WWWUP_EventHandlers.WWWUP_Observer;

    observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
    observerService.addObserver(observer, "user-interaction-inactive", false); 

WWWUP_EventHandlers = {

  WWWUP_Observer : {
    observe : function(subject, topic, data){
      if(topic.toLowerCase() == "user-interaction-inactive"){
        alert("User isn't interacting with the browser");    
      }
    }
  }
};  

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

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

发布评论

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

评论(1

暮年 2024-12-27 13:50:19

MDN 文档确认情况确实如此:

当 chrome 文档一段时间没有看到用户活动时发送。
连续不活动期间不会重复通知
期间。

然而,相反的事件——user-interaction-active——确实持续触发,我们可以利用这一点:

WWWUP_EventHandlers = {
    timer: undefined,
    WWWUP_Observer : {
        observe: function(subject, topic, data){
            if (topic.toLowerCase() == "user-interaction-active") {
                if (WWWUP_EventHandlers.timer)
                    clearInterval(WWWUP_EventHandlers.timer);
                WWWUP_EventHandlers.timer = setInterval(function() {
                    LOG("User isn't interacting with the browser");    
                }, 10000);
            }
        }
    }
};  

我们每次user-时都会重置计时器。 Interactive-active 触发并注册一个新的 setInterval 回调。这意味着回调只会在(至少)10 秒没有任何活动后才会首先执行。根据喜好调整超时值。

The MDN docs confirm that this is the case:

Sent when the chrome document has seen no user activity for a while.
The notification is not repeated during a continuous inactivity
period.

However, the reverse event -- user-interaction-active -- does continuously fire, which we can take advantage of:

WWWUP_EventHandlers = {
    timer: undefined,
    WWWUP_Observer : {
        observe: function(subject, topic, data){
            if (topic.toLowerCase() == "user-interaction-active") {
                if (WWWUP_EventHandlers.timer)
                    clearInterval(WWWUP_EventHandlers.timer);
                WWWUP_EventHandlers.timer = setInterval(function() {
                    LOG("User isn't interacting with the browser");    
                }, 10000);
            }
        }
    }
};  

We reset the timer every time user-interaction-active fires and register a new setInterval callback. This means that the callback will only first execute after (at least) 10 seconds have passed without any activity. Adjust the timeout value to taste.

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