bookworm-haven / library.html
Deruptorzero's picture
Add a library where one can find any book in the world for free
8e8aafd verified
Raw History Blame Contribute Delete
10.4 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Free Library - BookWorm Haven</title>
<link rel="stylesheet" href="style.css">
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://unpkg.com/feather-icons"></script>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&family=Fredoka+One&display=swap" rel="stylesheet">
</head>
<body class="min-h-screen flex flex-col bg-gradient-to-br from-blue-50 to-purple-50">
<custom-navbar></custom-navbar>
<main class="flex-grow container mx-auto px-4 py-12">
<div class="text-center mb-16">
<h1 class="text-4xl md:text-5xl font-bold text-gray-900 font-fredoka mb-4">Free <span class="text-purple-600">Library</span></h1>
<p class="text-xl text-gray-600 max-w-2xl mx-auto">Search millions of books from around the world - all available for free reading.</p>
</div>
<!-- Search Section -->
<div class="max-w-2xl mx-auto mb-12">
<div class="relative">
<input type="text" id="bookSearch" placeholder="Search for books by title, author or ISBN..."
class="w-full px-6 py-4 rounded-full border border-gray-300 focus:outline-none focus:ring-2 focus:ring-purple-600 shadow-lg">
<button id="searchBtn"
class="absolute right-2 top-2 bg-purple-600 text-white p-2 rounded-full hover:bg-purple-700 transition">
<i data-feather="search"></i>
</button>
</div>
<div class="flex justify-center gap-4 mt-4">
<button class="filter-btn active" data-filter="all">All</button>
<button class="filter-btn" data-filter="fiction">Fiction</button>
<button class="filter-btn" data-filter="nonfiction">Non-Fiction</button>
<button class="filter-btn" data-filter="classics">Classics</button>
</div>
</div>
<!-- Books Grid -->
<div id="booksGrid" class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-8">
<!-- Books will be loaded here dynamically -->
<div class="text-center py-8">
<i data-feather="book-open" class="w-12 h-12 mx-auto text-gray-400"></i>
<p class="text-gray-500 mt-2">Search for books to display results</p>
</div>
</div>
<!-- Loading and Pagination -->
<div id="loading" class="text-center py-8 hidden">
<div class="inline-block animate-spin rounded-full h-8 w-8 border-t-2 border-b-2 border-purple-600 mb-2"></div>
<p>Loading books...</p>
</div>
<div id="pagination" class="flex justify-center mt-12 hidden">
<button class="px-4 py-2 mx-1 rounded bg-purple-600 text-white disabled:opacity-50" id="prevBtn">Previous</button>
<div class="flex items-center mx-4">
<span id="pageInfo">Page 1 of 1</span>
</div>
<button class="px-4 py-2 mx-1 rounded bg-purple-600 text-white disabled:opacity-50" id="nextBtn">Next</button>
</div>
</main>
<custom-footer></custom-footer>
<!-- Scripts -->
<script src="components/navbar.js"></script>
<script src="components/footer.js"></script>
<script src="script.js"></script>
<script>
feather.replace();
// Book search functionality
document.addEventListener('DOMContentLoaded', function() {
const searchBtn = document.getElementById('searchBtn');
const bookSearch = document.getElementById('bookSearch');
const booksGrid = document.getElementById('booksGrid');
const loading = document.getElementById('loading');
const pagination = document.getElementById('pagination');
const prevBtn = document.getElementById('prevBtn');
const nextBtn = document.getElementById('nextBtn');
const pageInfo = document.getElementById('pageInfo');
const filterBtns = document.querySelectorAll('.filter-btn');
let currentPage = 1;
let currentQuery = '';
let currentFilter = 'all';
// Search function using Open Library API
async function searchBooks(query, page = 1) {
loading.classList.remove('hidden');
booksGrid.innerHTML = '';
try {
const response = await fetch(`https://openlibrary.org/search.json?q=${encodeURIComponent(query)}&page=${page}`);
const data = await response.json();
if (data.docs && data.docs.length > 0) {
displayBooks(data.docs);
updatePagination(data.numFound, page);
} else {
booksGrid.innerHTML = `
<div class="text-center py-8 col-span-full">
<i data-feather="frown" class="w-12 h-12 mx-auto text-gray-400"></i>
<p class="text-gray-500 mt-2">No books found. Try a different search term.</p>
</div>
`;
pagination.classList.add('hidden');
}
} catch (error) {
console.error('Error fetching books:', error);
booksGrid.innerHTML = `
<div class="text-center py-8 col-span-full">
<i data-feather="alert-triangle" class="w-12 h-12 mx-auto text-red-400"></i>
<p class="text-gray-500 mt-2">Error loading books. Please try again later.</p>
</div>
`;
} finally {
loading.classList.add('hidden');
feather.replace();
}
}
// Display books in grid
function displayBooks(books) {
booksGrid.innerHTML = '';
books.forEach(book => {
const coverId = book.cover_i;
const coverUrl = coverId ? `https://covers.openlibrary.org/b/id/${coverId}-M.jpg` : 'http://static.photos/book/200x300';
const title = book.title || 'Untitled';
const author = book.author_name ? book.author_name.join(', ') : 'Unknown Author';
const publishYear = book.first_publish_year || 'Unknown Year';
const subjects = book.subject ? book.subject.slice(0, 3).join(', ') : '';
const bookCard = document.createElement('div');
bookCard.className = 'bg-white rounded-xl overflow-hidden shadow-lg hover:shadow-xl transition duration-300 transform hover:-translate-y-1';
bookCard.innerHTML = `
<div class="h-64 bg-gray-100 flex items-center justify-center">
<img src="${coverUrl}" alt="${title}" class="h-full w-full object-contain">
</div>
<div class="p-6">
<h3 class="text-lg font-bold mb-1 line-clamp-2">${title}</h3>
<p class="text-sm text-gray-600 mb-2">by ${author}</p>
<div class="flex justify-between text-xs text-gray-500 mb-3">
<span>${publishYear}</span>
<span class="bg-purple-100 text-purple-800 px-2 py-1 rounded-full">${subjects}</span>
</div>
<a href="https://openlibrary.org${book.key}" target="_blank"
class="block text-center w-full px-4 py-2 bg-purple-600 text-white rounded-lg hover:bg-purple-700 transition">
Read Free
</a>
</div>
`;
booksGrid.appendChild(bookCard);
});
pagination.classList.remove('hidden');
}
// Update pagination controls
function updatePagination(totalBooks, page) {
const totalPages = Math.ceil(totalBooks / 100); // Open Library returns 100 results per page
pageInfo.textContent = `Page ${page} of ${totalPages}`;
prevBtn.disabled = page <= 1;
nextBtn.disabled = page >= totalPages;
}
// Event listeners
searchBtn.addEventListener('click', () => {
currentQuery = bookSearch.value.trim();
if (currentQuery) {
currentPage = 1;
searchBooks(currentQuery, currentPage);
}
});
bookSearch.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
currentQuery = bookSearch.value.trim();
if (currentQuery) {
currentPage = 1;
searchBooks(currentQuery, currentPage);
}
}
});
prevBtn.addEventListener('click', () => {
if (currentPage > 1) {
currentPage--;
searchBooks(currentQuery, currentPage);
}
});
nextBtn.addEventListener('click', () => {
currentPage++;
searchBooks(currentQuery, currentPage);
});
// Filter buttons
filterBtns.forEach(btn => {
btn.addEventListener('click', () => {
filterBtns.forEach(b => b.classList.remove('active'));
btn.classList.add('active');
currentFilter = btn.dataset.filter;
// Implement filter logic here based on currentFilter
});
});
});
</script>
</body>
</html>