Last active
January 5, 2026 16:19
-
-
Save jchiatt/a0e12268073522be8d09f0546e450307 to your computer and use it in GitHub Desktop.
Download all images from a webpage
This file contains hidden or 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 IMAGE_EXTENSIONS = [".jpg", ".png", ".svg", ".webp", ".gif"] | |
| const getFileExtension = url => `.${url.split('?')[0].split('.').pop()}` | |
| const imageUrls = | |
| Array.from( | |
| document.querySelectorAll('img') | |
| ).map(({ src }) => ({src})) | |
| .filter(({ src }) => { | |
| return src && IMAGE_EXTENSIONS.includes(getFileExtension(src)) | |
| }) | |
| .map(({src}, idx) => ({ src, filename: `Image-${idx}${getFileExtension(src)}`})) | |
| function downloadFile(url, name) { | |
| fetch(url).then(res => res.blob()).then(res => { | |
| const link = document.createElement('a'); | |
| link.setAttribute('download', name); | |
| const href = URL.createObjectURL(res); | |
| console.log(href) | |
| link.href = href; | |
| link.setAttribute('target', '_blank') | |
| link.click(); | |
| setTimeout(() => { | |
| URL.revokeObjectURL(href) | |
| }, 0) | |
| }) | |
| } | |
| imageUrls.forEach((img) => { | |
| downloadFile(img.src, filename) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To prevent browser blocking use (instead of code from line 27):
Code need to be pasted into browser console