如何从移动设备上的移动设备获取console.log输出?

jdm*_*eld 10 javascript mobile android google-chrome

我从移动设备上做了很多开发工作.有没有办法让JS访问来自CONSOLE.LOG输出移动浏览器?

Mar*_*hes 5

目前,最好的方法是"挂钩"本机控制台并将输出显示为HTML,同时仍然允许输出转到本机控制台.

你可以很容易地实现自己的版本....

// 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 );
    }
}
Run Code Online (Sandbox Code Playgroud)

....但不是重新发明轮子,有一些你可能有兴趣尝试的项目.

我个人正在使用hnlDesign的mobileConsole并且非常满意.这很简单,也是您想要和期望的.

我最近意识到Eruda,但除了玩他们的演示之外,还没有机会测试它.它实现了更多的开发人员工具,但由于这个原因,许多项目也可能过度.它感觉不那么轻巧(它的文件大小肯定要大得多,甚至缩小!),如果你想要开发工具的广度和强度,最好使用远程调试.我们大多数想要移动控制台的人只是想要快速测试的基础知识等.


小智 5

使用Eruda,这是迄今为止最好的,至少是我尝试过的。