From 7678742a42c9761ce4d17bc546bb983132bef7d0 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 20 Jul 2026 20:27:42 +0000 Subject: [PATCH 1/6] Initial plan From 423a282b8ec553c7135a43eb1b0a28b71157ad7b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 20 Jul 2026 20:33:21 +0000 Subject: [PATCH 2/6] feat: save exam answers to localStorage to persist state on navigation --- static/exam.html | 78 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/static/exam.html b/static/exam.html index 0d3b9d4..63ea0ec 100644 --- a/static/exam.html +++ b/static/exam.html @@ -177,6 +177,24 @@

Exam Complete!

renderExam(); return; } + // Check for a previously saved in-progress exam + const activeKey = localStorage.getItem('activeExamStateKey'); + if (activeKey) { + const savedState = localStorage.getItem(activeKey); + if (savedState) { + try { + const state = JSON.parse(savedState); + if (state.examData && state.attemptId) { + examData = state.examData; + attemptId = state.attemptId; + renderExam(); + return; + } + } catch (e) { + console.error('Failed to parse saved exam state:', e); + } + } + } showError('No exam token provided. Please use the link given to you by your instructor.'); return; } @@ -339,6 +357,7 @@

Exam Complete!

container.appendChild(div); }); + restoreExamAnswers(); showPanel('examPanel'); } @@ -361,6 +380,64 @@

Exam Complete!

document.getElementById('submitWarning').textContent = `${total - answered} question(s) not yet answered. You can still submit.`; } + saveExamState(); + } + + // ============ Exam State Persistence ============ + function getExamStateKey() { + const params = new URLSearchParams(window.location.search); + const token = params.get('token'); + if (token) return `examProgress_token_${token}`; + if (attemptId) return `examProgress_attempt_${attemptId}`; + return null; + } + + function saveExamState() { + const key = getExamStateKey(); + if (!key || !examData) return; + const answers = {}; + examData.questions.forEach(q => { + const selected = document.querySelector(`input[name="q_${q.id}"]:checked`); + if (selected) { + answers[String(q.id)] = parseInt(selected.value, 10); + } + }); + try { + const state = { + examData: examData, + attemptId: attemptId, + answers: answers, + savedAt: Date.now() + }; + localStorage.setItem(key, JSON.stringify(state)); + localStorage.setItem('activeExamStateKey', key); + } catch (e) { + console.error('Failed to save exam state:', e); + } + } + + function restoreExamAnswers() { + const key = getExamStateKey(); + if (!key) return; + try { + const saved = localStorage.getItem(key); + if (!saved) return; + const state = JSON.parse(saved); + if (!state.answers || Object.keys(state.answers).length === 0) return; + Object.entries(state.answers).forEach(([questionId, answerId]) => { + const input = document.querySelector(`input[name="q_${questionId}"][value="${answerId}"]`); + if (input) input.checked = true; + }); + onAnswerChange(); + } catch (e) { + console.error('Failed to restore exam answers:', e); + } + } + + function clearExamState() { + const key = getExamStateKey(); + if (key) localStorage.removeItem(key); + localStorage.removeItem('activeExamStateKey'); } // ============ Submit Exam ============ @@ -408,6 +485,7 @@

Exam Complete!

} const result = await response.json(); + clearExamState(); showResults(result); } catch (e) { alert('Network error: ' + e.message); From 4ab378b5c7fb3b2b86875760ba663f1b66421f51 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 20 Jul 2026 20:34:56 +0000 Subject: [PATCH 3/6] fix: address code review - validate localStorage values, skip redundant save on restore, reorder clearExamState --- static/exam.html | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/static/exam.html b/static/exam.html index 63ea0ec..4a0a2a8 100644 --- a/static/exam.html +++ b/static/exam.html @@ -361,7 +361,7 @@

Exam Complete!

showPanel('examPanel'); } - function onAnswerChange() { + function onAnswerChange(skipSave) { if (!examData) return; const total = examData.questions.length; let answered = 0; @@ -380,7 +380,7 @@

Exam Complete!

document.getElementById('submitWarning').textContent = `${total - answered} question(s) not yet answered. You can still submit.`; } - saveExamState(); + if (!skipSave) saveExamState(); } // ============ Exam State Persistence ============ @@ -399,7 +399,7 @@

