Reputation: 8640
I've created a tiny Firefox extension with just a small snippet of code. Something like this:
var load = function() {
Components.classes['@mozilla.org/observer-service;1'].getService(Components.interfaces.nsIObserverService).addObserver({
observe: function(subject, topic, data) {
var channel = subject.QueryInterface(Components.interfaces.nsIHttpChannel);
if(/google\.com/.test(channel.originalURI.host)) {
// magic here
}
}
}, 'http-on-modify-request', false);
};
window.addEventListener('load', load, false);
When I submit this plugin to Firefox Addons, I get the following as return:
You're creating an HTTP observer for every opened window, while there should be just a single instance.
Now how do I create a single observer instance?
Upvotes: 1
Views: 258
Reputation: 1362
You should use JS code module. In your overlay script put:
Cu.import("resource://yourPluginName/yourModule.jsm");
and in your yourModule.jsm
and only there put your observer for example:
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
var observerService = Cc["@mozilla.org/observer-service;1"]
.getService(Ci.nsIObserverService);
observerService.addObserver(testObs, "http-on-examine-response", false);
var testObs = {
observe: function(aSubject, aTopic, aData){
//...
}
}
And BTW in jsm, you can't use window object.
Upvotes: 1