Marat Mkhitaryan
Marat Mkhitaryan

Reputation: 886

How to download any generated by JS file from firefox addon?

I need to my extension can generate and save text file inside downloads folder. Just give me example of code how to do it.

Upvotes: 0

Views: 645

Answers (1)

Aleksandr
Aleksandr

Reputation: 46

The downloads API is what you are probably looking for: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/downloads

The downloads.download() function lets you download a file from a URL to your Downloads folder. Here is the example based on the Downloads.download() page.

function onStartedDownload(id) {
  console.log('Started downloading: ' + id);
}

function onFailed(error) {
  console.log('Download failed: ' + error);
}

var downloadUrl = "https://www.mozilla.org/media/img/home/2018/cards/irl-season-3.821df676279d.png";

var downloading = browser.downloads.download({
  url : downloadUrl,
  filename : 'mozilla-home.png',
  conflictAction : 'uniquify'
});

downloading.then(onStartedDownload, onFailed);

If you need to download data created in Javascript, then you'll first have to create a URL for that data using URL.createObjectURL()

Upvotes: 3

Related Questions