Quantcast
Channel: Recent Gists from hubgit
Viewing all articles
Browse latest Browse all 32

Download all downloadable links in a single zip archive

$
0
0
download-all-linked-files.js
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()

Viewing all articles
Browse latest Browse all 32

Latest Images

Trending Articles



Latest Images