Recipe Idea Generator

Recipe Idea Generator

${item.description || 'No description provided.'}

Key Ingredients:

    ${ingredientsList}
`; recipesOutput.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('rig-container'); const pdfWrapper = document.getElementById('rig-pdf-wrapper'); // Prepare for PDF capture container.classList.add('rig-pdf-export-mode'); html2canvas(pdfWrapper, { scale: 2 }).then(canvas => { // Restore original view container.classList.remove('rig-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 cuisine = document.getElementById('rig-cuisine').value.substring(0, 30).replace(/[^a-z0-9]/gi, '_').toLowerCase() || 'recipes'; pdf.save(`${cuisine}_recipe_ideas.pdf`); }).catch(err => { container.classList.remove('rig-pdf-export-mode'); console.error("PDF generation failed:", err); alert("Could not generate PDF. See console for details."); }); } // --- TAB NAVIGATION --- window.rig_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', generateRecipes); downloadPdfBtn.addEventListener('click', generatePDF); prevBtn.addEventListener('click', () => rig_changeTab(currentTab - 1)); nextBtn.addEventListener('click', () => rig_changeTab(currentTab + 1)); // Initial call to set up tab styling rig_changeTab(0); }); })();
Scroll to Top