Created
March 3, 2024 17:26
-
-
Save hubgit/4d266232ca82409fc38d8e41f1f6f11b to your computer and use it in GitHub Desktop.
Download all downloadable links in a single zip archive
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment