偶尔,我会在禁用“检查”的计算机上做一些web开发,这会使调试JavaScript (一种非常擅长避免错误的语言)变得非常痛苦。
我创建了一个JavaScript文件,当您通过HTML页面上的脚本标记包含它时,它将重写控制台处理程序并向页面底部添加一些元素:

代码:
// https://gist.github.com/UnsignedArduino/e23b8329c3a786d1e4e99d8ee941436e
// Include this JavaScript file in an HTML file and it will add a DIV element to the bottom of the page which will contain console output and
// an textarea to run JavaScript code!
// Set false to do nothing
const on_page_console = true;
if (on_page_console) {
(() => {
const element_to_append_to = document.body;
element_to_append_to.appendChild(document.createElement("br"));
element_to_append_to.appendChild(document.createElement("br"));
const on_page_console_div = document.createElement("div");
on_page_console_div.style.border = "1px outset black";
on_page_console_div.style.padding = "5px";
const warning_b = document.createElement("b");
warning_b.innerHTML = "On page console:";
on_page_console_div.appendChild(warning_b);
element_to_append_to.appendChild(document.createElement("br"));
on_page_console_div.appendChild(document.createElement("br"));
const console_textarea = document.createElement("textarea");
console_textarea.type = "text";
console_textarea.rows = 10;
console_textarea.cols = 40;
console_textarea.id = "command_input";
console_textarea.name = "command_input";
console_textarea.readOnly = true;
console_textarea.style = "width: 100%; resize: vertical; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box;"
on_page_console_div.appendChild(console_textarea);
on_page_console_div.appendChild(document.createElement("br"));
const command_label = document.createElement("label");
command_label.for = "command_input";
command_label.innerHTML = "Run JavaScript code: " +
"(Remember that you can only run code in the context " +
'of /on_page_console.js - see ' +
'here.)';
on_page_console_div.appendChild(command_label);
const command_input = document.createElement("textarea");
command_input.type = "text";
command_input.rows = 10;
command_input.cols = 40;
command_input.id = "command_input";
command_input.name = "command_input";
command_input.style = "width: 100%; resize: vertical; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box;"
on_page_console_div.appendChild(command_input);
on_page_console_div.appendChild(document.createElement("br"));
const command_button = document.createElement("button");
command_button.type = "button";
command_button.innerHTML = "Run";
command_button.onclick = () => {
console.log("Result: " + eval(command_input.value));
};
on_page_console_div.appendChild(command_button);
element_to_append_to.appendChild(on_page_console_div);
const auto_scroll = true;
// https://stackoverflow.com/a/50773729/10291933
function produce_text(name, args) {
return args.reduce((output, arg) => {
return output + (typeof arg === "object" && (JSON || {}).stringify ? JSON.stringify(arg) : arg) + "\n";
}, "");
}
function rewire_logging_func(name) {
console["old" + name] = console[name];
console[name] = (...arguments) => {
console_textarea.innerHTML += produce_text(name, arguments);;
console_textarea.scrollTop = console_textarea.scrollHeight;
console["old" + name].apply(undefined, arguments);
};
}
function rewire_logging() {
rewire_logging_func("log");
rewire_logging_func("debug");
rewire_logging_func("warn");
rewire_logging_func("error");
rewire_logging_func("info");
}
window.onerror = (error_msg, url, line_number, col_number, error) => {
let error_output;
if (error.stack == null) {
error_output = error_msg + "\n URL: " + url + ":" + line_number + ":" + col_number;
} else {
error_output = error.stack;
}
console.error(error_output);
return false;
};
rewire_logging();
})();
}如果需要,这里有一个雷普。
如果我能在代码样式、如何实现这种东西的最佳实践以及性能方面得到帮助(可能很奇怪),我会很感激。(页面将在执行JavaScript时冻结)注意,这是根据服务器只能提供静态文件而不是注入HTML之类的事实设计的。
发布于 2022-04-05 18:00:10
以下是我的想法:
tl;dr:页面冻结可能与元素之间共享id有关。其他一些更改可能会修复可读性,这取决于您的首选项。
我最初在码页上做过这方面的工作。
const showCustomConsole = true
if(showCustomConsole){
let documentBody = document.body
let consoleTitle = `On Page Console:`
let consoleId = 'console_out'
let consoleLabel = `Run JavaScript code:
(Remember that you can only run code in the context
of /on_page_console.js - see
here.)
`
let scriptId = 'script_in'
let textAreaStyle = 'style="width: 100%; resize: vertical; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box;"'
function runHandler(){
console.log(`Result: ${eval(document.getElementById(scriptId).value)}`)
}
// https://stackoverflow.com/a/50773729/10291933
function produce_text(name, args) {
return args.reduce((output, arg) => {
return output + (typeof arg === "object" && (JSON || {}).stringify ? JSON.stringify(arg) : arg) + "\n";
}, "");
}
function rewire_logging_func(name) {
let console_output = document.getElementById(consoleId)
console["old" + name] = console[name];
console[name] = (...arguments) => {
console_output.innerHTML += produce_text(name, arguments);;
console_output.scrollTop = console_output.scrollHeight;
console["old" + name].apply(undefined, arguments);
};
}
function rewire_logging() {
rewire_logging_func("log");
rewire_logging_func("debug");
rewire_logging_func("warn");
rewire_logging_func("error");
rewire_logging_func("info");
}
window.onerror = (error_msg, url, line_number, col_number, error) => {
let error_output;
if (error.stack == null) {
error_output = error_msg + "\n URL: " + url + ":" + line_number + ":" + col_number;
} else {
error_output = error.stack;
}
console.error(error_output);
return false;
};
let consoleHtml = `
${consoleTitle}
${consoleLabel}
Run
`
documentBody.insertAdjacentHTML("beforeend", consoleHtml)
rewire_logging()
} Inspect console directly on a web page
Sample Web Pagehttps://codereview.stackexchange.com/questions/275282
复制相似问题