Making a Chrome Extension Download a File

Making a Chrome Extension download a file

Fast-forward 3 years, and now Google Chrome offers chrome.downloads API (since Chrome 31).

After declaring "downloads" permission in the manifest, one can initiate a download with this call:

chrome.downloads.download({
url: "http://your.url/to/download",
filename: "suggested/filename/with/relative.path" // Optional
});

If you want to generate the file content in the script, you can use Blob and URL APIs, e.g.:

var blob = new Blob(["array of", " parts of ", "text file"], {type: "text/plain"});
var url = URL.createObjectURL(blob);
chrome.downloads.download({
url: url // The object URL can be used as download URL
//...
});

For more options (i.e. Save As dialog, overwriting existing files, etc.), see the documentation.

Chrome APP/Extension download file from URL

See Chrome App documentation on Handling external content. It provides a good overview.

A short version:

  1. Declare the origins you're going to access in the manifest.
  2. Fetch the resource via XHR (or, indeed, Fetch API).
  3. Use the response as a blob: (you can plug it into a <video src="...">, for instance).
  4. Optionally, save the resource locally.

How to download a file to a directory in my chrome extension?

Nevermind, I found the answer: Broswer ID3 Writer by egoroof. This literally does everything I need. What an amazing library!

Download file request chrome extension with headers

As per the docs headers need to be a Array of objects

chrome.downloads.download({
url: 'http://test/api/file/download',
filename: "file_from_web_api.exe",
headers: [
{'ProfileID': '1'}
]
});

you can also try to create a header object first and then add it to the array look here

EDIT: try with header object

chrome.downloads.download({
url: 'http://test/api/file/download',
filename: "file_from_web_api.exe",
headers: new Headers({
'ProfileID': '1'
})
});

How to download a file via a Chrome Content Script?

Write a background page or event page and do it from there using the example in your linked answer. Communicate from/to the content script with chrome messages.

Chrome extension doing a download can't always specify file extension?

Looks like an intended restriction in Chrome's handling of downloads to ensure "safety", which you can contest on https://crbug.com by advocating your use case.

Meanwhile, download the blob yourself and change its type:

chrome.contextMenus.onClicked.addListener(async ({linkUrl: url}) => {
const blob = await (await fetch(url)).blob();
const typedBlob = blob.type === 'application/octet-stream' ? blob :
new Blob([blob], {type: 'application/octet-stream'});
chrome.downloads.download({
url: URL.createObjectURL(typedBlob),
filename: url.substring(url.lastIndexOf('/') + 1) + '.qz',
conflictAction: 'uniquify',
});
});

P.S. Now that you don't need webRequest API you can use "persistent": false in manifest.json (FWIW there's a way though to use both at the same time by putting webRequest into optional_permissions, see the documentation).



Related Topics



Leave a reply



Submit