Custom download manager integration

Firefox Developer Edition: 66.0b9 (64-bit)
Mac OS: 10.14.3

I would like to add support for a custom download manager. Meaning, whenever I want to download some file in Firefox, I prefer the download to start in this 3rd-party download manager rather than in default Firefox downloader.

More or less the way it was with FlashGot. Basically, the only reason I want to do this is the fact that FlashGot is not supported in the latest versions of Firefox, apparently.

So far I’ve come up with the following extension of mine:

function handle_onCreated(downloadItem)
{
    browser.downloads.cancel(downloadItem.id);
    browser.downloads.erase({ id:downloadItem.id });
    browser.tabs.update({ url:"mydownloadmanager://".concat(downloadItem.url) });
}

browser.downloads.onCreated.addListener(handle_onCreated);

As you can see, I’m using quite dirty tricks here:

  1. Wait for a download to start (listen to onCreated event);
  2. Immediately cancel the download;
  3. Remove it from the downloads list;
  4. Pass the URL to my download manager (mydownloadmanager:// scheme).

That works just fine, but I am wondering if there is a “cleaner” way, for example some event like downloadIsAboutToStart or something similar, so I could grab the download URL before the download starts (before the onCreated event)?

I also find it rather unlikely that I’m the first one to query about this, so perhaps there is already a feature-request in progress?

I found a similar thread, but there the solution/advice was to intercept raw requests and get URL from there, which doesn’t seem to be a good option to me.

You might listen to onBeforeRequest, retrieve the url and cancel the request before downloads.onCreated fires, then call your downloadmanager.

1 Like

That is a good idea, I’ve just tried it and it works - I can get URLs from requests before the actual download starts, but how can I understand that this particular request is for a downwload?

The details object doesn’t have any properties specifying that, or does it? I tried investigating its requestBody object, but it’s always undefined (yes, I provided it in extraInfoSpec).

You could check HTTP headers if it contains Content-Disposition for example.