I want to wait for Firefox-Browser-Events (sessionstore-windows-restored
, user-interaction-inactive
,..) in order to clear the history. My Extension is able to clear the history on shortcut for now, but i don't know how to listen for such events. I already had a lood at observer notification, but nothing i tried se开发者_Go百科ems to work.
My extension code so far is here:
(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) {
}
}
}
}
}
Some example code would be nice. What do i need to solve this issue?
I don't see anything wrong with with the parts of your code that are specific to nsIObserver
, which means your problem is likely somewhere else. This complete example works:
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);
In general, I find it helpful to start with something very simple the first time I attempt to use an unfamiliar component, like what I've done above, so that I can isolate it from application-specific code and verify that I'm using it correctly. I don't pull it into the larger application until I know it's working.
精彩评论