如何在 JavaScript 中从 Chrome 的控制台读取 [英] How to read from Chrome's console in JavaScript
问题描述
我想在我的应用程序中放置一个按钮,如果您按下它,它将获取写入控制台的所有内容并将其通过电子邮件发送给我(用于报告错误).我知道我可以保留一个变量,每次执行 console.log 时也会将消息附加到该变量,但我试图将应用程序的内存消耗保持在较低水平,因此仅从控制台.
I would like to put a button in my app, if you press it it will get the contents of everything that was written to the console and email it to me (for reporting bugs). I know I can keep a variable around and every time I do a console.log also append the message to that variable but I am trying to keep the memory consumption of the app low so it would be much more efficient just to grab it from the console.
有没有办法从 javascript 中检索控制台消息?
Is there a way to retrieve the console messages from javascript?
推荐答案
你不能.无法从 JavaScript 读取控制台中的内容.
You can't. What's in the console can't be read from JavaScript.
您可以做的是挂钩 console.log
功能,以便您在记录时进行存储:
What you can do is hook the console.log
function so that you store when it logs :
console.stdlog = console.log.bind(console);
console.logs = [];
console.log = function(){
console.logs.push(Array.from(arguments));
console.stdlog.apply(console, arguments);
}
console.logs
包含所有记录的内容.您可以随时通过执行 console.logs.length = 0;
来清理它.
console.logs
contains all what was logged. You can clean it at any time by doing console.logs.length = 0;
.
您仍然可以通过调用 console.stdlog
来执行标准的、非存储的日志.
You can still do a standard, non storing, log by calling console.stdlog
.
这篇关于如何在 JavaScript 中从 Chrome 的控制台读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!