lundi 6 avril 2015

firefox addon: quit-application observer not working


Vote count:

0




I've been trying to use observers for a while now, but it seems like I cannot get them to work. I put the observe function in different modules of my extension, and also different parts of my code. Registering it from everywhere but it seems like every way that I tried was just a deadened. I can't get it to work. My goal is to listen for when a user exits from Firefox so I can clear preferences objects of my addon. As of now my observer is in bootstrap.js, and this is how I implemented it. I implemented my observe function this way because it was mentioned in this stackoverflow post that this is the right way to observe for quit-application notification. On a side note observers registered gets printed in console log by "oh observing" doesn't.



function myObserver()
{
this.register();
}
myObserver.prototype = {
observe: function(subject, topic, data) {
console.log("oh observing!!");

if (topic == "app-startup" || topic == "profile-after-change") {
var observerService = Components.classes["@http://ift.tt/1efiEYk"]
.getService(Components.interfaces.nsIObserverService);
observerService.addObserver(this, "quit-application", false);
}

else if (topic == "quit-application-requested" || topic == "quit-application")
{
console.log("browser closing");
alert('hello');
myextension.Utils.prefService.clearUserPref("questionType");
myextension.Utils.prefService.clearUserPref("clickThrough");
this.unregister();
}
},

register: function() {

var observerService = Components.classes["@http://ift.tt/1efiEYk"]
.getService(Components.interfaces.nsIObserverService);

observerService.addObserver(this, "quit-application-requested",false);
observerService.addObserver(this, "quit-application",false);
observerService.addObserver(this, "app-startup", false);
observerService.addObserver(this, "profile-after-change", false);
console.log("observers registered");

},

unregister: function() {
var observerService = Components.classes["@http://ift.tt/1efiEYk"]
.getService(Components.interfaces.nsIObserverService);
observerService.removeObserver(this, "quit-application-requested");
console.log("unregistering obs");
}
}


This is my startup function:



function startup(data, reason) {
Components.utils.import("chrome://ext/content/commons.jsm");
let wm = Cc["@http://ift.tt/1ijOkEp"].
getService(Ci.nsIWindowMediator);

let windows = wm.getEnumerator("navigator:browser");
while (windows.hasMoreElements()) {
let domWindow = windows.getNext().QueryInterface(Ci.nsIDOMWindow);

WindowListener.setupBrowserUI(domWindow);

}

// Wait for any new browser windows to open
wm.addListener(WindowListener);

}


and this is my setupBrowserUI function in which I register the observer:



setupBrowserUI: function(domWindow) {
extension.onLoad(domWindow.gBrowser);
observer = new myObserver();
}


asked 27 secs ago







firefox addon: quit-application observer not working

Aucun commentaire:

Enregistrer un commentaire