Exam Complete!

examData.questions.forEach(q => { const selected = document.querySelector(`input[name="q_${q.id}"]:checked`); if (selected) { - answers[String(q.id)] = parseInt(selected.value, 10); + answers[q.id] = parseInt(selected.value, 10); } }); try { @@ -425,10 +425,13 @@

Exam Complete!

const state = JSON.parse(saved); if (!state.answers || Object.keys(state.answers).length === 0) return; Object.entries(state.answers).forEach(([questionId, answerId]) => { - const input = document.querySelector(`input[name="q_${questionId}"][value="${answerId}"]`); + const qId = parseInt(questionId, 10); + const aId = parseInt(answerId, 10); + if (isNaN(qId) || isNaN(aId)) return; + const input = document.querySelector(`input[name="q_${qId}"][value="${aId}"]`); if (input) input.checked = true; }); - onAnswerChange(); + onAnswerChange(true); } catch (e) { console.error('Failed to restore exam answers:', e); } @@ -485,8 +488,8 @@

Exam Complete!

} const result = await response.json(); - clearExamState(); showResults(result); + clearExamState(); } catch (e) { alert('Network error: ' + e.message); document.getElementById('submitExamBtn').disabled = false; From e96b641332acf717fb960b23cbc8ef6fafdf6432 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 20 Jul 2026 20:37:15 +0000 Subject: [PATCH 4/6] refactor: split exam state storage, add staleness check and data validation --- static/exam.html | 58 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 16 deletions(-) diff --git a/static/exam.html b/static/exam.html index 4a0a2a8..54b3593 100644 --- a/static/exam.html +++ b/static/exam.html @@ -180,13 +180,15 @@

Exam Complete!

// Check for a previously saved in-progress exam const activeKey = localStorage.getItem('activeExamStateKey'); if (activeKey) { - const savedState = localStorage.getItem(activeKey); - if (savedState) { + const metaRaw = localStorage.getItem(`${activeKey}_meta`); + if (metaRaw) { try { - const state = JSON.parse(savedState); - if (state.examData && state.attemptId) { - examData = state.examData; - attemptId = state.attemptId; + const meta = JSON.parse(metaRaw); + if (meta.examData && Array.isArray(meta.examData.questions) && + meta.examData.questions.length > 0 && + Number.isInteger(meta.attemptId) && meta.attemptId > 0) { + examData = meta.examData; + attemptId = meta.attemptId; renderExam(); return; } @@ -357,6 +359,7 @@

Exam Complete!

container.appendChild(div); }); + persistExamData(); restoreExamAnswers(); showPanel('examPanel'); } @@ -392,6 +395,22 @@

Exam Complete!

return null; } + // Stores the full exam data once when the exam starts (not on every answer change) + function persistExamData() { + const key = getExamStateKey(); + if (!key || !examData || !attemptId) return; + try { + localStorage.setItem(`${key}_meta`, JSON.stringify({ + examData: examData, + attemptId: attemptId, + savedAt: Date.now() + })); + localStorage.setItem('activeExamStateKey', key); + } catch (e) { + console.error('Failed to persist exam data:', e); + } + } + function saveExamState() { const key = getExamStateKey(); if (!key || !examData) return; @@ -403,14 +422,10 @@

Exam Complete!

} }); try { - const state = { - examData: examData, - attemptId: attemptId, + localStorage.setItem(`${key}_answers`, JSON.stringify({ answers: answers, savedAt: Date.now() - }; - localStorage.setItem(key, JSON.stringify(state)); - localStorage.setItem('activeExamStateKey', key); + })); } catch (e) { console.error('Failed to save exam state:', e); } @@ -420,9 +435,14 @@

Exam Complete!

