fix(dnfs): 前端代码节点运行改为局部输出缓冲+捕获窗口自动恢复console,避免应用自身日志(pid)刷进输出框干扰导航
This commit is contained in:
1 parent
f17a6a8233
commit
745ce06f85
1 file changed
+35
-25
+35
-25
@@ -525,33 +525,40 @@ function renderSpJs(container, sp, appendNote) {
|
|||||||
out.style.display = out.style.display === 'none' ? 'block' : 'none';
|
out.style.display = out.style.display === 'none' ? 'block' : 'none';
|
||||||
if (out.dataset.executed) return; // 已执行过,仅切换显示
|
if (out.dataset.executed) return; // 已执行过,仅切换显示
|
||||||
out.dataset.executed = '1';
|
out.dataset.executed = '1';
|
||||||
/* 首次注入 console 捕获:保留原 console 行为,同时备份到 __spLines(避免影响页面其他脚本) */
|
/* 本次执行专属输出缓冲:捕获脚本执行(含早期异步)期间的 console 输出,捕获窗口结束后恢复
|
||||||
if (!window.__spLines) {
|
* 应用自身 console,避免把应用日志(如 load_notes 打印的 pid/节点对象)刷进输出框干扰交互。 */
|
||||||
window.__spLines = [];
|
var lines = [];
|
||||||
var _orig = window.console;
|
var _orig = window.console;
|
||||||
window.console.log = function() {
|
var restore = function() {
|
||||||
window.__spLines.push(Array.prototype.slice.call(arguments).join(' '));
|
window.console.log = _orig.log.bind(_orig);
|
||||||
_orig.log.apply(_orig, arguments);
|
window.console.error = _orig.error.bind(_orig);
|
||||||
};
|
window.console.warn = _orig.warn.bind(_orig);
|
||||||
window.console.error = function() {
|
};
|
||||||
window.__spLines.push('Error: ' + Array.prototype.slice.call(arguments).join(' '));
|
window.console.log = function() {
|
||||||
_orig.error.apply(_orig, arguments);
|
lines.push(Array.prototype.slice.call(arguments).join(' '));
|
||||||
};
|
_orig.log.apply(_orig, arguments);
|
||||||
window.console.warn = function() {
|
};
|
||||||
window.__spLines.push('Warn: ' + Array.prototype.slice.call(arguments).join(' '));
|
window.console.error = function() {
|
||||||
_orig.warn.apply(_orig, arguments);
|
lines.push('Error: ' + Array.prototype.slice.call(arguments).join(' '));
|
||||||
};
|
_orig.error.apply(_orig, arguments);
|
||||||
window.addEventListener('error', function(ev) {
|
};
|
||||||
window.__spLines.push('Error: ' + ev.message);
|
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 文档 */
|
/* 解析完整 HTML 文档 */
|
||||||
var doc;
|
var doc;
|
||||||
try {
|
try {
|
||||||
doc = new DOMParser().parseFromString(sp.code, 'text/html');
|
doc = new DOMParser().parseFromString(sp.code, 'text/html');
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
window.__spLines.push('Error: 解析 HTML 失败: ' + (err.message || err));
|
lines.push('Error: 解析 HTML 失败: ' + (err.message || err));
|
||||||
out.textContent = window.__spLines.join('\n');
|
restore();
|
||||||
|
window.removeEventListener('error', errHandler);
|
||||||
|
out.textContent = lines.join('\n');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
/* 注入 <style> 到页面 head */
|
/* 注入 <style> 到页面 head */
|
||||||
@@ -583,12 +590,15 @@ function renderSpJs(container, sp, appendNote) {
|
|||||||
logBox.className = 'sp-log';
|
logBox.className = 'sp-log';
|
||||||
out.appendChild(logBox);
|
out.appendChild(logBox);
|
||||||
var render = function() {
|
var render = function() {
|
||||||
var lines = window.__spLines.slice();
|
|
||||||
logBox.textContent = lines.join('\n') || '执行完成';
|
logBox.textContent = lines.join('\n') || '执行完成';
|
||||||
};
|
};
|
||||||
render();
|
render();
|
||||||
/* 异步输出(setTimeout 等)轮询刷新 */
|
/* 异步输出(setTimeout 等)轮询刷新;捕获窗口 3 秒后恢复应用自身 console,避免永久污染全局日志 */
|
||||||
var timer = setInterval(render, 400);
|
setInterval(render, 400);
|
||||||
|
setTimeout(function() {
|
||||||
|
restore();
|
||||||
|
window.removeEventListener('error', errHandler);
|
||||||
|
}, 3000);
|
||||||
});
|
});
|
||||||
bar.appendChild(audit_btn);
|
bar.appendChild(audit_btn);
|
||||||
bar.appendChild(run_btn);
|
bar.appendChild(run_btn);
|
||||||
|
|||||||
Reference in new issue
Block a user