From c07cea4ff76bbaf7210fade06c97c306f6f5d490 Mon Sep 17 00:00:00 2001 From: qinglong Date: Thu, 11 Jun 2026 14:43:24 +0800 Subject: [PATCH] Fix page loader: execute inline scripts, JS module loading optional - innerHTML strips script tags, now extract and eval them - External .js module: 404 = skip gracefully (no error) - Progress bar: completes regardless of JS module - Proxy/projects pages now render with CSS + inline JS executes --- config/plugins/commands.yaml | 2 +- config/services/network_routes.yaml | 2 +- static/web_panel/js/app.js | 53 ++++++++++++++++------------- 3 files changed, 31 insertions(+), 26 deletions(-) diff --git a/config/plugins/commands.yaml b/config/plugins/commands.yaml index 827a44a..9e6c288 100644 --- a/config/plugins/commands.yaml +++ b/config/plugins/commands.yaml @@ -92,7 +92,7 @@ commands: permissions: - framework.command.test source: internal -last_updated: 279099.130874572 +last_updated: 279407.522655496 plugin_commands: example_plugin: echo: *id001 diff --git a/config/services/network_routes.yaml b/config/services/network_routes.yaml index 9c9bde3..a307021 100644 --- a/config/services/network_routes.yaml +++ b/config/services/network_routes.yaml @@ -1,5 +1,5 @@ http_port: 4200 -last_updated: 279099.134553218 +last_updated: 279407.52679284 plugin_routes: example_plugin: - methods: diff --git a/static/web_panel/js/app.js b/static/web_panel/js/app.js index e26fa2c..78621c2 100644 --- a/static/web_panel/js/app.js +++ b/static/web_panel/js/app.js @@ -43,35 +43,40 @@ async function loadPage(pageName) { await new Promise(r => requestAnimationFrame(() => { bar.style.width = '80%'; setTimeout(r, 100); })); try { - // 🟢 关键修改:fetch 路径必须包含 /static/ - const html = await fetch(`./static/pages/${pageName}.html`).then(r => { - if(!r.ok) throw new Error('404'); - return r.text(); + 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 = html; - - // 动态加载对应 JS 模块 - // 🟢 关键修改:script src 路径必须包含 /static/ - const script = document.createElement('script'); - script.src = `./static/pages/${pageName}.js?t=${Date.now()}`; - - script.onload = () => { - // 触发模块初始化 - const moduleName = pageName.charAt(0).toUpperCase() + pageName.slice(1) + 'Module'; - if(window[moduleName]?.init) { - window[moduleName].init(); + + content.innerHTML = cleanHtml; + + // Execute inline scripts + for(const code of scripts) { + try { (new Function(code)).call(window); } catch(e) { console.error('Inline script error:', e); } + } + + // Load external JS module (optional — skip if 404) + try { + const jsResp = await fetch(`./static/pages/${pageName}.js?t=${Date.now()}`); + if(jsResp.ok) { + const jsCode = await jsResp.text(); + (new Function(jsCode)).call(window); + const moduleName = pageName.charAt(0).toUpperCase() + pageName.slice(1) + 'Module'; + if(window[moduleName]?.init) window[moduleName].init(); } - bar.style.width = '100%'; - setTimeout(() => bar.classList.remove('active'), 200); - }; - script.onerror = () => { - throw new Error('JS Load Failed'); - }; - document.head.appendChild(script); + } catch(e) { /* JS module optional */ } + + bar.style.width = '100%'; + setTimeout(() => bar.classList.remove('active'), 200); } catch(e) { content.innerHTML = `
页面加载失败: ${e.message}
`; bar.style.background = 'var(--error)'; - setTimeout(() => { bar.style.width = '100%'; setTimeout(() => { bar.classList.remove('active'); bar.style.background = 'var(--accent)'; }, 200); }, 100); + setTimeout(() => { bar.style.width = '100%'; setTimeout(() => { bar.classList.remove('active'); bar.style.background = 'var(--primary)'; }, 200); }, 100); } }