Files
myblog/assets/js/main.js
T
2026-09-09 19:30:18 +08:00

44 lines
1.6 KiB
JavaScript

(function () {
const btn = document.getElementById("themeBtn");
if (!btn) return;
function applyTheme(t) {
document.documentElement.setAttribute("data-theme", t);
btn.textContent = t === "dark" ? "浅色模式" : "深色模式";
try { localStorage.setItem("glass-theme", t); } catch(e) {}
}
function currentTheme() {
return document.documentElement.getAttribute("data-theme") || "light";
}
var saved = "light";
try { saved = localStorage.getItem("glass-theme") || "light"; } catch(e) {}
applyTheme(saved);
btn.addEventListener("click", function () {
applyTheme(currentTheme() === "dark" ? "light" : "dark");
});
})();
(function () {
var modal = document.getElementById("confirmModal");
if (!modal) return;
var text = document.getElementById("confirmText");
var ok = document.getElementById("confirmOk");
var cancel = document.getElementById("confirmCancel");
var target = null;
function open(msg, href) {
if (text) text.textContent = msg;
target = href;
modal.style.display = "flex";
}
function close() { modal.style.display = "none"; target = null; }
document.querySelectorAll("[data-confirm]").forEach(function (el) {
el.addEventListener("click", function (e) {
e.preventDefault();
open(el.getAttribute("data-confirm"), el.href);
});
});
if (ok) ok.addEventListener("click", function () { if (target) location.href = target; });
if (cancel) cancel.addEventListener("click", close);
modal.addEventListener("click", function (e) { if (e.target === modal) close(); });
document.addEventListener("keydown", function (e) { if (e.key === "Escape") close(); });
})();