Last active
September 30, 2023 17:31
-
-
Save ErykDarnowski/c0c6e0222c1c73aa5dd96f5d2682607f to your computer and use it in GitHub Desktop.
Automatically download multiple images from a reddit post
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
/* How to use | |
1. Click on a reddit post with bunch of images. | |
2. Open the dev tools (<kbd>F12</kbd>) go to the `Console` tab. | |
3. Copy & paste in the code bellow and press <kbd>Enter</kbd>. | |
4. Click `Allow` on the multiple file download popup. | |
5. Enjoy! | |
*/ | |
// https://dev.to/sbodi10/download-images-using-javascript-51a9 | |
const downloadImage = async (imgUrl, fileName) => { | |
const image = await fetch(imgUrl); | |
const imageBlog = await image.blob(); | |
const imageURL = URL.createObjectURL(imageBlog); | |
const link = document.createElement('a'); | |
link.href = imageURL; | |
link.download = fileName; | |
document.body.appendChild(link); | |
link.click(); | |
document.body.removeChild(link); | |
}; | |
// 30.09.2023 | |
const nameRegex = /(?<=t\/).*(?=\.)/; | |
const imgEls = $x('/html/body/div[1]/div/div[2]/div[2]/div/div/div/div[2]/div[3]/div[1]/div[3]/div[1]/div/div/div/div[5]/div[1]/div/div[1]/ul/li/figure/a'); | |
for (let i = 0; i < imgEls.length; i++) { | |
const imgUrl = imgEls[i].href; | |
downloadImage(imgUrl, (i + 1) + '_' + nameRegex.exec(imgUrl)[0]); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment