Reader Magnet Generator
Generating ideas... Please wait.
Generated Reader Magnet Ideas
This tool uses the Gemini API to analyze your book pitch and generate creative ideas based on marketing best practices.
gemini-2.5-flash-preview-09-2025
Structured JSON (Array of Objects)
Format: ${item.Format || 'N/A'}
Hook: ${item.ContentHook || 'No hook provided.'}
`; ideasOutput.appendChild(card); }); resultsContainer.classList.remove('hidden'); downloadPdfBtn.classList.remove('hidden'); } // --- API UTILITY WITH BACKOFF --- async function fetchWithBackoff(url, options, delay = 1000, maxRetries = 5) { for (let i = 0; i < maxRetries; i++) { try { const response = await fetch(url, options); if (response.status === 429 && i < maxRetries - 1) { await new Promise(resolve => setTimeout(resolve, delay)); delay *= 2; continue; } if (!response.ok) { const errorData = await response.json(); throw new Error(`API Error ${response.status}: ${errorData.error?.message || response.statusText}`); } return response.json(); } catch (error) { if (i === maxRetries - 1) throw error; } } } // --- PDF GENERATION --- function generatePDF() { const { jsPDF } = window.jspdf; const container = document.getElementById('rmg-container'); const pdfWrapper = document.getElementById('rmg-pdf-wrapper'); // Prepare for PDF capture container.classList.add('rmg-pdf-export-mode'); html2canvas(pdfWrapper, { scale: 2 }).then(canvas => { // Restore original view container.classList.remove('rmg-pdf-export-mode'); const imgData = canvas.toDataURL('image/png'); const pdf = new jsPDF('p', 'mm', 'a4'); const pdfWidth = pdf.internal.pageSize.getWidth(); const canvasWidth = canvas.width; const canvasHeight = canvas.height; const ratio = canvasWidth / canvasHeight; const imgWidth = pdfWidth - 20; const imgHeight = imgWidth / ratio; pdf.addImage(imgData, 'PNG', 10, 10, imgWidth, imgHeight); const pitch = document.getElementById('rmg-book-pitch').value.substring(0, 30).replace(/[^a-z0-9]/gi, '_').toLowerCase() || 'ideas'; pdf.save(`readermagnet_ideas_${pitch}.pdf`); }).catch(err => { container.classList.remove('rmg-pdf-export-mode'); console.error("PDF generation failed:", err); alert("Could not generate PDF. See console for details."); }); } // --- TAB NAVIGATION --- window.rmg_changeTab = function(tabIndex) { if (tabIndex < 0 || tabIndex >= tabButtons.length) return; tabButtons.forEach((b, i) => { b.classList.toggle('active', i === tabIndex); b.classList.toggle('text-gray-500', i !== tabIndex); }); tabContents.forEach((c, i) => { c.classList.toggle('active', i === tabIndex); c.classList.toggle('hidden', i !== tabIndex); }); currentTab = tabIndex; prevBtn.style.visibility = currentTab === 0 ? 'hidden' : 'visible'; nextBtn.style.visibility = currentTab === tabButtons.length - 1 ? 'hidden' : 'visible'; } // --- ATTACH EVENT LISTENERS --- generateBtn.addEventListener('click', generateIdeas); downloadPdfBtn.addEventListener('click', generatePDF); prevBtn.addEventListener('click', () => rmg_changeTab(currentTab - 1)); nextBtn.addEventListener('click', () => rmg_changeTab(currentTab + 1)); // Initial call to set up tab styling rmg_changeTab(0); }); })();