Video Learning Tool
Watch a video and test your knowledge with an auto-generated quiz.
Enter YouTube Video URL
Video player will appear here
Comprehension Quiz
After loading a video, generate a quiz to test your understanding of the topic.
Quiz Results
Your results will be displayed here after you complete the quiz.
${index + 1}. ${q.questionText}
${q.options.map((option, i) => ``).join('')}
`;
quizForm.appendChild(questionBlock);
});
const submitButton = document.createElement('button');
submitButton.type = 'submit';
submitButton.className = 'action-button w-full';
submitButton.textContent = 'Submit Answers';
quizForm.appendChild(submitButton);
quizContainer.appendChild(quizForm);
quizForm.addEventListener('submit', (e) => {
e.preventDefault();
handleSubmitQuiz(new FormData(quizForm));
});
};
const handleSubmitQuiz = (formData) => {
let allAnswered = true;
for (let i = 0; i < quizData.length; i++) {
if (!formData.has(`question${i}`)) {
allAnswered = false;
break;
}
}
if (!allAnswered) {
showMessage('Please answer all questions before submitting.');
return;
}
clearMessage();
displayResults(formData);
showTab('resultsTab');
};
const displayResults = (formData) => {
resultsContainer.innerHTML = '';
let score = 0;
quizData.forEach((q, index) => {
const userAnswer = parseInt(formData.get(`question${index}`), 10);
const isCorrect = userAnswer === q.correctAnswerIndex;
if (isCorrect) score++;
const resultBlock = document.createElement('div');
resultBlock.className = `p-4 rounded-lg bg-white shadow-sm border-l-4 ${isCorrect ? 'border-green-500' : 'border-red-500'}`;
resultBlock.innerHTML = `${index + 1}. ${q.questionText}
Your answer: ${q.options[userAnswer]} ${isCorrect ? '(Correct)' : '(Incorrect)'}
${!isCorrect ? `Correct answer: ${q.options[q.correctAnswerIndex]}
` : ''}Explanation: ${q.explanation}
`; resultsContainer.appendChild(resultBlock); }); const scoreBlock = document.createElement('div'); scoreBlock.className = 'p-4 rounded-lg bg-blue-100 text-blue-800 text-center text-xl font-bold'; scoreBlock.textContent = `Your Final Score: ${score} out of ${quizData.length}`; resultsContainer.prepend(scoreBlock); pdfDownloadBtn.classList.remove('hidden'); }; const downloadPDF = async () => { const { jsPDF } = window.jspdf; const pdfOutput = document.getElementById('pdf-output'); const canvas = await html2canvas(pdfOutput, { scale: 2, backgroundColor: '#ffffff' }); const imgData = canvas.toDataURL('image/png'); const pdf = new jsPDF({ orientation: 'portrait', unit: 'pt', format: 'a4' }); const pdfWidth = pdf.internal.pageSize.getWidth(); const imgProps = pdf.getImageProperties(imgData); const pdfHeight = (imgProps.height * pdfWidth) / imgProps.width; pdf.addImage(imgData, 'PNG', 40, 40, pdfWidth - 80, pdfHeight - 80); pdf.save('Video-Quiz-Results.pdf'); }; // --- Event Listeners --- loadVideoBtn.addEventListener('click', handleLoadVideo); generateQuizBtn.addEventListener('click', handleQuizGeneration); pdfDownloadBtn.addEventListener('click', downloadPDF); // --- Initial Load --- showTab('videoTab'); });