问题描述
我正在开发基于Web的游戏。
在桌面浏览器中运行时,我可以在几乎所有浏览器中的经典Web开发人员控制台中轻松查看日志:
问题
如何像开发人员工具在台式机浏览器中提供的那样,在移动浏览器中获取console.log条目?
移动浏览器不提供打开开发者工具
的简便方法研究
我发现这些选项没有成功:
- https://help.duo.com/s/article/4957?language=en_US
- 但是我可以在Android浏览器上启用开发者工具
我正在审查的 - eruda,似乎是浏览器的仿真。
- 替代方案::我当时正在考虑创建一个极简主义的Web门户,在其中可以使用ajax(覆盖console.log)从我的网站发送日志。我相信当在移动浏览器中对Web进行测试时,在Web上查看日志可能会更舒适
编辑1
我认为#1如果不是侵入性的并且易于在开发阶段启用,则可能是一个解决方案。可能如下图所示,其中日志条目未修改UI:
解决方法
这可能有用吗?
// 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 );
}
}
,
最后,我创建了一个简单的库以在同一网站上查看我的日志:
我只需将其添加到我的网站顶部:
<script src="https://cdn.jsdelivr.net/gh/jrichardsz/log4browser@master/log4browser.min.js"></script>
注意:将其从您的产品包中删除