Your generated citations will appear here.
`;
return;
}
const style = citationStyleSelect.value;
bibliographyOutput.innerHTML = citations.map((data, index) => {
const formatted = formatCitation(data, style);
return `
`;
}).join('');
}
function formatCitation(data, style) {
const { author, title, year, url, site, publisher, journal, volume, pages } = data;
const authorPart = author || "N.p.";
const yearPart = year || "n.d.";
switch(style) {
case 'APA':
if (data.type === 'website') return `${authorPart}. (${yearPart}).
${title}. ${site}. Retrieved from ${url}`;
if (data.type === 'book') return `${authorPart}. (${yearPart}).
${title}. ${publisher}.`;
if (data.type === 'journal') return `${authorPart}. (${yearPart}). ${title}.
${journal}, ${volume}, ${pages}.`;
break;
case 'MLA':
if (data.type === 'website') return `${authorPart}. "${title}."
${site}, ${yearPart}, ${url}.`;
if (data.type === 'book') return `${authorPart}.
${title}. ${publisher}, ${yearPart}.`;
if (data.type === 'journal') return `${authorPart}. "${title}."
${journal}, vol. ${volume}, ${yearPart}, pp. ${pages}.`;
break;
case 'Chicago':
if (data.type === 'website') return `${authorPart}. "${title}." ${site}. Accessed ${new Date().toLocaleDateString()}. ${url}.`;
if (data.type === 'book') return `${authorPart}.
${title}. ${publisher}, ${yearPart}.`;
if (data.type === 'journal') return `${authorPart}. "${title}."
${journal} ${volume} (${yearPart}): ${pages}.`;
break;
}
return "Invalid format.";
}
// --- UTILITY ---
function showNotification(message) {
notification.textContent = message;
notification.classList.remove('opacity-0');
setTimeout(() => notification.classList.add('opacity-0'), 2000);
}
// --- PDF GENERATION ---
async function generatePdfReport() {
if (citations.length === 0) {
showNotification("No citations to export.");
return;
}
downloadPdfBtn.disabled = true;
downloadPdfBtn.textContent = '...';
const style = citationStyleSelect.value;
const listItems = citations.map(c => `
${formatCitation(c, style)}`).join('');
const reportHtml = `
`;
const pdfTemplate = document.getElementById('pdf-template');
pdfTemplate.innerHTML = reportHtml;
pdfTemplate.classList.remove('invisible');
try {
const { jsPDF } = window.jspdf;
const canvas = await html2canvas(pdfTemplate.querySelector('.pdf-report-container'), { scale: 2 });
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({ orientation: 'p', unit: 'pt', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth(), pdfHeight = (canvas.height * pdfWidth) / canvas.width;
pdf.addImage(imgData, 'PNG', 0, 0, pdfWidth, pdfHeight);
pdf.save('Bibliography.pdf');
} catch (e) { console.error('PDF Generation Error:', e); } finally {
downloadPdfBtn.disabled = false;
downloadPdfBtn.textContent = 'Download PDF';
pdfTemplate.classList.add('invisible');
pdfTemplate.innerHTML = '';
}
}
// --- EVENT LISTENERS ---
addCitationBtn.addEventListener('click', addCitation);
citationStyleSelect.addEventListener('change', renderBibliography);
bibliographyOutput.addEventListener('click', (e) => {
if (e.target.classList.contains('delete-btn')) {
citations.splice(e.target.dataset.index, 1);
renderBibliography();
}
});
downloadPdfBtn.addEventListener('click', generatePdfReport);
});