download-all-linked-files.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const links = document.querySelectorAll('a[download]') | |
if (links.length === 0) { | |
console.log("No downloadable files found") | |
return | |
} | |
const handle = await showSaveFilePicker({ | |
suggestedName: 'files.zip', | |
types: [{ | |
accept: { | |
'application/zip': ['.zip'] | |
} | |
}] | |
}) | |
const writer = await handle.createWritable() | |
const { default: JSZip } = await import('https://esm.sh/jszip') | |
const zip = new JSZip() | |
for (const link of links) { | |
const filename = link.getAttribute('download') // TODO: Content-Disposition response header? | |
if (filename) { | |
const data = fetch(link.getAttribute('href')).then(response => response.arrayBuffer()) | |
zip.file(filename, data) | |
} | |
} | |
zip.generateInternalStream({ type: 'uint8array', streamFiles: true }) | |
.on('data', data => writer.write(data)) | |
.on('error', err => writer.error(err)) | |
.on('end', () => writer.close()) | |
.resume() |