Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top