2011-03-03 81 views
2

我想等待Firefox瀏覽器事件(sessionstore-windows-restored,user-interaction-inactive,..)以清除歷史記錄。我的擴展程序現在可以清除歷史記錄,但我不知道如何監聽這些事件。我已經在observer notification有一個lood,但我沒有試過似乎工作。我如何觀察Firefox擴展中的指定Firefox事件?

我的擴展代碼到目前爲止是在這裏:

(function() { 

    (window.myObj = 
    { 
     myObserver : function() 
     { 
      this.register(); 
     }, 

     init : function() 
     { 
      try { 

       myObserver.prototype = { 
        observe: function(subject, topic, data){ 

         switch (topic) { 
          case 'sessionstore-windows-restored': 
          Firebug.Console.log('sessionstore-windows-restored observed'); 
          myObj.clearHistory(); 
          break; 
          case 'user-interaction-inactive': 
          Firebug.Console.log('user-interaction-inactive'); 
          break; 
          case 'user-interaction-active': 
          Firebug.Console.log('user-interaction-active'); 
          break; 
         } 

         Firebug.Console.log('HUI :' + subject + ','+topic+','+data); 
        }, 
        register: function(){ 
         var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService); 
         observerService.addObserver(this, "readyToClearHistory", false); 
        }, 
        unregister: function(){ 
         var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService); 
         observerService.removeObserver(this, "readyToClearHistory"); 
        } 
       } 
      } 
      catch (e) { 
       Firebug.Console.log('MIST :' + e); 
      } 

      observer = new myObserver(); 
     }, 
     clearHistory : function(){ 
      var globalHistory = Components.classes["@mozilla.org/browser/global-history;2"].getService(Components.interfaces.nsIBrowserHistory); 
      var now = Date.now(); 

      // clear one hour 
      globalHistory.removeVisitsByTimeframe(now - 3600, now); 

      try { 
       var os = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService); 
       os.notifyObservers(null, "browser:purge-session-history", ""); 
      } 
      catch (e) { 

       // Clear last URL of the Open Web Location dialog 
       var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch2); 
       try { 
        prefs.clearUserPref("general.open_location.last_url"); 
       } 
       catch (e) { 
       } 
      } 
       } 
    } 
} 

一些示例代碼,將是很好的。我需要什麼來解決這個問題?

回答

7

我沒有看到代碼中特定於nsIObserver的部分有任何問題,這意味着您的問題可能在其他地方。這個完整的示例工作:

var ObserverTest = { 

    register: function() { 
     var observerService = Components.classes["@mozilla.org/observer-service;1"] 
      .getService(Components.interfaces.nsIObserverService); 
     observerService.addObserver(ObserverTest, "user-interaction-active", false); 
    }, 

    observe: function(subject, topic, data) { 
     switch (topic) { 
      case 'sessionstore-windows-restored': 
       // do stuff 
       break; 
      case 'user-interaction-inactive': 
       // do stuff 
       break; 
      case 'user-interaction-active': 
       // every 5 seconds and immediately when user becomes active 
       alert("active"); 
       break; 
     } 
    }, 

    unregister: function() { 
     var observerService = Components.classes["@mozilla.org/observer-service;1"] 
      .getService(Components.interfaces.nsIObserverService); 
     observerService.removeObserver(ObserverTest, "user-interaction-active"); 
    } 
} 

window.addEventListener("load", ObserverTest.register, false); 
window.addEventListener("unload", ObserverTest.unregister, false); 

一般情況下,我發現它有助於啓動的東西,我第一次嘗試使用一個陌生的成分,像我之前所做的那樣,這樣我就可以隔離開來很簡單從應用程序特定的代碼,並驗證我正確使用它。在我知道它正在工作之前,我不會將它放到更大的應用程序中。

+0

+1謝謝你很多 – Thariama 2011-03-03 17:13:55

+1

你的'unregister'方法不能正常工作,因爲'this'沒有指向'ObserverTest'對象。否則,發現。 – sdwilsh 2011-03-03 20:26:43

+0

@sdwilsh - 修正。謝謝。 – 2011-03-03 20:46:51