From 745ce06f85497ed0091e2bbe9120e6fcb8424f3e Mon Sep 17 00:00:00 2001 From: qing <1776177662@qq.com> Date: Tue, 4 Aug 2026 22:47:48 +0800 Subject: [PATCH] =?UTF-8?q?fix(dnfs):=20=E5=89=8D=E7=AB=AF=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E8=8A=82=E7=82=B9=E8=BF=90=E8=A1=8C=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E5=B1=80=E9=83=A8=E8=BE=93=E5=87=BA=E7=BC=93=E5=86=B2+?= =?UTF-8?q?=E6=8D=95=E8=8E=B7=E7=AA=97=E5=8F=A3=E8=87=AA=E5=8A=A8=E6=81=A2?= =?UTF-8?q?=E5=A4=8Dconsole=EF=BC=8C=E9=81=BF=E5=85=8D=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E8=87=AA=E8=BA=AB=E6=97=A5=E5=BF=97(pid)=E5=88=B7=E8=BF=9B?= =?UTF-8?q?=E8=BE=93=E5=87=BA=E6=A1=86=E5=B9=B2=E6=89=B0=E5=AF=BC=E8=88=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- static/public/js/dnfs.js | 60 +++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/static/public/js/dnfs.js b/static/public/js/dnfs.js index 9997402..1af11e6 100644 --- a/static/public/js/dnfs.js +++ b/static/public/js/dnfs.js @@ -525,33 +525,40 @@ function renderSpJs(container, sp, appendNote) { out.style.display = out.style.display === 'none' ? 'block' : 'none'; if (out.dataset.executed) return; // 已执行过,仅切换显示 out.dataset.executed = '1'; - /* 首次注入 console 捕获:保留原 console 行为,同时备份到 __spLines(避免影响页面其他脚本) */ - if (!window.__spLines) { - window.__spLines = []; - var _orig = window.console; - window.console.log = function() { - window.__spLines.push(Array.prototype.slice.call(arguments).join(' ')); - _orig.log.apply(_orig, arguments); - }; - window.console.error = function() { - window.__spLines.push('Error: ' + Array.prototype.slice.call(arguments).join(' ')); - _orig.error.apply(_orig, arguments); - }; - window.console.warn = function() { - window.__spLines.push('Warn: ' + Array.prototype.slice.call(arguments).join(' ')); - _orig.warn.apply(_orig, arguments); - }; - window.addEventListener('error', function(ev) { - window.__spLines.push('Error: ' + ev.message); - }); - } + /* 本次执行专属输出缓冲:捕获脚本执行(含早期异步)期间的 console 输出,捕获窗口结束后恢复 + * 应用自身 console,避免把应用日志(如 load_notes 打印的 pid/节点对象)刷进输出框干扰交互。 */ + var lines = []; + var _orig = window.console; + var restore = function() { + window.console.log = _orig.log.bind(_orig); + window.console.error = _orig.error.bind(_orig); + window.console.warn = _orig.warn.bind(_orig); + }; + window.console.log = function() { + lines.push(Array.prototype.slice.call(arguments).join(' ')); + _orig.log.apply(_orig, arguments); + }; + window.console.error = function() { + lines.push('Error: ' + Array.prototype.slice.call(arguments).join(' ')); + _orig.error.apply(_orig, arguments); + }; + window.console.warn = function() { + lines.push('Warn: ' + Array.prototype.slice.call(arguments).join(' ')); + _orig.warn.apply(_orig, arguments); + }; + var errHandler = function(ev) { + lines.push('Error: ' + ev.message); + }; + window.addEventListener('error', errHandler); /* 解析完整 HTML 文档 */ var doc; try { doc = new DOMParser().parseFromString(sp.code, 'text/html'); } catch (err) { - window.__spLines.push('Error: 解析 HTML 失败: ' + (err.message || err)); - out.textContent = window.__spLines.join('\n'); + lines.push('Error: 解析 HTML 失败: ' + (err.message || err)); + restore(); + window.removeEventListener('error', errHandler); + out.textContent = lines.join('\n'); return; } /* 注入