Skip to content

Instantly share code, notes, and snippets.

@hu0p
Created March 11, 2021 17:28
Show Gist options
  • Save hu0p/d74d3583d92bf79713bb854f922c5a2c to your computer and use it in GitHub Desktop.
Save hu0p/d74d3583d92bf79713bb854f922c5a2c to your computer and use it in GitHub Desktop.
A utility function to bulk download images on a page
function downloadImages(selector) {
function saveImg(blob, fileName) {
var a = document.createElement("a")
document.body.appendChild(a)
a.style = "display: none"
var url = window.URL.createObjectURL(blob)
a.href = url
a.download = fileName
a.click()
window.URL.revokeObjectURL(url)
}
const srcURLs = [...document.querySelectorAll(selector)].map(node => node.src)
return srcURLs.map(async (src) => {
const res = await fetch(src)
const blob = await res.blob()
saveImg(blob, src)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment