move js inline

The js is small enough to fit inline. Although we may decide to move the code to the sitewide js, it's easier to review when it's inline.
master
eric 2021-05-06 14:05:57 -04:00
parent 7714ad9b62
commit 6611704389
1 changed files with 48 additions and 1 deletions

View File

@ -392,8 +392,55 @@ ${site_top()}
url: window.location.href
}
}
window.onload = function() {
const addbtn = document.getElementById('add-to-booklist')
const currentList = JSON.parse(localStorage.getItem('booklist'))
if (currentList && currentList[bookId]) {
// book already in list
addbtn.innerHTML = '-'
addbtn.setAttribute('title', 'Remove book from My Book List')
addbtn.setAttribute('style', 'background-color: indianred;')
addbtn.classList.add('book-in-list')
}
addbtn.onclick = function () {
if(addbtn.classList.contains('book-in-list')){
removeBook()
return
}
try{
const bookData = getBookMetadata();
const newList = { ...JSON.parse(localStorage.getItem('booklist')) }
newList[bookId] = bookData
addbtn.classList.add('book-in-list')
addbtn.innerHTML = '-'
addbtn.setAttribute('title', 'Remove book from My Book List')
addbtn.setAttribute('style', 'background-color: indianred;')
localStorage.setItem('booklist', JSON.stringify(newList))
console.log(localStorage.getItem('booklist'))
}
catch(err){
console.log('Error adding book', err)
}
}
function removeBook () {
const newlist = JSON.parse(localStorage.getItem('booklist'))
delete newlist[bookId]
localStorage.setItem('booklist', JSON.stringify(newlist))
console.log(localStorage.getItem('booklist'))
addbtn.classList.remove('book-in-list')
addbtn.innerHTML = '+'
addbtn.setAttribute('title', 'Add book to My Book List')
addbtn.setAttribute('style', 'background-color: cornflowerblue;')
}
}
</script>
<script src="http://cdn.jsdelivr.net/gh/EbookFoundation/reading-lists/ebooks/book.js"></script>
</body>
</html>