`;
}
const recList = document.getElementById('recommendationsList');
recList.innerHTML = reportResults.recommendations.map(rec => `${rec.title}: ${rec.text} `).join('');
};
startTestBtn.addEventListener('click', runTest);
// --- PDF Generation ---
downloadPdfBtn.addEventListener('click', () => {
if (!window.jspdf || !window.jspdf.jsPDF.autoTable || !reportResults.overall) return;
const { jsPDF } = window.jspdf;
const doc = new jsPDF({ orientation: 'p', unit: 'pt', format: 'letter' });
const page = { width: doc.internal.pageSize.getWidth(), margin: 72 };
let y = page.margin;
// Header
doc.setFont('times', 'bold');
doc.setFontSize(22);
doc.text('Performance & Core Web Vitals Report', page.width / 2, y, { align: 'center' });
y += 30;
doc.setFont('times', 'normal');
doc.setFontSize(12);
doc.text(`For: ${document.getElementById('websiteUrl').value}`, page.width / 2, y, { align: 'center' });
y += 15;
doc.text(`Date: ${new Date().toLocaleDateString()}`, page.width / 2, y, { align: 'center' });
y += 50;
// Summary Metrics
doc.setFont('times', 'bold');
doc.setFontSize(16);
doc.text('Overall Performance Score', page.margin, y);
y += 20;
doc.setFontSize(28);
doc.text(`${reportResults.overall}/100`, page.margin, y);
y += 40;
// Vitals Table
const head = [['Metric', 'Value', 'Assessment']];
const body = [];
for (const key in reportResults.vitals) {
const v = reportResults.vitals[key];
let assessment = v.score > 89 ? 'Good' : v.score > 49 ? 'Needs Improvement' : 'Poor';
body.push([v.name, `${v.value}${v.unit}`, assessment]);
}
doc.autoTable({
startY: y, head, body, theme: 'grid',
headStyles: { fillColor: [29, 78, 216] },
didParseCell: function(data) {
if (data.cell.section === 'body' && data.column.index === 2) {
if (data.cell.raw === 'Good') data.cell.styles.textColor = [0, 128, 0];
if (data.cell.raw === 'Poor') data.cell.styles.textColor = [255, 0, 0];
if (data.cell.raw === 'Needs Improvement') data.cell.styles.textColor = [245, 158, 11];
}
}
});
y = doc.autoTable.previous.finalY + 40;
// Recommendations
doc.setFont('times', 'bold');
doc.setFontSize(16);
doc.text('Recommendations', page.margin, y);
y += 20;
doc.setFont('times', 'normal');
doc.setFontSize(11);
reportResults.recommendations.forEach(rec => {
doc.setFont('times', 'bold');
doc.text(rec.title, page.margin, y);
y += 15;
doc.setFont('times', 'normal');
const lines = doc.splitTextToSize(rec.text, page.width - page.margin * 2);
doc.text(lines, page.margin, y);
y += lines.length * 12 + 15;
});
doc.save(`Performance-Report-${document.getElementById('websiteUrl').value.replace(/https?:\/\//, '')}.pdf`);
});
// --- Initial Setup ---
switchTab('input');
});
