sh1k4ku commited on
Commit
21f8947
·
verified ·
1 Parent(s): 8cc8d07

Session: persist answers in localStorage with no expiry (until Clear)

Browse files
Files changed (2) hide show
  1. assignment1.html +2 -2
  2. assignment2.html +6 -6
assignment1.html CHANGED
@@ -278,7 +278,7 @@ problemSel.onchange = async () => {
278
  applyMode();
279
 
280
  // ===== 24h local session: keep the code you typed across reloads =====
281
- const A1_KEY = "st_a1_session_v1", A1_TTL = 24 * 3600 * 1000;
282
  let __a1timer = null;
283
  function persistA1(){
284
  clearTimeout(__a1timer);
@@ -288,7 +288,7 @@ function persistA1(){
288
  }
289
  function restoreA1(){
290
  let d; try { d = JSON.parse(localStorage.getItem(A1_KEY)); } catch(_){ return; }
291
- if (!d || !d.ts || (Date.now() - d.ts) > A1_TTL){ localStorage.removeItem(A1_KEY); return; }
292
  if (d.work){
293
  if (typeof d.work.run === "string") work.run = d.work.run;
294
  if (d.work.test){ if (d.work.test.Q1 != null) work.test.Q1 = d.work.test.Q1; if (d.work.test.Q2 != null) work.test.Q2 = d.work.test.Q2; }
 
278
  applyMode();
279
 
280
  // ===== 24h local session: keep the code you typed across reloads =====
281
+ const A1_KEY = "st_a1_session_v1"; // persists in this browser (no expiry)
282
  let __a1timer = null;
283
  function persistA1(){
284
  clearTimeout(__a1timer);
 
288
  }
289
  function restoreA1(){
290
  let d; try { d = JSON.parse(localStorage.getItem(A1_KEY)); } catch(_){ return; }
291
+ if (!d) return;
292
  if (d.work){
293
  if (typeof d.work.run === "string") work.run = d.work.run;
294
  if (d.work.test){ if (d.work.test.Q1 != null) work.test.Q1 = d.work.test.Q1; if (d.work.test.Q2 != null) work.test.Q2 = d.work.test.Q2; }
assignment2.html CHANGED
@@ -434,8 +434,7 @@ document.getElementById("copyBtn").onclick = async () => {
434
  };
435
 
436
  // ============ 24h local session (auto-save / restore) ============
437
- const SESSION_KEY = "st_a2_session_v1";
438
- const SESSION_TTL = 24 * 3600 * 1000; // 24 hours
439
  let __saveTimer = null;
440
  function saveSession(){
441
  // debounce rapid edits (typing in the editor / blanks)
@@ -466,7 +465,7 @@ function restoreRadio(name, val){
466
  function loadSession(){
467
  let d;
468
  try { d = JSON.parse(localStorage.getItem(SESSION_KEY)); } catch(_){ return; }
469
- if (!d || !d.ts || (Date.now() - d.ts) > SESSION_TTL){ localStorage.removeItem(SESSION_KEY); return; }
470
  document.getElementById("studentId").value = d.studentId || "";
471
  restoreRadio("q1", d.q1); restoreRadio("q2", d.q2); restoreRadio("q5", d.q5);
472
  if (d.q3){ answers.q3 = Object.assign({}, d.q3);
@@ -478,11 +477,12 @@ function loadSession(){
478
  lastQ4 = { original_output: d.q4.original_output || "", mutant_output: d.q4.mutant_output || "" };
479
  }
480
  // banner
481
- const mins = Math.round((Date.now() - d.ts) / 60000);
482
- const ago = mins < 60 ? mins + " min ago" : Math.round(mins/60) + " h ago";
 
483
  const b = document.createElement("div");
484
  b.style.cssText = "margin:10px 0;padding:8px 12px;border:1px solid var(--border);border-radius:8px;background:#262a3f;font-size:12px;color:var(--muted)";
485
- b.innerHTML = `↺ Restored your answers from this browser (saved ${ago}). They are kept for 24 hours. `;
486
  const x = document.createElement("button"); x.textContent = "Clear & start fresh"; x.className = "ghost";
487
  x.style.cssText = "padding:3px 10px;font-size:11px;margin-left:6px";
488
  x.onclick = clearSession;
 
434
  };
435
 
436
  // ============ 24h local session (auto-save / restore) ============
437
+ const SESSION_KEY = "st_a2_session_v1"; // persists in this browser until "Clear saved"
 
438
  let __saveTimer = null;
439
  function saveSession(){
440
  // debounce rapid edits (typing in the editor / blanks)
 
465
  function loadSession(){
466
  let d;
467
  try { d = JSON.parse(localStorage.getItem(SESSION_KEY)); } catch(_){ return; }
468
+ if (!d) return;
469
  document.getElementById("studentId").value = d.studentId || "";
470
  restoreRadio("q1", d.q1); restoreRadio("q2", d.q2); restoreRadio("q5", d.q5);
471
  if (d.q3){ answers.q3 = Object.assign({}, d.q3);
 
477
  lastQ4 = { original_output: d.q4.original_output || "", mutant_output: d.q4.mutant_output || "" };
478
  }
479
  // banner
480
+ let ago = "earlier";
481
+ if (d.ts){ const mins = Math.round((Date.now() - d.ts) / 60000);
482
+ ago = mins < 1 ? "just now" : mins < 60 ? mins + " min ago" : mins < 1440 ? Math.round(mins/60) + " h ago" : Math.round(mins/1440) + " d ago"; }
483
  const b = document.createElement("div");
484
  b.style.cssText = "margin:10px 0;padding:8px 12px;border:1px solid var(--border);border-radius:8px;background:#262a3f;font-size:12px;color:var(--muted)";
485
+ b.innerHTML = `↺ Restored your answers from this browser (saved ${ago}). They stay saved in this browser until you press Clear. `;
486
  const x = document.createElement("button"); x.textContent = "Clear & start fresh"; x.className = "ghost";
487
  x.style.cssText = "padding:3px 10px;font-size:11px;margin-left:6px";
488
  x.onclick = clearSession;