Is there a file "OnFileDownload" event in Firefox extensions

Hello everyone,

I was just wondering if there was a possible way to catch file downloads. In other words, is it possible from an extension to perform/ support the following scenario:

  1. User clicks on a file download link
  2. The extension catches the request
  3. The extension pauses the download and performs some background checks
  4. Once the checks are complete, the extension resumes the download.

Thanks a lot for your cooperation in advance.

Mostafa

This should do what you want:

To intercept a download before it even becomes a download is more complex. You would need to use http observers and start messing with the request and channel. Pray you don’t need to do that.

2 Likes

Thank you @Lithopsian for the reply. However, it seems to me that you can only use download.jsm to start/control downloads that were triggered from within your extension. I can’t find a listener that listens to all downloads occurring within the browser. Am I missing something?

Thanks a bunch,
Mostafa

I haven’t used the new Downloads.jsm. It is a relatively recent replacement for the old nsIDownloadsManager interface. However, it should show you all current downloads, started from any source. “Old” downloads are not available through this API and have to be queried using PlacesUtils.jsm.

The API is entirely different from before, using views and promises. Views are created against a particular download list (probably ALL in most cases), and define what sort of download events you will be notified about. Adding a view creates a promise to call functions such as onDownloadAdded. Actually changing the state of downloads is probably best done from a singleton, or possibly under user control from individual windows.

1 Like

Thanks a bunch @Lithopsian for the clarification. That was very helpful.