const key = getExamStateKey(); if (!key) return; try { - const saved = localStorage.getItem(key); + const saved = localStorage.getItem(`${key}_answers`); if (!saved) return; const state = JSON.parse(saved); + // Expire saved answers older than 7 days + if (state.savedAt && (Date.now() - state.savedAt) > 7 * 24 * 60 * 60 * 1000) { + clearExamState(); + return; + } if (!state.answers || Object.keys(state.answers).length === 0) return; Object.entries(state.answers).forEach(([questionId, answerId]) => { const qId = parseInt(questionId, 10); @@ -439,7 +459,10 @@

Exam Complete!

function clearExamState() { const key = getExamStateKey(); - if (key) localStorage.removeItem(key); + if (key) { + localStorage.removeItem(`${key}_answers`); + localStorage.removeItem(`${key}_meta`); + } localStorage.removeItem('activeExamStateKey'); } @@ -488,8 +511,11 @@

Exam Complete!

} const result = await response.json(); - showResults(result); - clearExamState(); + try { + showResults(result); + } finally { + clearExamState(); + } } catch (e) { alert('Network error: ' + e.message); document.getElementById('submitExamBtn').disabled = false; From d441a1e9af5e90e636e19c4ded3f445191bf7c95 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 20 Jul 2026 20:38:58 +0000 Subject: [PATCH 5/6] fix: validate activeKey pattern, extract expiry constant, simplify submit flow --- static/exam.html | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/static/exam.html b/static/exam.html index 54b3593..2b1e744 100644 --- a/static/exam.html +++ b/static/exam.html @@ -140,6 +140,8 @@

Exam Complete!

let examData = null; let attemptId = null; + const EXAM_STATE_EXPIRY_MS = 7 * 24 * 60 * 60 * 1000; // 7 days + function logout() { localStorage.removeItem('user'); window.location.href = '/index.html'; @@ -179,7 +181,7 @@

Exam Complete!

} // Check for a previously saved in-progress exam const activeKey = localStorage.getItem('activeExamStateKey'); - if (activeKey) { + if (activeKey && /^examProgress_(token_|attempt_)\S+$/.test(activeKey)) { const metaRaw = localStorage.getItem(`${activeKey}_meta`); if (metaRaw) { try { @@ -439,7 +441,7 @@

Exam Complete!

if (!saved) return; const state = JSON.parse(saved); // Expire saved answers older than 7 days - if (state.savedAt && (Date.now() - state.savedAt) > 7 * 24 * 60 * 60 * 1000) { + if (state.savedAt && (Date.now() - state.savedAt) > EXAM_STATE_EXPIRY_MS) { clearExamState(); return; } @@ -511,11 +513,8 @@

Exam Complete!

} const result = await response.json(); - try { - showResults(result); - } finally { - clearExamState(); - } + showResults(result); + clearExamState(); } catch (e) { alert('Network error: ' + e.message); document.getElementById('submitExamBtn').disabled = false; From 2c25136aa2913dd3539983a74ce8465cd2ca1c15 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 20 Jul 2026 20:40:10 +0000 Subject: [PATCH 6/6] fix: tighten key regex, validate savedAt as integer, use ES6 shorthand properties --- static/exam.html | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/static/exam.html b/static/exam.html index 2b1e744..8252274 100644 --- a/static/exam.html +++ b/static/exam.html @@ -181,7 +181,7 @@

Exam Complete!

} // Check for a previously saved in-progress exam const activeKey = localStorage.getItem('activeExamStateKey'); - if (activeKey && /^examProgress_(token_|attempt_)\S+$/.test(activeKey)) { + if (activeKey && /^examProgress_(token_|attempt_)[a-zA-Z0-9_-]+$/.test(activeKey)) { const metaRaw = localStorage.getItem(`${activeKey}_meta`); if (metaRaw) { try { @@ -403,8 +403,8 @@

Exam Complete!

if (!key || !examData || !attemptId) return; try { localStorage.setItem(`${key}_meta`, JSON.stringify({ - examData: examData, - attemptId: attemptId, + examData, + attemptId, savedAt: Date.now() })); localStorage.setItem('activeExamStateKey', key); @@ -425,7 +425,7 @@

Exam Complete!

}); try { localStorage.setItem(`${key}_answers`, JSON.stringify({ - answers: answers, + answers, savedAt: Date.now() })); } catch (e) { @@ -441,7 +441,8 @@

Exam Complete!

if (!saved) return; const state = JSON.parse(saved); // Expire saved answers older than 7 days - if (state.savedAt && (Date.now() - state.savedAt) > EXAM_STATE_EXPIRY_MS) { + if (state.savedAt && Number.isInteger(state.savedAt) && + (Date.now() - state.savedAt) > EXAM_STATE_EXPIRY_MS) { clearExamState(); return; }