Skip to content

Instantly share code, notes, and snippets.

@finnp
Created December 25, 2023 21:18
Show Gist options
  • Save finnp/f729afe89835e612767b77e0b154a2ac to your computer and use it in GitHub Desktop.
Save finnp/f729afe89835e612767b77e0b154a2ac to your computer and use it in GitHub Desktop.
Code to paste in dev tools to download all books you read this year
async function downloadFile(url, fileName) {
try {
// Fetch the content from the URL
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
// Get the Blob from the response
const blob = await response.blob();
// Create a URL for the Blob
const blobUrl = window.URL.createObjectURL(blob);
// Create an anchor element and set the download attribute
const anchor = document.createElement('a');
anchor.href = blobUrl;
anchor.download = fileName || 'downloadedFile';
// Append the anchor to the body, trigger click, and remove it
document.body.appendChild(anchor);
anchor.click();
document.body.removeChild(anchor);
// Clean up the Blob URL
window.URL.revokeObjectURL(blobUrl);
} catch (error) {
console.error('Download failed:', error);
}
}
$$('.bookCover').forEach((img, index) => downloadFile(img.currentSrc, `book-${index}.jpg`))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment