Created
November 4, 2022 14:43
-
-
Save jdarling/82ec295094d38072af3ac8d6eef02bc0 to your computer and use it in GitHub Desktop.
Download all images from a webpage
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 images = Array.from(document.querySelectorAll('img')); | |
const imageFormat = 'png'; | |
const imgToDataUrl = (img)=>{ | |
const canvas = document.createElement('canvas'); | |
const context = canvas.getContext('2d'); | |
canvas.height = img.naturalHeight; | |
canvas.width = img.naturalWidth; | |
context.drawImage(img, 0, 0); | |
const dataURL = canvas.toDataURL(`image/${imageFormat}`); | |
return dataURL; | |
}; | |
const downloadDataUrl = (name, dataUrl)=>{ | |
const link = document.createElement('a'); | |
link.download = name; | |
link.href = dataUrl.replace(`image/${imageFormat}`, `image/octet-stream`); | |
link.click(); | |
}; | |
images.forEach((img, i)=>{ | |
const dataUrl = imgToDataUrl(img); | |
const fn = `image ${i}.${imageFormat}`; | |
downloadDataUrl(fn, dataUrl); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment