Firefox 扩展 - 用户交互非活动通知仅触发一次
我正在开发一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MDN 文档确认情况确实如此:
然而,相反的事件——
user-interaction-active
——确实持续触发,我们可以利用这一点:我们每次
user-时都会重置计时器。 Interactive-active
触发并注册一个新的setInterval
回调。这意味着回调只会在(至少)10 秒没有任何活动后才会首先执行。根据喜好调整超时值。The MDN docs confirm that this is the case:
However, the reverse event --
user-interaction-active
-- does continuously fire, which we can take advantage of:We reset the timer every time
user-interaction-active
fires and register a newsetInterval
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.