Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Legimi.pl - Snippet for adding book to shelf from main catalogue
// This snippet adds 'Add to Shelf' button while viewing legimi.pl catalogue
// How to add and run snippet:
// https://developers.google.com/web/tools/chrome-devtools/javascript/snippets#runcommandmenu
let books = document.querySelectorAll('.book-box');
books.forEach(book => {
let link = book.querySelector("a.book-title");
let url = link.href;
let bookId = url.slice(url.indexOf(',') + 2, url.lastIndexOf('.'));
let btn = document.createElement('button');
btn.setAttribute("onclick", "addToShelf(this)");
let cssText = "background-color:#4e9af1;display:inline-block;padding:0.3em 1.2em;margin:0 0.1em 0.1em 0;border:0.16em solid rgba(255,255,255,0);border-radius:2em;box-sizing: border-box;text-decoration:none;font-family:'Roboto',sans-serif;font-weight:300;color:#FFFFFF;text-shadow: 0 0.04em 0.04em rgba(0,0,0,0.35);text-align:center;transition: all 0.2s;"
btn.style.cssText = cssText;
btn.setAttribute('data-book-id', bookId);
let bookPanel = book.querySelector('.panel');
btn.innerText = "Add To Shelf";
bookPanel.appendChild(btn);
});
function addToShelf(btn) {
fetch("https://www.legimi.pl/api/shopping/shelf/addbook", {
method: "POST",
body: JSON.stringify({
bookId: Number(btn.dataset.bookId)
}),
headers: {
"Content-Type": "application/json",
"X-XSRF-Token": window.userInfoAsync.xsrf,
"authorization": "Basic " + window.logger.apiService.apiKey
},
credentials: "same-origin"
}).then(function(response) {
if (response.status == 401) {
buttons = document.querySelectorAll('button[data-book-id]');
buttons.forEach(button => {
button.innerText = "Please log in!";
})
}
if (response.ok) {
return response.json()
}
}).then(data => {
if (data.success == true) {
btn.innerText = "Added";
btn.animate([{
opacity: 1
}, {
opacity: 0
}], {
duration: 1100,
iterations: 1
}).onfinish = () => btn.remove();
} else
{
btn.innerText = "You already added that book!";
btn.animate([{
opacity: 1
}, {
opacity: 0
}], {
duration: 1100,
iterations: 1
}).onfinish = () => btn.remove();
}
}).catch((error) => {})
}
document.styleSheets[document.styleSheets.length - 1].insertRule('button:hover{border-color:rgba(255,255,255,1)!important}')
@leoossa
Copy link
Author

leoossa commented Jul 25, 2021

screenshot-add-to-gh

@leoossa
Copy link
Author

leoossa commented Jul 25, 2021

screenshot-run-via-devtools-snippets

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment