Skip to content

Instantly share code, notes, and snippets.

@dsp1589
Created May 2, 2022 18:46
Show Gist options
  • Save dsp1589/55fbb508df0811c37119f6120f9da599 to your computer and use it in GitHub Desktop.
Save dsp1589/55fbb508df0811c37119f6120f9da599 to your computer and use it in GitHub Desktop.
JS download image from url
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