const BIN_ID = "6afdfcf8ffd5d16053cc7490"; const API_KEY = "Y$2a$10$K392DHyQjbKCLJTJFXiwDehUeOUEtdNfd1pQVLBDgPmh/9Db2vD32"; let books = []; async function initApp() { await fetchBooksFromCloud(); renderDashboard(); renderTable(); } async function fetchBooksFromCloud() { try { const response = await fetch(`https://api.jsonbin.io/v3/b/${BIN_ID}/latest`, { headers: { 'X-Master-Key': API_KEY } }); const data = await response.json(); books = data.record || []; } catch (err) { console.error("Failed to load books from cloud:", err); } } async function saveBooksToCloud() { try { await fetch(`https://api.jsonbin.io/v3/b/${BIN_ID}`, { method: 'PUT', headers: { 'Content-Type': 'application/json', 'X-Master-Key': API_KEY }, body: JSON.stringify(books) }); } catch (err) { console.error("Failed to save books to cloud:", err); } } // Updated submit handler that automatically fetches metadata from Hardcover via your Cloudflare Function! async function handleBookSubmit(e) { e.preventDefault(); const idxInput = document.getElementById('bookIndex'); const index = idxInput ? parseInt(idxInput.value) : -1; const title = document.getElementById('inputTitle').value.trim(); const fmt = document.getElementById('inputFormat').value; const isAudio = fmt.toLowerCase().includes('audio'); // Default fallback values in case offline or not found let autoCover = 'https://images.unsplash.com/photo-1544716278-ca5e3f4abd8c?auto=format&fit=crop&w=400&q=80'; let autoGenre = document.getElementById('inputGenre').value.trim() || 'General'; let autoNarrator = '—'; // Automatically fetch metadata from Hardcover via Cloudflare Worker try { const res = await fetch('/search', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ title }) }); if (res.ok) { const meta = await res.json(); if (meta.cover) autoCover = meta.cover; if (meta.genre) autoGenre = meta.genre; if (meta.narrator) autoNarrator = meta.narrator; } } catch (err) { console.warn("Could not fetch automatic metadata from Hardcover, using defaults.", err); } const bookObj = { title: title, author: document.getElementById('inputAuthor').value.trim(), format: fmt, pages: !isAudio && document.getElementById('inputPages').value ? parseInt(document.getElementById('inputPages').value) : '—', time: isAudio && document.getElementById('inputTime').value ? document.getElementById('inputTime').value.trim() : '—', isbn: document.getElementById('inputIsbn').value.trim() || '978000000000', genre: autoGenre, narrator: autoNarrator, // Added narrator field from Hardcover! rating: parseFloat(document.getElementById('inputRating').value), started: parseInt(document.getElementById('inputStarted').value) || 1, ended: parseInt(document.getElementById('inputEnded').value) || 5, cover: autoCover }; if (index >= 0) { books[index] = bookObj; } else { books.push(bookObj); } await saveBooksToCloud(); closeAddModal(); initApp(); } async function deleteBook(index) { if (confirm("Are you sure you want to delete this book?")) { books.splice(index, 1); await saveBooksToCloud(); initApp(); } } window.onload = initApp;