Question

Is is possible to use a bookmarklet in Firefox to save/open a file directly?

Many bookmarklets open a page on which one can click on a link to download the result. E.g. using a blob link. Is it possible to avoid this extra click and invoke the "save file" / "open file" dialog directly?

Was it helpful?

Solution

To trigger the "Save As" dialog for any resource (blob:, http:, whatever permitted scheme), use the download attribute of an anchor. This is supported since Firefox 20.

Example: A bookmarklet that presents the current page as a download:

javascript:(function() {
    var a = document.createElement('a');
    a.href = location.href;
    a.download = 'filename.html';
    document.body.appendChild(a);
    a.click();
    a.parentNode.removeChild(a);
})();

To trigger the Open dialog, create an <input type="file">, and click() it. For many examples, see Using files from web applications.

OTHER TIPS

Should be possible if the bookmarklet sends you to a page where the web server sends the appropriate headers to force a download. Example:

Content-Disposition: attachment; filename="filename.zip"
Content-Type: application/zip
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top