Javascript Browser.downloads.download !exclusive! -
The file path relative to the browser's default downloads directory. Note that you cannot use absolute paths or navigate "up" (e.g., ../ ) for security reasons.
The delta object tells you exactly what changed, such as the state moving from in_progress to complete , or the fileSize being updated. Browser Compatibility and Best Practices javascript browser.downloads.download
browser.downloads.download({ url: "https://example.com", filename: "my-images/saved-image.png", saveAs: false }).then((downloadId) => { console.log(`Download started with ID: ${downloadId}`); }).catch((error) => { console.error(`Download failed: ${error}`); }); Use code with caution. Key Options Parameters The URL of the resource to download. The file path relative to the browser's default
const myData = { name: "Extension Config", version: 1 }; const blob = new Blob([JSON.stringify(myData, null, 2)], { type: "application/json" }); const url = URL.createObjectURL(blob); browser.downloads.download({ url: url, filename: "config.json" }).then(() => { // Always revoke the URL to free up memory URL.revokeObjectURL(url); }); Use code with caution. Handling Download Progress and Events Browser Compatibility and Best Practices browser
Starting a download is only half the battle. To provide a good user experience, you should monitor the download status using browser.downloads.onChanged . javascript
Managing the download lifecycle (canceling, pausing, or resuming). Prerequisites
browser.downloads.onChanged.addListener((delta) => { if (delta.state && delta.state.current === "complete") { console.log("Download finished!"); } else if (delta.error) { console.error(`Download failed because: ${delta.error.current}`); } }); Use code with caution.