Spaces:
Sleeping
Sleeping
File size: 6,024 Bytes
e66cfb4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | /**
* session_state.js
* 事件驅動版,不做定時掃描,只在互動時儲存。
*/
(function () {
"use strict";
var STORE_KEY = "dash_math_page_state";
function loadState() {
try { return JSON.parse(sessionStorage.getItem(STORE_KEY) || "{}"); }
catch (e) { return {}; }
}
function saveState(s) {
try { sessionStorage.setItem(STORE_KEY, JSON.stringify(s)); }
catch (e) {}
}
function getPathname() { return window.location.pathname || "/"; }
function saveProp(key, value) {
var s = loadState(), p = getPathname();
if (!s[p]) s[p] = {};
s[p][key] = value;
saveState(s);
}
function saveNested(group, key, value) {
var s = loadState(), p = getPathname();
if (!s[p]) s[p] = {};
if (!s[p][group]) s[p][group] = {};
s[p][group][key] = value;
saveState(s);
}
function getPs() {
var s = loadState(), p = getPathname();
return s[p] || {};
}
// ── 恢復 ──────────────────────────────────────────────────────────────
var _restoring = false;
function restoreState() {
var ps = getPs();
_restoring = true;
// Section
if (ps.section) {
var b = document.querySelector('[data-sid="' + ps.section + '"]');
if (b) b.click();
}
// 填空題
var inputs = ps.quiz_inputs || {};
Object.keys(inputs).forEach(function (qid) {
var val = inputs[qid];
if (!val) return;
// 用 querySelectorAll 找包含此 qid 的 input
document.querySelectorAll("input").forEach(function (el) {
var w = el.closest("[id]");
if (!w || w.id.indexOf(qid) === -1 || w.id.indexOf("quiz-input") === -1) return;
var setter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, "value").set;
setter.call(el, val);
el.dispatchEvent(new Event("input", { bubbles: true }));
});
});
// 解法展開
var solutions = ps.solutions || {};
Object.keys(solutions).forEach(function (key) {
if (!solutions[key]) return;
document.querySelectorAll("button").forEach(function (btn) {
if (btn.id && btn.id.indexOf(key) > -1 && btn.id.indexOf("solution-toggle") > -1) {
btn.click();
}
});
});
setTimeout(function () { _restoring = false; }, 800);
}
// ── 掛載監聽 ─────────────────────────────────────────────────────────
var _attached = new Set();
function attachListeners() {
// Section 按鈕
document.querySelectorAll("[data-sid]").forEach(function (btn) {
var sid = btn.getAttribute("data-sid");
if (_attached.has("s:" + sid)) return;
_attached.add("s:" + sid);
btn.addEventListener("click", function () {
if (!_restoring) saveProp("section", sid);
});
});
// 填空題 input
document.querySelectorAll("input").forEach(function (el) {
if (_attached.has(el)) return;
var w = el.closest("[id]");
if (!w || !w.id || w.id.indexOf("quiz-input") === -1) return;
var m = w.id.match(/"qid":"([^"]+)"/);
if (!m) return;
var qid = m[1];
_attached.add(el);
el.addEventListener("change", function () {
if (!_restoring) saveNested("quiz_inputs", qid, el.value);
});
});
// 解法 toggle 按鈕
document.querySelectorAll("button").forEach(function (btn) {
if (_attached.has(btn)) return;
if (!btn.id || btn.id.indexOf("solution-toggle") === -1) return;
var m = btn.id.match(/"key":"([^"]+)"/);
if (!m) return;
var key = m[1];
_attached.add(btn);
btn.addEventListener("click", function () {
if (_restoring) return;
// 延遲判斷是否展開
setTimeout(function () {
var col = document.querySelector('[id*="' + key + '"][id*="solution-collapse"]');
var opened = col ? col.offsetHeight > 10 : false;
saveNested("solutions", key, opened);
}, 400);
});
});
}
// ── 換頁偵測 ─────────────────────────────────────────────────────────
var _lastPath = null, _timer = null;
function checkPath() {
var cur = getPathname();
if (cur !== _lastPath) {
_lastPath = cur;
_attached.clear();
clearTimeout(_timer);
_timer = setTimeout(function () {
attachListeners();
restoreState();
}, 900);
} else {
attachListeners();
}
}
// MutationObserver:只做 debounce checkPath,不掃描整個 DOM
var _mutTimer = null;
var obs = new MutationObserver(function () {
clearTimeout(_mutTimer);
_mutTimer = setTimeout(checkPath, 200);
});
function init() {
_lastPath = getPathname();
obs.observe(document.body, { childList: true, subtree: false }); // subtree:false 減少觸發
setTimeout(function () {
attachListeners();
restoreState();
}, 1000);
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", init);
} else {
init();
}
})();
|