Last active
April 24, 2025 21:15
-
-
Save mmousawy/7cb95ad4a412ac537e13f0cf7a8ee739 to your computer and use it in GitHub Desktop.
Download all images from a webpage that match on CSS selector
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
| // This script downloads all images one after another from a webpage that match a specific CSS selector. | |
| // Requires images to be downloadable through the browser (CORS policy must allow it). | |
| // You can run this code in the browser console. | |
| const downloadAllImages = async (pattern) => { | |
| const images = document.querySelectorAll(pattern); | |
| const imageUrls = Array.from(images).map(img => img.src); | |
| const imagePromises = imageUrls.map(url => fetch(url).then(res => res.blob())); | |
| const imageBlobs = await Promise.all(imagePromises); | |
| const imageDataUrls = await Promise.all(imageBlobs.map(blob => URL.createObjectURL(blob))); | |
| // Download all images | |
| const a = document.createElement("a"); | |
| imageDataUrls.forEach((dataUrl, index) => { | |
| a.href = dataUrl; | |
| a.download = dataUrl.split('?')[0].split('/').pop(); // Extract filename from URL | |
| a.click(); | |
| URL.revokeObjectURL(dataUrl); | |
| }); | |
| a.remove(); | |
| } | |
| // Example usage: download all images from a specific class | |
| downloadAllImages(".product__media img"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment