Handling and aborting downloads from Firefox extension

257 views Asked by At

I need strange behavior for my extension :)

When user enters URL or clicks on link that points to some document I need show him some web page instead of downloading this file (web viewer for pdf, for example). Another words, I want to have binding between content's mimetype and action.

Is there any way to do that from privileged XUL code?

PS. I know that I can write plugin for displaying content in browser, like Adobe Reader plugin, but I prefer writing in JS instead of C++ (and don't wanna cross-compile my code for all platforms where plugin should work).

1

There are 1 answers

3
Neil On BEST ANSWER

You can register a component that implements the nsIRUIContentListener interface with the category manager. The category is external-uricontentlisteners. The entry is the MIME type that you want to register. The value is your component's contract ID.

Alternatively it is possible to register a component directly with the URI listener but this is only useful if your are already loading your component at startup.

When your user clicks on a link to a document served with that MIME type (and there are no installed plugins already handling that type) then your component will be created. One of the isPreferred or canHandleContent methods will be called; you should verify that the content type is the one you want and then return true. Your doContent method will then be called and you can use this to open a window to handle the request. You should return true to indicate that you are not actually providing content for the existing window.

EDIT:

If you want to read the document and output another document in-place, you need to register a stream converter instead. This is done by registering a component that implements the nsIStreamConverter interface hierarchy with the contract ID @mozilla.org/streamconv;1?from=<MIME>&to=<MIME>. I'm not 100% sure but you may have to set the to to be */* and then your GetContentType method should return the actual content type you provide. Your asyncConvertData method will then be passed the destination stream listener. Data will be made available to you via the base nsIStreamListener interface and you can then make the converted data available to the destination stream.