Skip to content

Instantly share code, notes, and snippets.

@thecatfix
Last active August 7, 2024 10:00
Show Gist options
  • Save thecatfix/dfc4f9f939b42e8339147c04ae0758c0 to your computer and use it in GitHub Desktop.
Save thecatfix/dfc4f9f939b42e8339147c04ae0758c0 to your computer and use it in GitHub Desktop.
// See https://gist.github.com/thecatfix/60ba97638e223ccafe1a141852bafbee
// Select all elements with the class "book_info-title"
var titleElements = document.querySelectorAll('div[id^="book_info-title"]');
// Initialize an array to store the book info
var books = [];
// Loop through each title element to get the text content
titleElements.forEach(function(titleElement) {
var title = titleElement.textContent;
// Find the corresponding author element by navigating to its parent and then the sibling
var authorElement = titleElement.nextElementSibling;
var author = authorElement ? authorElement.textContent : "Author not found";
// Add the book info to the array
books.push({
title: title,
author: author
});
});
// Log the books array to the console
console.log(books);
// Optional: Convert to CSV format
var csvContent = "data:text/csv;charset=utf-8,Title,Author\n";
books.forEach(function(book) {
csvContent += book.title + "," + book.author + "\n";
});
// Optional: Create a downloadable link for the CSV
var encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "books.csv");
document.body.appendChild(link); // Required for FF
// Optional: Trigger the download
link.click();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment