Some introductory content.
First Main Section
Content for the first section.
A Subsection
Details for the subsection.
Another H1 Tag (SEO Issue)
Second Main Section
Content for the second section.
Another Subsection
More details.
A Third Subsection
`;
let appData = {
htmlContent: defaultHtml,
analysisResults: null
};
let charts = {};
// --- DOM REFERENCES ---
const tabsContainer = document.getElementById('tabs-container');
const mainContent = document.getElementById('main-content');
const prevTabBtn = document.getElementById('prev-tab-btn');
const nextTabBtn = document.getElementById('next-tab-btn');
const loadingOverlay = document.getElementById('loading-overlay');
let tabIdentifiers = [];
let activeTabIndex = 0;
// --- LOGIC & CALCULATION FUNCTIONS ---
const analyzeTags = () => {
const parser = new DOMParser();
const doc = parser.parseFromString(appData.htmlContent, 'text/html');
const h1s = Array.from(doc.querySelectorAll('h1')).map(tag => tag.innerText.trim());
const h2s = Array.from(doc.querySelectorAll('h2')).map(tag => tag.innerText.trim());
const h3s = Array.from(doc.querySelectorAll('h3')).map(tag => tag.innerText.trim());
let seoFeedback = [];
if (h1s.length === 0) {
seoFeedback.push({ type: 'error', text: 'No H1 tag found. Each page should have exactly one H1 tag.' });
} else if (h1s.length > 1) {
seoFeedback.push({ type: 'error', text: `Multiple (${h1s.length}) H1 tags found. Best practice is to use only one H1 per page.` });
} else {
seoFeedback.push({ type: 'success', text: 'Exactly one H1 tag found. Great!' });
}
if (h2s.length === 0) {
seoFeedback.push({ type: 'warning', text: 'No H2 tags found. H2 tags help structure your content for readers and search engines.' });
}
appData.analysisResults = {
h1s, h2s, h3s, seoFeedback
};
};
// --- UI & RENDERING FUNCTIONS ---
const renderDashboard = () => {
const dashboardContent = document.getElementById('content-analysis-dashboard');
if (!dashboardContent) return;
if (!appData.analysisResults) analyzeTags();
const { h1s, h2s, h3s, seoFeedback } = appData.analysisResults;
const feedbackHtml = seoFeedback.map(item => {
const colors = item.type === 'error' ? 'bg-red-100 text-red-800 border-red-200' : (item.type === 'warning' ? 'bg-yellow-100 text-yellow-800 border-yellow-200' : 'bg-green-100 text-green-800 border-green-200');
const icon = item.type === 'error' ? 'x-circle' : (item.type === 'warning' ? 'alert-triangle' : 'check-circle-2');
return `
${item.text}
`;
}).join('');
const tagListHtml = (tags, title) => {
if(tags.length === 0) return `
No ${title} tags found.
`;
return `
${tags.map(t => `- ${t}
`).join('')}
`;
};
dashboardContent.innerHTML = `
SEO Feedback
${feedbackHtml}
Found Tags
H1 Tags (${h1s.length})
${tagListHtml(h1s, 'H1')}
H2 Tags (${h2s.length})
${tagListHtml(h2s, 'H2')}
H3 Tags (${h3s.length})
${tagListHtml(h3s, 'H3')}
`;
renderTagChart({ h1s, h2s, h3s });
lucide.createIcons();
document.getElementById('download-pdf-btn').addEventListener('click', generatePdf);
};
const renderTagChart = ({ h1s, h2s, h3s }) => {
const ctx = document.getElementById('tag-chart').getContext('2d');
if (charts.tags) charts.tags.destroy();
charts.tags = new Chart(ctx, {
type: 'bar',
data: {
labels: ['H1', 'H2', 'H3'],
datasets: [{
label: 'Tag Count',
data: [h1s.length, h2s.length, h3s.length],
backgroundColor: ['#3b82f6', '#60a5fa', '#93c5fd']
}]
},
options: { responsive: true, maintainAspectRatio: false, plugins: { title: { display: true, text: 'Heading Tag Distribution' }, legend: { display: false } }, scales: { y: { beginAtZero: true, ticks: { stepSize: 1 } } } }
});
};
const renderDataConfig = () => {
const configContent = document.getElementById('content-data-configuration');
if (!configContent) return;
configContent.innerHTML = `
Paste HTML Content
`;
attachConfigListeners();
};
// --- EVENT HANDLERS & LOGIC ---
const attachConfigListeners = () => {
document.getElementById('html-editor').addEventListener('input', (e) => {
appData.htmlContent = e.target.value;
appData.analysisResults = null;
renderDashboard();
});
};
const generatePdf = () => {
loadingOverlay.style.display = 'flex';
const { jsPDF } = window.jspdf;
document.getElementById('pdf-generated-date').textContent = new Date().toLocaleString();
const pdfHeader = document.getElementById('pdf-header');
pdfHeader.classList.remove('hidden');
const fullContent = document.getElementById('pdf-content-area');
html2canvas(fullContent, { scale: 2, useCORS: true, logging: false, windowWidth: 1200 })
.then(canvas => {
pdfHeader.classList.add('hidden');
const imgData = canvas.toDataURL('image/jpeg', 0.9);
const pdf = new jsPDF({ orientation: 'landscape', unit: 'px', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth();
const imgProps = pdf.getImageProperties(imgData);
const imgHeight = (imgProps.height * pdfWidth) / imgProps.width;
pdf.addImage(imgData, 'JPEG', 0, 0, pdfWidth, imgHeight);
pdf.save('Heading-Tag-Analysis.pdf');
loadingOverlay.style.display = 'none';
}).catch(err => {
console.error("PDF generation failed:", err);
pdfHeader.classList.add('hidden');
loadingOverlay.style.display = 'none';
alert('An error occurred generating the PDF.');
});
};
// --- TAB NAVIGATION & INITIALIZATION ---
const switchTab = (tabIndex) => {
activeTabIndex = tabIndex;
document.querySelectorAll('.tab-btn').forEach((btn, i) => btn.classList.toggle('active', i === tabIndex));
document.querySelectorAll('.tab-content').forEach((content, i) => content.classList.toggle('hidden', i !== tabIndex));
updateNavButtons();
};
const updateNavButtons = () => {
prevTabBtn.disabled = activeTabIndex === 0;
nextTabBtn.disabled = activeTabIndex === tabIdentifiers.length - 1;
};
const initializeUI = () => {
const tabs = [
{ name: 'Analysis Dashboard', id: 'analysis-dashboard' },
{ name: 'Data Configuration', id: 'data-configuration' }
];
tabIdentifiers = tabs.map(t => t.id);
tabsContainer.innerHTML = tabs.map(tab => `
`).join('');
mainContent.innerHTML = tabs.map(tab => `
`).join('');
tabs.forEach((tab, index) => {
document.getElementById(`tab-${tab.id}`).addEventListener('click', () => switchTab(index));
});
renderDashboard();
renderDataConfig();
switchTab(0);
};
initializeUI();
prevTabBtn.addEventListener('click', () => { if (activeTabIndex > 0) switchTab(activeTabIndex - 1); });
nextTabBtn.addEventListener('click', () => { if (activeTabIndex < tabIdentifiers.length - 1) switchTab(activeTabIndex + 1); });
});