Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firefox Addon Downloads.jsm

I'm trying to use the Downloads.jsm lib of Firefox (it's new in Firefox 23) in a jetpack addin.

var {Cu} = require("chrome"); //works fine
const {Downloads} = Cu.import("resource://gre/modules/Downloads.jsm"); //works fine

But executing either of these functions has no effect:

download = Downloads.createDownload({source: "http://cdn.sstatic.net", target: "/tmp/kaki.html"}); //download is an object but has no function "start"
Downloads.simpleDownload("http://cdn.sstatic.net","/tmp/kaki.html");

Documentation: https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Downloads.jsm https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Downloads.jsm/Download

Do you have any idea, how to use these functions? I haven't found any examples on the web

like image 552
balping Avatar asked Aug 12 '13 19:08

balping


People also ask

How do I add a download extension to Firefox?

Download the file to your local computer. , click Add-ons and Themes and select Extensions. To add the downloaded add-on to the list of available add-ons, drag and drop the file into the Add-ons window. The add-on is added to the list.

What is the best download manager for Firefox?

Both Turbo Download Manager and Download Manager (S3) look like they have the functionality that you are looking for and are both listed as recommended add-ons on the Firefox add-on website. There are manager a lot of others to choose from as well.

Is there a download manager for Firefox?

Free Download Manager is an external program available for Windows, macOS, Android, and Linux. It's a powerful download manager with a ton of neat features, and even comes with a corresponding add-on for Firefox to improve your browsing experience.


1 Answers

The API functions return a promise, not the actual Download object.

In short, the following should work:

const {Downloads} = Cu.import("resource://gre/modules/Downloads.jsm", {});
var downloadPromise = Downloads.createDownload({source: "http://cdn.sstatic.net", target: "/tmp/kaki.html"})
downloadPromise.then(function success(d) {
  d.start();
});

Read up on promises, and to make dealing with them a lot more fun, also Task.jsm

The API did change quite a bit recently; what is documented is the current Aurora-25 or later API. The "old" API is documented within the source.

A more complete example with Firefox <25 support is available in this gist.

like image 102
nmaier Avatar answered Oct 26 '22 23:10

nmaier