Sunil Kumar
Sunil Kumar

Reputation: 632

Add download to Firefox via Firefox extension

This is my first Firefox extension which I'm developing with addon-builder [builder.addons.mozilla.org/] .

My question is simple but after trying many things, for many days, I'm unable to get results.

I want to know: How to add a file download to Firefox downloader??

I've a url like: http:// example.com/file.zip and a file location like: D:\myFolder. I want to add this download via my firefox extension.

The things which I've searched are:

https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIWebBrowserPersist#saveURI%28%29

https://developer.mozilla.org/en-US/docs/Code_snippets/Downloading_Files

Components.utils.import("resource://gre/modules/PrivateBrowsingUtils.jsm");

const WebBrowserPersist = Components.Constructor("@mozilla.org/embedding/browser/nsWebBrowserPersist;1",
                                                 "nsIWebBrowserPersist");

var persist = WebBrowserPersist();

var targetFile = Services.dirsvc.get("Desk", Ci.nsIFile);
targetFile.append("file.bin");

// Obtain the privacy context of the browser window that the URL
// we are downloading comes from. If, and only if, the URL is not
// related to a window, null should be used instead.
var privacy = PrivateBrowsingUtils.privacyContextFromWindow(urlSourceWindow);

persist.persistFlags = persist.PERSIST_FLAGS_FROM_CACHE
                     | persist.PERSIST_FLAGS_REPLACE_EXISTING_FILES;

persist.saveURI(uriToSave, null, null, null, "", targetFile, privacy);

Can you just gimme a start from where I should get the easiest possible download function.

Upvotes: 1

Views: 493

Answers (1)

jongo45
jongo45

Reputation: 3090

Components.utils.import("resource://gre/modules/Services.jsm");
var {downloads}=Services;
downloads.addDownload(/*parameters*/);  //see documentation for parameters.

Documentation for addDownload: nsIDownloadManager#addDownload()

Documentation and directory for the wide range of services provided by Services.jsm: Services.jsm

Upvotes: 1

Related Questions