Idioms & Slang Dictionary
Enter a phrase to get its meaning, origin, and examples from our AI.
${data.meaning}
Origin
${data.origin}
`; examplesPane.innerHTML = `- ${data.examples.map(ex => `
- ${ex} `).join('')}
${data.context}
`; analysisContent.style.display = 'block'; setActiveTab(0); }; const setActiveTab = (index) => { if (index < 0 || index >= tabButtons.length) return; currentTabIndex = index; tabButtons.forEach((btn, i) => btn.classList.toggle('active', i === index)); tabPanes.forEach((pane, i) => pane.classList.toggle('hidden', i !== index)); prevBtn.disabled = index === 0; nextBtn.disabled = index === tabButtons.length - 1; }; const showError = (message) => { apiErrorMessage.textContent = message; apiErrorMessage.style.display = 'block'; resultsContainer.style.display = 'none'; }; const hideError = () => { apiErrorMessage.style.display = 'none'; }; // --- PDF Generation --- async function generatePdf() { const { phrase, meaning, origin, examples, context } = lastResult; if (!phrase) return; pdfContentArea.innerHTML = `Idiom & Slang Report: "${phrase}"
Meaning
${meaning}
Origin
${origin}
Example Sentences
- ${examples.map(ex => `
- ${ex} `).join('')}
Context & Usage
${context}
`;
pdfContentArea.style.display = 'block';
pdfContentArea.style.position = 'absolute';
pdfContentArea.style.left = '-9999px';
pdfContentArea.style.width = '800px';
try {
const canvas = await html2canvas(pdfContentArea, { scale: 2 });
const imgData = canvas.toDataURL('image/png');
const { jsPDF } = window.jspdf;
const pdf = new jsPDF({ orientation: 'portrait', unit: 'px', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth();
const imgHeight = canvas.height * pdfWidth / canvas.width;
pdf.addImage(imgData, 'PNG', 0, 0, pdfWidth, imgHeight);
pdf.save(`Report-${phrase.replace(/[^a-z0-9]/gi, '-')}.pdf`);
} catch (error) {
console.error("PDF generation failed:", error);
alert("Could not generate PDF.");
} finally {
pdfContentArea.style.display = 'none';
}
}
// --- Event Listeners ---
getDefinitionBtn.addEventListener('click', getDefinition);
phraseInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') getDefinition();
});
tabButtons.forEach((btn, i) => btn.addEventListener('click', () => setActiveTab(i)));
prevBtn.addEventListener('click', () => setActiveTab(currentTabIndex - 1));
nextBtn.addEventListener('click', () => setActiveTab(currentTabIndex + 1));
downloadPdfBtn.addEventListener('click', generatePdf);
// --- Initial State ---
phraseInput.value = "spill the beans";
});
