Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting HTTP headers from a Firefox extension

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.

like image 496
luisgo Avatar asked Nov 19 '09 20:11

luisgo


People also ask

How do I get HTTP headers in Firefox?

Select the Network tab or directly press Ctrl+Shift+E together from your computer keyboard. 3. Reload the page, select any HTTP request, and the HTTP headers will be displayed on the right panel.

How do I see HTML headers in Firefox?

In the Network tab, right-click the element you wish to inspect and select Save All As HAR. In this view, the HTTP headers are also visible in the Headers box on the right-hand side.

Can HTTP headers be modified?

You can manipulate the headers of incoming HTTP requests through HTTP Request Header Modification Rules. Through these rules you can: Set the value of an HTTP request header to a literal string value, overwriting its previous value or adding a new header to the request.


2 Answers

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);
like image 159
luisgo Avatar answered Sep 20 '22 01:09

luisgo


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.

like image 38
delfuego Avatar answered Sep 19 '22 01:09

delfuego