Online AI-Powered Book Summarization Tool

Online AI-Powered Book Summarization Tool

Enter a book title and author to generate a concise summary of its key themes and plot.

The AI is reading, please wait...

'; const prompt = `Act as a literary analyst. Provide a concise summary for the book "${title}" by ${author || 'unknown author'}. The summary should cover the main plot points, key characters, and overarching themes. Structure the response with clear headings for "Plot Overview", "Key Characters", and "Major Themes".`; const payload = { contents: [{ role: "user", parts: [{ text: prompt }] }] }; try { const response = await fetch(API_URL, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload) }); if (!response.ok) throw new Error(`API request failed with status ${response.status}`); const result = await response.json(); if (result.candidates && result.candidates.length > 0) { const summaryText = result.candidates[0].content.parts[0].text; displaySummary(summaryText); lastResult = { title, author, summary: summaryText }; } else { throw new Error("No summary was returned from the AI."); } } catch (error) { console.error("Error during summarization:", error); errorMessage.textContent = `An error occurred: ${error.message}. Please try again.`; summaryOutputDiv.innerHTML = '

Failed to generate summary.

'; } finally { setLoadingState(false); } }; /** * Renders the summary in the UI. */ const displaySummary = (summaryText) => { // Simple markdown-to-HTML conversion const htmlText = summaryText .replace(/### (.*$)/gm, '

$1

') .replace(/\*\* (.*$)/gm, '

$1

') .replace(/^\* (.*$)/gm, '
  • $1
  • ') .replace(/\n/g, '
    '); summaryOutputDiv.innerHTML = htmlText; resultsSection.classList.remove('hidden'); }; /** * Generates and triggers the download of a PDF report. */ const generatePdf = () => { if (!lastResult) return; const { jsPDF } = window.jspdf; const doc = new jsPDF({ unit: 'pt', format: 'a4' }); doc.setFontSize(20); doc.text("AI Book Summary Report", doc.internal.pageSize.getWidth() / 2, 40, { align: 'center' }); doc.setFontSize(14); doc.text(`Title: ${lastResult.title}`, 40, 70); if(lastResult.author) doc.text(`Author: ${lastResult.author}`, 40, 85); doc.setFontSize(11); const summaryLines = doc.splitTextToSize(lastResult.summary, doc.internal.pageSize.getWidth() - 80); doc.text(summaryLines, 40, 110); doc.save(`Summary_of_${lastResult.title.replace(/\s/g, '_')}.pdf`); }; // --- EVENT LISTENERS --- summarizeBtn.addEventListener('click', handleSummarization); downloadPdfBtn.addEventListener('click', generatePdf); });
    Scroll to Top