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:
- Declare the origins you're going to access in the manifest.
- Fetch the resource via XHR (or, indeed, Fetch API).
- Use the response as a
blob:
(you can plug it into a<video src="...">
, for instance). - 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
How to Determine If a Checkbox Is Checked
How to Pass Variable Value Between Different HTML Pages in JavaScript
How to Dynamically Scale Text Size Based on Browser Width
What Are Some Good Ways to Prevent People from Copying My Source Code
Change <Select>'s Option and Trigger Events with JavaScript
Difference Between Element.Value and Element.Getattribute("Value")
Html5 Filereader How to Return Result
JavaScript Variables in HTML Attributes
How to Automatically Allow Blocked Content in Ie
Why Does Jquery Only Affect the First Div Element
Links in <Select> Dropdown Options
Disable Dragging an Image from an HTML Page
How to Create an Asynchronous Function in JavaScript
Vuejs Error: the Client-Side Rendered Virtual Dom Tree Is Not Matching Server-Rendered