While working on a Firefox extension I needed to intercept links Firefox is about to open and stop it on certain conditions. While it seems like an easy task it took more time then I thought due to not much information was available online. I saw the question asked multiple times with no definitive answer. Correct recipe turns out to be using a observer and listen for an "http-on-examine-response".

Following script will listen all request and you will have a chance to stop the transmission based on your rules.

var observer = {
observe: function(subject,topic,data){

var httpChannel =
subject.QueryInterface(Components.interfaces.nsIHttpChannel);

var channel = subject.QueryInterface(Components.interfaces.nsIChannel);
var url = channel.URI.spec;
url = url.toString();

&&  contentType.indexOf("html") == -1 ){

window.getBrowser().stop();