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) {
}
}
}
}
}
一些示例代碼,將是很好的。我需要什麼來解決這個問題?
+1謝謝你很多 – Thariama 2011-03-03 17:13:55
你的'unregister'方法不能正常工作,因爲'this'沒有指向'ObserverTest'對象。否則,發現。 – sdwilsh 2011-03-03 20:26:43
@sdwilsh - 修正。謝謝。 – 2011-03-03 20:46:51