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

新手上路,请多包涵

我在移动设备上做很多开发工作。有没有办法让 js 从移动浏览器 访问 console.log 输出?

原文由 jdmayfield 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 589
2 个回答

目前,最好的方法是“连接到”本机控制台并将输出显示为 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 );
    }
}

….但与其重新发明轮子,还有一些您可能有兴趣尝试的项目。

我个人使用 hnlDesign 的 mobileConsole 并且对它非常满意。它很简单,正是您想要和期望的。

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

原文由 Marcus Hughes 发布,翻译遵循 CC BY-SA 3.0 许可协议

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

原文由 Alejo Salvo 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题