Created
March 14, 2021 02:17
-
-
Save leoossa/e1d279eee790b9a05c25536bb652e842 to your computer and use it in GitHub Desktop.
Legimi.pl - Snippet for adding book to shelf from main catalogue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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}') |
Author
leoossa
commented
Jul 25, 2021
Do you have an access to legimis api?
@globus1987 not exactly. But I did some reverse engineering here and there and have quite a good knowledge of schema
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment