Created
May 2, 2022 18:46
-
-
Save dsp1589/55fbb508df0811c37119f6120f9da599 to your computer and use it in GitHub Desktop.
JS download image from url
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
async function downloadImage(imageSrc, fName) { | |
const image = await fetch(imageSrc) | |
const imageBlog = await image.blob() | |
const imageURL = URL.createObjectURL(imageBlog) | |
const link = document.createElement('a') | |
link.href = imageURL | |
link.download = fName | |
document.body.appendChild(link) | |
link.click() | |
document.body.removeChild(link) | |
} | |
var x = 100; | |
var items = Array.from(document.querySelectorAll("a.item-image.b-lazy")); | |
var iter = items.length; | |
for (let index = 0; index < iter && index < 8; index++) { | |
downloadImage(items[index].style.backgroundImage.slice(items[index].style.backgroundImage.indexOf('"') + 1, items[index].style.backgroundImage.lastIndexOf('"')), `${x+index}.webp`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment