Created
March 11, 2021 17:28
-
-
Save hu0p/d74d3583d92bf79713bb854f922c5a2c to your computer and use it in GitHub Desktop.
A utility function to bulk download images on a page
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
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