`;
let tableHtml = `
| Keyword |
Mobile Volume |
Mobile CPC ($) |
Voice Search |
`;
result.keywords.forEach(kw => {
const voiceClass = kw.voiceSearch === 'High' ? 'bg-green-100 text-green-800' : 'bg-yellow-100 text-yellow-800';
tableHtml += `
| ${kw.term} |
${kw.volume.toLocaleString()} |
$${kw.cpc.toFixed(2)} |
${kw.voiceSearch} |
`;
});
tableHtml += `
`;
resultsContainer.innerHTML = tableHtml;
};
// --- CORE LOGIC ---
const handleFind = () => {
findBtnSpinner.classList.remove('hidden');
findBtnText.textContent = 'Searching...';
findBtn.disabled = true;
setTimeout(() => {
const topic = document.getElementById('seed-keyword-input').value.trim().toLowerCase();
if (!topic) {
findBtnSpinner.classList.add('hidden');
findBtnText.textContent = 'Find Mobile Keywords';
findBtn.disabled = false;
return;
}
let foundKeywordsRaw = [];
const association = keywordAssociations.find(a => a.topic.toLowerCase() === topic);
const voiceStarters = ['what', 'how', 'where', 'who', 'when', 'why', 'best', 'find'];
const getVoiceSuitability = (term) => voiceStarters.some(s => term.toLowerCase().startsWith(s)) ? 'High' : 'Medium';
if (association) {
foundKeywordsRaw = association.keywords.split(',').map(k => k.trim());
} else {
foundKeywordsRaw = [`${topic} near me`, `best ${topic}`, `how much does ${topic} cost`, `what are the top ${topic}`];
}
const keywords = foundKeywordsRaw.map(k => ({
term: k,
volume: Math.floor(Math.random() * 5000 + 500),
cpc: Math.random() * 3 + 0.5,
voiceSearch: getVoiceSuitability(k)
}));
const totalVolume = keywords.reduce((sum, kw) => sum + kw.volume, 0);
const totalCpc = keywords.reduce((sum, kw) => sum + kw.cpc, 0);
const result = {
topic: document.getElementById('seed-keyword-input').value.trim(),
keywords: keywords.sort((a,b) => b.volume - a.volume),
avgVolume: totalVolume / keywords.length || 0,
avgCpc: totalCpc / keywords.length || 0,
voiceSearchCount: keywords.filter(k => k.voiceSearch === 'High').length
};
renderDashboard(result);
downloadPdfBtn.disabled = false;
findBtnSpinner.classList.add('hidden');
findBtnText.textContent = 'Find Mobile Keywords';
findBtn.disabled = false;
}, 800);
};
// --- UI & EVENT HANDLERS ---
const switchTab = (tabId) => {
currentTab = tabId;
Object.values(tabPanes).forEach(pane => pane.classList.add('hidden'));
tabPanes[tabId].classList.remove('hidden');
Object.values(tabButtons).forEach(btn => btn.classList.replace('tab-active', 'tab-inactive'));
tabButtons[tabId].classList.replace('tab-inactive', 'tab-active');
updateNavButtons();
};
const navigateTabs = (direction) => {
const currentIndex = tabs.indexOf(currentTab);
const newIndex = direction === 'next' ? currentIndex + 1 : currentIndex - 1;
if (newIndex >= 0 && newIndex < tabs.length) switchTab(tabs[newIndex]);
};
const updateNavButtons = () => {
const currentIndex = tabs.indexOf(currentTab);
prevBtn.disabled = currentIndex === 0;
nextBtn.disabled = currentIndex === tabs.length - 1;
prevBtn.classList.toggle('opacity-50', prevBtn.disabled);
nextBtn.classList.toggle('opacity-50', nextBtn.disabled);
};
const handlePdfDownload = () => {
const pdfRenderContainer = document.getElementById('pdf-render-content');
const pdfContent = document.getElementById('pdf-content').innerHTML;
const topic = document.getElementById('seed-keyword-input').value;
const header = `
Mobile Keyword Report for "${topic}"
`;
pdfRenderContainer.innerHTML = header + pdfContent + '';
html2canvas(pdfRenderContainer, { scale: 2 }).then(canvas => {
const imgData = canvas.toDataURL('image/png');
const { jsPDF } = window.jspdf;
const pdf = new jsPDF({ orientation: 'portrait', unit: 'pt', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth(), margin = 40;
const contentWidth = pdfWidth - margin * 2;
const pdfHeight = (canvas.height * contentWidth) / canvas.width;
pdf.addImage(imgData, 'PNG', margin, margin, contentWidth, pdfHeight);
pdf.save(`Mobile-Keyword-Report-${topic}.pdf`);
});
};
// --- EVENT LISTENERS ---
window.switchTab = switchTab;
window.navigateTabs = navigateTabs;
findBtn.addEventListener('click', handleFind);
downloadPdfBtn.addEventListener('click', handlePdfDownload);
addAssociationBtn.addEventListener('click', () => {
keywordAssociations.push({ id: nextId++, topic: '', keywords: '' });
renderConfig();
});
configContainer.addEventListener('input', e => {
const id = parseInt(e.target.closest('[data-id]').dataset.id);
const prop = e.target.dataset.prop;
const item = keywordAssociations.find(a => a.id === id);
if (item) item[prop] = e.target.value;
});
configContainer.addEventListener('click', e => {
if (!e.target.classList.contains('rm-btn')) return;
const id = parseInt(e.target.closest('[data-id]').dataset.id);
keywordAssociations = keywordAssociations.filter(a => a.id !== id);
renderConfig();
});
// --- INITIALIZATION ---
renderConfig();
updateNavButtons();
switchTab('dashboard');
});