如何从移动设备上的移动设备获取console.log输出? [英] How can I get console.log output from my mobile ON the mobile device?

查看:121
本文介绍了如何从移动设备上的移动设备获取console.log输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的很多开发工作都是通过移动设备完成的.有没有办法让js从内部移动浏览器访问console.log输出?

I do a lot of my dev work from mobile devices. Is there a way to get js access to console.log output from within a mobile browser?

推荐答案

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

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.

您可以非常轻松地实现自己的版本....

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 mobileConsole and have been very happy with it. It's simple and just what you'd want and expect.

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

I recently became aware of Eruda but 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.

这篇关于如何从移动设备上的移动设备获取console.log输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