Skip to content

Instantly share code, notes, and snippets.

@ilovewine
Last active February 29, 2024 15:13
Show Gist options
  • Save ilovewine/29ad11bd72e87cd748d6444e214c297a to your computer and use it in GitHub Desktop.
Save ilovewine/29ad11bd72e87cd748d6444e214c297a to your computer and use it in GitHub Desktop.
Lubimy czytać - import data from your bookshelf to XML. Note that the gist downloads only a selected portion of information. Feel free to adjust the `bookshelfData` array to your needs.
let booksXML = "";
let nextPageButton;
const readBooks = () => {
setTimeout(() => {
const bookshelfData = [
...document.querySelectorAll(".authorAllBooks__single"),
].map((bookDOM) => {
const linkTag = bookDOM.querySelector(".authorAllBooks__singleTextTitle");
const title = linkTag.textContent.trim();
const link = linkTag.href;
const author = bookDOM.querySelector(
".authorAllBooks__singleTextAuthor"
).textContent;
const imageURL = bookDOM.querySelector("img").src;
const rating = bookDOM
.querySelectorAll(".listLibrary__ratingStarsNumber")[1]
.textContent.trim();
return {
title,
link,
author,
imageURL,
rating,
};
});
booksXML += bookshelfData
.map(
(bookData) => `
<book>
<title>${bookData.title}</title>
<link>${bookData.link}</link>
<author>${bookData.author}</author>
<image-url>${bookData.imageURL}</image-url>
<rating>${bookData.rating}</rating>
</book>`
)
.join("\n");
nextPageButton = document.querySelector(".page-item.next-page");
if (!nextPageButton.classList.contains("disabled")) {
nextPageButton.querySelector("a").click();
readBooks();
} else {
const xml = `<?xml version="1.0" encoding="UTF-8"?><bookshelf>${booksXML}</bookshelf>`;
const filename = "data.xml";
const a = document.createElement("a");
const blob = new Blob([xml], { type: "text/plain" });
a.setAttribute("href", window.URL.createObjectURL(blob));
a.setAttribute("download", filename);
a.dataset.downloadurl = ["text/plain", a.download, a.href].join(":");
a.click();
}
}, 1000);
};
readBooks();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment