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");    
      }
    }
  }
};  
有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top