Skip to content

Instantly share code, notes, and snippets.

@moekhalil
Created February 17, 2020 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moekhalil/0266a84a0109fdcf9456bdbbd899ef47 to your computer and use it in GitHub Desktop.
Save moekhalil/0266a84a0109fdcf9456bdbbd899ef47 to your computer and use it in GitHub Desktop.
Craigslist-ImageDownloader.js
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