Created
February 17, 2020 09:49
-
-
Save moekhalil/0266a84a0109fdcf9456bdbbd899ef47 to your computer and use it in GitHub Desktop.
Craigslist-ImageDownloader.js
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
const list = document.createElement("ul"); | |
const filenamePre = document.title.split("-")[0].replace(/ /g, "."); | |
imgList.forEach((i, k) => fetch(i.url).then(x => x.blob()).then(imgBlob => { | |
const url = URL.createObjectURL(imgBlob); | |
const imgNum = k + 1; | |
const item = document.createElement("li"); | |
const ele = document.createElement("a"); | |
ele.innerHTML = `Image ${imgNum} - Download<br />`; | |
ele.href = url; | |
ele.download = `${filenamePre}.craigslist-image-${imgNum}.jpg`; | |
ele.setAttribute("class", "downloadable"); | |
ele.addEventListener("click", () => { | |
console.log(`Image ${imgNum} downloaded. Revoking image url...`); | |
setTimeout(() => { | |
URL.revokeObjectURL(url); | |
list.removeChild(item); | |
}, 1500); | |
}); | |
item.appendChild(ele); | |
list.appendChild(item); | |
})); | |
document.body.append(list); | |
const downloadAllImages = () => { | |
console.log(`imgList size is ${imgList.length}`); | |
if (list.querySelectorAll("a").length === imgList.length) { | |
list.querySelectorAll("a").forEach(a => setTimeout(() => a.click(), 25)); | |
} else { | |
setTimeout(downloadAllImages, 100); | |
} | |
}; | |
setTimeout(downloadAllImages, 250); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment