Developing an addon for Firefox I find that I need to be able to launch a download as if the user requested it, that is, either showing the normal file save dialog or saving the file to wherever the user prefers, as it could be configured under preferences > content.
Every single post or documentation regarding downloads seem to only take in consideration the scenario where I know where to download the file, but that is not what I need in this case, in this case it needs to be as if the user started the download.
How can this be accomplished preferably via the methods of the SDK?
Like @canuckistani said, use the Downloads.jsm
Well, you could just initiate an actual save.
Initiating a save link from your code:
In the context menu the oncommand value is
gContextMenu.saveLink();
. saveLink() is defined in:chrome://browser/content/nsContextMenu.js
. It does some housekeeping and then calls saveHelper() which is defined in the same file. You could just call saveHelper() with appropriate arguments. It is included in panels fromchrome://browser/content/web-panels.xul
with:Then the
gContextMenu
variable declared inchrome://browser/content/browser.js
asnull
is assigned:gContextMenu = new nsContextMenu(this, event.shiftKey);
in the onpopupshowing event handler for context menus. It is returned to:
'gContextMenu = null;'
in the
onpopuphiding
event handler.If you want to use it in your own code you can do:
Alternative to using loadSubScript to load
nsContextMenu.js
:My preference is to use loadSubScript to load the saveHelper code from
nsContextMenu.js
. This keeps the code up to date with any changes which are made in future Firefox releases. However, it introduces the dependency that you are using a function from a non-official API. Thus, it might change in some way in future Firefox release and require changes in your add-on. The alternative is to duplicate thesaveHelper()
code in your extension. It is defined as the following: