Ignoring CORS
I purchased some AI generated head shots but of course the website only allows you to download them one by one and you have to open them and close the modality each time each batch generated over 400 images and there is no way I was downloading each one one by one. Below is how I did it.
My good old friend, Javascript.
const originalWindowOpen = window.open;
window.open = function (url) {
console.log("Intercepted URL: ", url);
// Prevent opening of new window and download file
fetch(url)
.then((response) => response.blob())
.then((blob) => {
const url = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.style.display = "none";
a.href = url;
a.download = "your-file-name";
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
});
};
async function automateDownloadSequence() {
const buttons = document.querySelectorAll("button");
const gridItemsSelector = ".absolute.w-full.h-full.left-0.top-0.rounded-md.cursor-pointer.opacity-0.transition-all.duration-100";
const gridItems = document.querySelectorAll(gridItemsSelector);
for (let i = 0; i < gridItems.length; i++) {
gridItems[i].click(); // Open Modal
await new Promise((r) => setTimeout(r, 1000)); // Wait for modal to load
const downloadButtonSelector = ".bg-orange.text-white.border.border-solid.text-lg";
const downloadButton = document.querySelector(downloadButtonSelector);
downloadButton.click(); // Download Image
await new Promise((r) => setTimeout(r, 1000)); // Wait for download to complete
}
}
automateDownloadSequence();
It wouldn’t be fun if it ended there, I started getting some cross origin errors from AWS saying it wasn’t allowed access to the resource. I found a nifty way around that by opening chrome and disabling web security.
open -na Google\ Chrome --args --user-data-dir=/tmp/temporary-chrome-profile-dir -- disable-web-security --disable-site-isolation-trials