`;
dom.output.innerHTML += descCard;
});
dom.output.classList.remove('hidden');
dom.downloadBtn.classList.remove('hidden');
lucide.createIcons();
// Add event listeners to new copy buttons
dom.output.querySelectorAll('.copy-desc-btn').forEach(btn => {
btn.addEventListener('click', (e) => {
const index = e.currentTarget.dataset.index;
const textToCopy = descriptions[index];
copyText(textToCopy, e.currentTarget);
});
});
}
function copyText(text, button) {
navigator.clipboard.writeText(text).then(() => {
const originalHTML = button.innerHTML;
button.innerHTML = 'Copied!';
button.disabled = true;
setTimeout(() => {
button.innerHTML = originalHTML;
button.disabled = false;
lucide.createIcons();
}, 1500);
}).catch(err => {
console.error('Failed to copy text: ', err);
alert('Could not copy text automatically.');
});
}
function downloadPDF() {
const { jsPDF } = window.jspdf;
const descriptions = Array.from(dom.output.querySelectorAll('.description-text')).map(el => el.textContent);
if (descriptions.length === 0) return;
const pdf = new jsPDF({ orientation: 'p', unit: 'pt', format: 'a4' });
const pageMargin = 40;
const pdfWidth = pdf.internal.pageSize.getWidth();
const contentWidth = pdfWidth - (pageMargin * 2);
pdf.setFont('Inter', 'bold');
pdf.setFontSize(18);
pdf.text('Generated Meta Descriptions', pdfWidth / 2, pageMargin, { align: 'center' });
pdf.setFontSize(11);
let yPos = pageMargin + 40;
descriptions.forEach((desc, index) => {
pdf.setFont('Inter', 'bold');
pdf.text(`Option ${index + 1}:`, pageMargin, yPos);
yPos += 15;
pdf.setFont('Inter', 'normal');
const lines = pdf.splitTextToSize(desc, contentWidth);
pdf.text(lines, pageMargin, yPos);
yPos += (lines.length * 12) + 20; // 12pt font size, plus spacing
});
pdf.save('Meta_Descriptions.pdf');
}
init();
});
