Techno Barje

Catch All Requests to a Specific Mime Type/file Extension in Firefox

Here is a Mozilla Javascript Module which allow to catch absolutely all requests based on their Content-Type Http header (ie Mime type) or their filename.

(under LGPL License)

Normally, it would be simple to catch all firefox request by adding a nsIURIContentListener like this :

Components.classes[";1"].getService(Components.interfaces.nsIURILoader).registerContentListener( ...nsIURIContentListener... );

But for some reason, this listener is bypassed here when the HTTP request contains a Content-Disposition header :(
So I give you there all Mozilla black magic needed to catch really all requests.

Hello world

var contentTypeObserver = {};

// Tell if we must catch requests with this content-type
// requestInfo is an object with 3 attributes : contentType, contentLength and fileName.
contentTypeObserver.getRequestListener = function (requestInfo) {
  // Return a new instance of nsIWebProgressListener
  // (a new instance to avoid conflicts with multiple simultaneous downloads)
  return {
    onStartRequest : function (request, context) {

    onStopRequest : function (request, context, statusCode) {

    onDataAvailable : function (request, context, inputStream, offset, count) {

  // There is an helper function that allow to automatically save this request to a file,
  // you just have to pass destinationFile argument which hold a nsIFile instance :
  return createSaveToFileRequestListener(requestInfo, destinationFile, function () { dump("file : "+destinationFile.spec+" downloaded!
"); }