views:

440

answers:

2

How can I set HTTP headers in my firefox extension?

I'll make it so these are only sent while hitting my site so I can detect if the plugin is installed or not and not promote the plugin if it is.

Thanks!

+1  A: 

There are a few existing Firefox extensions that modify HTTP headers en route to the server, and at least one of them, modifyheaders, has open source code.

Or, of course, there's the relevant page in the Mozilla Developer Center, Setting HTTP request headers.

delfuego
Thanks delfuego. I had read that but could not figure out how to get it to work. Finally got a friend to help me out and based on his experience and the docs you suggested we solved it.
luisgo
+1  A: 

Here's the most compact way I found to make this work:

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 ( /mysite/.test( channel.originalURI.host ) ) {
                channel.setRequestHeader("x-mysite-extended", "true", false);
            }
    }
},"http-on-modify-request",false);
luisgo
If you want more info concerning this:http://www.softwareishard.com/blog/firebug/nsitraceablechannel-intercept-http-traffic/
Avindra Goolcharan