Created
August 13, 2013 11:39
-
-
Save nmaier/6220299 to your computer and use it in GitHub Desktop.
Test SDK extension for Downloads.jsm
See: http://stackoverflow.com/questions/18195513/firefox-addon-downloads-jsm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {Cc, Ci, Cu} = require("chrome"); | |
const {notify} = require("sdk/notifications"); | |
const {Widget} = require("sdk/widget"); | |
const {Downloads} = Cu.import("resource://gre/modules/Downloads.jsm", {}); | |
const {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); | |
const {Task} = Cu.import("resource://gre/modules/Task.jsm", {}); | |
function download() { | |
Task.spawn(function() { | |
var options = { | |
source: "http://cdn.sstatic.net", | |
target: "/tmp/kaki.html", | |
}; | |
// Firefox pre Aurora-25 did implement an "old" API version, which required | |
// a different set of options. | |
// See: http://mzl.la/1cwWZ2N | |
if (Services.vc.compare(Services.appinfo.version, "25.0a") < 0) { | |
var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile); | |
file.initWithPath(options.target); | |
options = { | |
source: {uri: Services.io.newURI(options.source, null, null)}, | |
target: {file: file}, | |
saver: {type: "copy"}, | |
}; | |
} | |
var d = yield Downloads.createDownload(options); | |
var res = d.whenSucceeded(); | |
// lets start the download ... | |
yield d.start(); | |
// ... and wait for it to finish | |
yield res; | |
notify({ | |
title: "Test-Extension", | |
text: "Download finished", | |
onClick: function (data) { | |
if ('showContainingDirectory' in d) { | |
d.showContainingDirectory(); | |
} | |
} | |
}); | |
}).then(null, function(ex) { | |
Cu.reportError(ex); | |
notify({ | |
title: "Test-Extension", | |
text: "Error: " + ex | |
}); | |
}); | |
} | |
Widget({ | |
id: "test-downloads", | |
label: "Start a test download", | |
contentURL: "https://tn123.org/favicon.ico", | |
onClick: download | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment