javascript 如何从移动设备上的移动设备获取 console.log 输出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47064232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get console.log output from my mobile ON the mobile device?
提问by jdmayfield
I do a lot of my dev work from mobile devices. Is there a way to get js access to console.log output from withina mobile browser?
我在移动设备上做了很多开发工作。有没有办法从获取到的console.log输出JS访问中移动浏览器?
采纳答案by Marcus Hughes
Currently, the best method would be to 'hook into' the native console and display the output as HTML, while still allowing the output to go to the native console.
目前,最好的方法是“连接”到本地控制台并将输出显示为 HTML,同时仍然允许输出到本地控制台。
You could implement your own version very easily....
你可以很容易地实现你自己的版本......
// Reference to an output container, use 'pre' styling for JSON output
var output = document.createElement('pre');
document.body.appendChild(output);
// Reference to native method(s)
var oldLog = console.log;
console.log = function( ...items ) {
// Call native method first
oldLog.apply(this,items);
// Use JSON to transform objects, all others display normally
items.forEach( (item,i)=>{
items[i] = (typeof item === 'object' ? JSON.stringify(item,null,4) : item);
});
output.innerHTML += items.join(' ') + '<br />';
};
// You could even allow Javascript input...
function consoleInput( data ) {
// Print it to console as typed
console.log( data + '<br />' );
try {
console.log( eval( data ) );
} catch (e) {
console.log( e.stack );
}
}
....but rather than reinvent the wheel, there's a couple projects you might be interested in trying.
....但与其重新发明轮子,不如尝试一些您可能感兴趣的项目。
I'm personally using hnlDesign's mobileConsoleand have been very happy with it. It's simple and just what you'd want and expect.
我个人在使用hnlDesign 的 mobileConsole并且对它非常满意。这很简单,正是您想要和期望的。
I recently became aware of Erudabut haven't had a chance to test it, other than playing with their demo. It implements a lot more developer tools, but for this reason might also be overkill for a lot of projects. It doesn't feel as lightweight (its file size is definitely much larger, even minified!), and if you want the breadth and intensity of developer tools, it would be better to use remote debugging. Most of us who are wanting a mobile console are just wanting the basics for quick tests and the like.
我最近才知道Eruda,但除了玩他们的演示之外,还没有机会测试它。它实现了更多的开发人员工具,但由于这个原因,对于很多项目来说也可能是矫枉过正。感觉没有那么轻量级(它的文件大小肯定要大得多,甚至缩小了!),如果您想要开发人员工具的广度和强度,最好使用远程调试。我们大多数想要移动控制台的人只是想要快速测试等的基础知识。
回答by Alejo Salvo
回答by Gene Bo
For Android - I found this works:
对于 Android - 我发现这有效:
- Open Android Studio IDE
- Connecting the device in debug mode
- Open logcat
- Run the browser UI on android device - to see the console log output
- 打开 Android Studio IDE
- 在调试模式下连接设备
- 打开 logcat
- 在 android 设备上运行浏览器 UI - 查看控制台日志输出
Here's a few console.log() lines from logcat:
这是 logcat 中的一些 console.log() 行:
[INFO:CONSOLE(18)] "100 - checking FUNC PARAM ... ", source: https://somewhere/util/message_util.js (18)
[INFO:CONSOLE(18)] "100 - checking FUNC PARAM ... ", source: https://somewhere/util/message_util.js (18)
[INFO:CONSOLE(18)] "101 - > ENTER: AppAuth.onHasAuthFunc", source: https://somewhere/util/message_util.js (18)
[INFO:CONSOLE(18)] "101 - > ENTER: AppAuth.onHasAuthFunc", source: https://somewhere/util/message_util.js (18)
Idea to try this approach, thanks to @Marcus's answer, suggesting:
感谢@Marcus 的回答,尝试这种方法的想法建议:
" 'hook into' the native console "
“'挂钩'本地控制台”
~~~~~~
~~~~~~
Also, saw other posts suggesting to type: about:debug
另外,看到其他帖子建议输入: about:debug
.. into the address bar of device browser. Not sure if that had anything to do with getting the connection to work
.. 进入设备浏览器的地址栏。不确定这是否与让连接正常工作有关

