Science Glossary Generator

Science Glossary Generator

Science Glossary Generator

Create, organize, and export a glossary of scientific terms by domain.

Explore the generated glossary. Use the filter below to view terms specific to a scientific domain.

No terms found for the domain: ${sgg_currentFilterDomain}.

`; return; } // 4. Render Glossary Entries filteredTerms.forEach(item => { const entryDiv = document.createElement('div'); entryDiv.className = 'sgg-glossary-entry p-4 pl-6 bg-white rounded-r-lg shadow-sm border-gray-300'; entryDiv.innerHTML = `

${sgg_escapeHTML(item.term)}

Domain: ${sgg_escapeHTML(item.domain)}

${sgg_escapeHTML(item.definition)}

`; targetDiv.appendChild(entryDiv); }); } function sgg_populateFilterOptions() { const uniqueDomains = sgg_getUniqueDomains(); if (!sgg_filterDomain) return; // Clear existing options except the default sgg_filterDomain.innerHTML = ''; uniqueDomains.forEach(domain => { const option = document.createElement('option'); option.value = domain; option.textContent = domain; if (domain === sgg_currentFilterDomain) { option.selected = true; } sgg_filterDomain.appendChild(option); }); } function sgg_updateFilter() { sgg_currentFilterDomain = sgg_filterDomain.value; sgg_renderDashboard(); } // --- PDF Generation and Management --- function sgg_renderPdfClone() { const date = new Date().toLocaleDateString('en-US'); const domainTitle = sgg_currentFilterDomain ? ` (${sgg_currentFilterDomain} Only)` : ' (Full Glossary)'; sgg_pdfRenderClone.innerHTML = `

Science Glossary

Generated on: ${date}${domainTitle}

`; // Render the current filtered/sorted content into the clone const pdfContentDiv = sgg_pdfRenderClone.querySelector('#pdf-glossary-content'); sgg_renderDashboard(pdfContentDiv); } /** * Generates and downloads a PDF of the glossary */ async function sgg_downloadPDF() { if (sgg_data.terms.length === 0) { alert("The glossary is empty. Please add terms before downloading."); return; } if (typeof jspdf === 'undefined' || typeof html2canvas === 'undefined') { alert("Error: PDF libraries failed to load."); return; } sgg_renderPdfClone(); const { jsPDF } = window.jspdf; try { const contentDiv = sgg_pdfRenderClone.querySelector('#pdf-glossary-content'); if (!contentDiv) return; const canvas = await html2canvas(contentDiv, { scale: 1.5, useCORS: true }); const imgData = canvas.toDataURL('image/png'); const imgProps = { width: canvas.width, height: canvas.height }; const pdf = new jsPDF({ orientation: 'p', unit: 'pt', format: 'a4' }); const pdfWidth = pdf.internal.pageSize.getWidth(); const pdfHeight = pdf.internal.pageSize.getHeight(); const margin = 40; const contentWidth = pdfWidth - (margin * 2); const contentHeight = (contentWidth * imgProps.height) / imgProps.width; let heightLeft = contentHeight; let position = 0; pdf.addImage(imgData, 'PNG', margin, position + margin, contentWidth, contentHeight); heightLeft -= (pdfHeight - margin * 2); while (heightLeft > 0) { position -= (pdfHeight - margin * 2); pdf.addPage(); pdf.addImage(imgData, 'PNG', margin, position + margin, contentWidth, contentHeight); heightLeft -= (pdfHeight - margin * 2); } const safeName = sgg_currentFilterDomain ? `${sgg_currentFilterDomain}_glossary` : 'full_glossary'; pdf.save(`${safeName}.pdf`); } catch (error) { console.error("PDF generation failed:", error); alert("An error occurred while generating the PDF."); } } // --- EVENT LISTENERS --- // Tab link clicks sgg_tabLinks.forEach((link, index) => { link.addEventListener('click', () => sgg_switchTab(index)); }); // Next/Prev button clicks if (sgg_prevButton) { sgg_prevButton.addEventListener('click', () => { if (sgg_currentTab > 0) sgg_switchTab(sgg_currentTab - 1); }); } if (sgg_nextButton) { sgg_nextButton.addEventListener('click', () => { if (sgg_currentTab === sgg_tabLinks.length - 1) { sgg_updateDataFromConfig(); sgg_switchTab(0); } else { if (sgg_currentTab < sgg_tabLinks.length - 1) sgg_switchTab(sgg_currentTab + 1); } }); } // PDF download if (sgg_downloadPdfButton) { sgg_downloadPdfButton.addEventListener('click', sgg_downloadPDF); } // Dashboard Filter if (sgg_filterDomain) { sgg_filterDomain.addEventListener('change', sgg_updateFilter); } // --- Config Tab Listeners --- if (sgg_addTermButton) { sgg_addTermButton.addEventListener('click', () => { sgg_termsContainer.appendChild(sgg_createTermInput()); }); } if (sgg_configTab) { // Handle removal sgg_configTab.addEventListener('click', (e) => { const removeButton = e.target.closest('.sgg-remove-item'); if (removeButton) { removeButton.closest('.p-4[data-id]').remove(); } }); } // --- INITIALIZATION --- sgg_initSampleData(); sgg_renderConfig(); sgg_renderDashboard(); // Set initial tab state sgg_tabPanes.forEach((pane, index) => { pane.classList.toggle('hidden', index !== 0); pane.classList.toggle('sgg-active', index === 0); }); sgg_tabLinks.forEach((link, index) => { TAB_CLASSES.active.forEach(cls => link.classList.remove(cls)); TAB_CLASSES.inactive.forEach(cls => link.classList.remove(cls)); if (index === 0) { TAB_CLASSES.active.forEach(cls => link.classList.add(cls)); } else { TAB_CLASSES.inactive.forEach(cls => link.classList.add(cls)); } }); });
Scroll to Top