Optimizing content...
';
placeholder.classList.remove('hidden');
resultsContainer.classList.add('hidden');
setTimeout(() => {
const title = titleInput.value;
const description = descriptionInput.value;
const keywords = keywordsInput.value.split(',').map(k => k.trim().toLowerCase()).filter(k => k);
const titleAnalysis = analyzeTitle(title, keywords);
const descriptionAnalysis = analyzeDescription(description, keywords);
const suggestions = generateSuggestions(title, description, keywords);
renderResults(titleAnalysis, descriptionAnalysis, suggestions);
}, 1500);
};
const renderResults = (titleData, descData, suggestions) => {
placeholder.classList.add('hidden');
resultsContainer.classList.remove('hidden');
// Title
titleScoreText.textContent = `${titleData.score}/100`;
titleScoreBar.style.width = `${titleData.score}%`;
titleScoreBar.style.backgroundColor = getScoreColor(titleData.score);
titleFeedback.innerHTML = titleData.feedback.map(f => `
${f}`).join('');
// Description
descriptionScoreText.textContent = `${descData.score}/100`;
descriptionScoreBar.style.width = `${descData.score}%`;
descriptionScoreBar.style.backgroundColor = getScoreColor(descData.score);
descriptionFeedback.innerHTML = descData.feedback.map(f => `
${f}`).join('');
// Suggestions
suggestedTitleEl.textContent = suggestions.title;
suggestedDescriptionEl.textContent = suggestions.description;
};
const analyzeTitle = (title, keywords) => {
let score = 0;
const feedback = [];
// Length check (50 points)
if (title.length >= 50 && title.length <= 70) {
score += 50;
feedback.push('✅ Excellent length (50-70 characters).');
} else {
score += 20;
feedback.push('⚠️ Title length should ideally be 50-70 characters for best SEO results.');
}
// Keyword check (50 points)
const primaryKeyword = keywords[0] || '';
if (primaryKeyword && title.toLowerCase().includes(primaryKeyword)) {
score += 50;
feedback.push('✅ Primary keyword is included.');
} else {
score += 10;
feedback.push('❌ Primary keyword is missing from the title.');
}
return { score, feedback };
};
const analyzeDescription = (description, keywords) => {
let score = 0;
const feedback = [];
const wordCount = description.split(/\s+/).length;
// Word count (30 points)
if (wordCount >= 100) {
score += 30;
feedback.push('✅ Good word count (100+ words).');
} else {
feedback.push('⚠️ Consider adding more detail (aim for 100+ words).');
}
// Keyword density (40 points)
let keywordCount = 0;
keywords.forEach(kw => {
const matches = description.toLowerCase().match(new RegExp(kw, 'g')) || [];
keywordCount += matches.length;
});
if (keywordCount >= keywords.length && keywordCount > 0) {
score += 40;
feedback.push('✅ Keywords are well-integrated.');
} else {
score += 10;
feedback.push('❌ Some keywords are missing. Try to include them naturally.');
}
// Call to action (30 points)
const ctas = ['buy now', 'add to cart', 'order today'];
if (ctas.some(cta => description.toLowerCase().includes(cta))) {
score += 30;
feedback.push('✅ Includes a clear call to action.');
} else {
feedback.push('⚠️ Missing a strong call to action (e.g., "Add to Cart").');
}
return { score, feedback };
};
const generateSuggestions = (title, description, keywords) => {
const primaryKeyword = keywords[0] || 'Product';
const capitalizedPK = primaryKeyword.charAt(0).toUpperCase() + primaryKeyword.slice(1);
const suggestedTitle = `${capitalizedPK} | ${title} - Ideal for ${keywords[1] || 'Everyone'}`;
const suggestedDescription = `Discover the finest ${primaryKeyword}. ${description} This high-quality item is perfect for anyone looking for a ${keywords[1] || 'great product'}. As a special ${keywords[2] || 'gift idea'}, it's guaranteed to impress. Order today and experience the difference!`;
return { title: suggestedTitle, description: suggestedDescription };
};
const getScoreColor = (score) => {
if (score >= 80) return '#22c55e'; // Green
if (score >= 50) return '#f59e0b'; // Yellow
return '#ef4444'; // Red
};
const handleDownloadPdf = () => {
const pdfContentEl = document.getElementById('pdf-content');
if (!pdfContentEl) return;
// Populate PDF
document.getElementById('pdf-timestamp').textContent = new Date().toLocaleString('en-US');
document.getElementById('pdf-original-title').textContent = titleInput.value;
document.getElementById('pdf-original-description').textContent = descriptionInput.value;
document.getElementById('pdf-title-score').textContent = titleScoreText.textContent.split('/')[0];
document.getElementById('pdf-desc-score').textContent = descriptionScoreText.textContent.split('/')[0];
document.getElementById('pdf-title-feedback').innerHTML = titleFeedback.outerHTML;
document.getElementById('pdf-desc-feedback').innerHTML = descriptionFeedback.outerHTML;
document.getElementById('pdf-suggested-title').textContent = suggestedTitleEl.textContent;
document.getElementById('pdf-suggested-description').textContent = suggestedDescriptionEl.textContent;
pdfContentEl.classList.remove('hidden');
html2canvas(pdfContentEl, { scale: 2 }).then(canvas => {
pdfContentEl.classList.add('hidden');
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({ orientation: 'portrait', unit: 'pt', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth();
const margin = 40;
const imgWidth = pdfWidth - (margin * 2);
const imgHeight = (canvas.height * imgWidth) / canvas.width;
pdf.addImage(imgData, 'PNG', margin, margin, imgWidth, imgHeight);
pdf.save('product-optimization-report.pdf');
}).catch(err => {
console.error("Error generating PDF:", err);
pdfContentEl.classList.add('hidden');
});
};
// --- Event Listeners ---
analyzeBtn.addEventListener('click', handleAnalysis);
pdfBtn.addEventListener('click', handleDownloadPdf);
});