Original Text
${result.highlightedText1}
Compared Text
${result.highlightedText2}
`;
};
// --- CORE LOGIC ---
const handleCheck = () => {
checkBtnSpinner.classList.remove('hidden');
checkBtnText.textContent = 'Analyzing...';
checkBtn.disabled = true;
setTimeout(() => {
const text1 = document.getElementById('text-original').value;
const text2 = document.getElementById('text-compare').value;
const isCaseSensitive = document.getElementById('case-sensitive').checked;
const minWordLength = parseInt(document.getElementById('common-word-threshold').value) || 0;
const processText = (text) => {
const processed = isCaseSensitive ? text : text.toLowerCase();
return processed.split(/\s+/).filter(word => word.length >= minWordLength);
};
const words1 = processText(text1);
const words2 = processText(text2);
const set1 = new Set(words1);
const set2 = new Set(words2);
const commonWords = new Set([...set1].filter(word => set2.has(word)));
const score = (2 * commonWords.size) / (set1.size + set2.size) * 100 || 0;
const highlight = (text, commonSet) => {
const regex = new RegExp(`\\b(${Array.from(commonSet).join('|')})\\b`, isCaseSensitive ? 'g' : 'gi');
return text.replace(regex, '
$&');
};
const highlightedText1 = highlight(text1, commonWords);
const highlightedText2 = highlight(text2, commonWords);
renderResults({ score, highlightedText1, highlightedText2 });
downloadPdfBtn.disabled = false;
checkBtnSpinner.classList.add('hidden');
checkBtnText.textContent = 'Check for Plagiarism';
checkBtn.disabled = false;
}, 500);
};
// --- 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 header = `
Plagiarism Analysis Report
`;
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('Plagiarism-Report.pdf');
});
};
// --- EVENT LISTENERS ---
window.switchTab = switchTab;
window.navigateTabs = navigateTabs;
checkBtn.addEventListener('click', handleCheck);
downloadPdfBtn.addEventListener('click', handlePdfDownload);
// --- INITIALIZATION ---
updateNavButtons();
switchTab('dashboard');
});