// 主题初始化 (必须在渲染前执行, 防止闪白) (function(){ var s = localStorage.getItem("sensu-theme") || "dark"; document.documentElement.setAttribute("data-theme", s); })(); window.toggleTheme = function(){ var t = document.documentElement.getAttribute("data-theme") === "light" ? "dark" : "light"; document.documentElement.setAttribute("data-theme", t); localStorage.setItem("sensu-theme", t); var btn = document.querySelector(".theme-toggle"); if(btn) btn.textContent = t === "light" ? "☀️" : "🌙"; }; // 初始化检查 window.onload = async () => { // 设置按钮初始图标 var btn = document.querySelector(".theme-toggle"); if(btn) btn.textContent = document.documentElement.getAttribute("data-theme") === "light" ? "☀️" : "🌙"; try { const res = await fetch('./api/auth/status', { credentials: 'include' }); if(res.status === 401) { window.location.href = './index.html'; return; } const data = await res.json(); if(!data.authenticated) { window.location.href = './index.html'; return; } document.getElementById('uname').textContent = data.username || 'Admin'; loadPage('dashboard'); // 默认加载 } catch(e) { window.location.href = './index.html'; } }; // 路由加载器 async function loadPage(pageName) { const content = document.getElementById('page-content'); const bar = document.getElementById('progress'); // 侧边栏高亮 document.querySelectorAll('.nav-item').forEach(el => el.classList.remove('active')); document.querySelector(`.nav-item[data-page="${pageName}"]`)?.classList.add('active'); // 进度条动画 bar.classList.add('active'); bar.style.width = '0%'; await new Promise(r => requestAnimationFrame(() => { bar.style.width = '80%'; setTimeout(r, 100); })); try { const resp = await fetch(`./static/pages/${pageName}.html`); if(!resp.ok) throw new Error('404'); const html = await resp.text(); // Extract inline scripts before innerHTML (browsers skip them) const scripts = []; const cleanHtml = html.replace(/]*>([\s\S]*?)<\/script>/gi, (m, code) => { scripts.push(code.trim()); return ''; }); content.innerHTML = cleanHtml; // Execute inline scripts via dynamic