link script externally

reading-list
aundus 2020-11-26 12:51:08 -05:00
parent 65813e05c4
commit cc7cb81a1b
2 changed files with 2 additions and 60 deletions

View File

@ -62,6 +62,7 @@ Gutenberg metadata much faster than by scraping.
.qrcode { background: transparent url(${os.qrcode_url}) 0 0 no-repeat; }
</style>
<meta name="google" content="notranslate" />
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
</head>
<body>
@ -384,7 +385,7 @@ ${site_top()}
</div>
<script src="./book.js"></script>
<script src="http://cdn.jsdelivr.net/gh/EbookFoundation/reading-lists/ebooks/book.js"></script>
</body>
</html>

View File

@ -1,59 +0,0 @@
(function ($) {
const addbtn = $('#add-to-booklist')
const url = window.location && window.location.toString().split('/')
const bookId = url[url.length - 1].match(/(\d+)/)[0]
const currentList = JSON.parse(localStorage.getItem('booklist'))
if (currentList && currentList[bookId]) {
// book already in list
addbtn.html('-')
addbtn.attr('title', 'Remove book from My Book List')
addbtn.css('background-color', 'indianred')
addbtn.addClass('book-in-list')
}
addbtn.on('click', function (event) {
if (addbtn.hasClass('book-in-list')) {
removeBook()
return
}
// add book to list
let author = ''
$('tbody>tr').each(function (index) {
const row = $(this)
const rowTitle = row.find('th:first').html()
if (rowTitle === 'Author') {
const tableRow = row.find('td:first')
author = tableRow.find('a:first').html()
}
})
const bookData = {
title: $('h1').html(),
bookId: bookId,
imgSrc: $('img.cover-art').attr('src'),
author: author,
url: window.location.href
}
const newList = { ...JSON.parse(localStorage.getItem('booklist')) }
newList[bookId] = bookData
addbtn.addClass('book-in-list')
addbtn.html('-')
addbtn.attr('title', 'Remove book from My Book List')
addbtn.css('background-color', 'indianred')
localStorage.setItem('booklist', JSON.stringify(newList))
console.log(localStorage.getItem('booklist'))
})
function removeBook () {
const newlist = JSON.parse(localStorage.getItem('booklist'))
delete newlist[bookId]
localStorage.setItem('booklist', JSON.stringify(newlist))
console.log(localStorage.getItem('booklist'))
addbtn.removeClass('book-in-list')
addbtn.html('+')
addbtn.attr('title', 'Add book to My Book List')
addbtn.css('background-color', 'cornflowerblue')
}
})(jQuery) // eslint-disable-line