简体   繁体   English

如何将控制台添加到客户端页面

[英]How to add console to client side page

I'm looking for a way to have the console visible and able to accept input from the client side of a node.js electron app.我正在寻找一种使控制台可见并能够接受来自 node.js 电子应用程序客户端的输入的方法。 To be clear, it is an app I developed for my own personal use to help build charts and reports from local excel files, so security is not a problem.需要明确的是,这是我开发的一个应用程序,供我个人使用,用于帮助从本地 excel 文件构建图表和报告,因此安全性不是问题。

I have my data saved as JS objects that feed my charts and tables.我将我的数据保存为 JS 对象,用于提供我的图表和表格。 However, every once in a while I need to access data from an object that doesn't merit creating a client side process because of its rarity.然而,每隔一段时间,我需要从一个对象访问数据,因为它的稀有性而不值得创建客户端进程。 Is there a way I can have the console visible in the client side of my app so I can quickly look up data from my global objects in the console?有没有办法让控制台在我的应用程序的客户端可见,这样我就可以从控制台中的全局对象快速查找数据?

Thanks, Sychord谢谢,西和弦

You can override console.log 's default behavior to instead print to the page:您可以覆盖console.log的默认行为以改为打印到页面:

 console.log = function(text){ document.body.insertAdjacentHTML('beforeend', `<div class='log'><div class='text'>${text}</div><div class='time'>${new Date().toLocaleString()}</div></div>`) } console.log('Hello World!') console.log('Log #2')
 .log{display:flex;justify-content:space-between;padding:15px 20px;border-bottom:1px solid grey;margin-bottom:5px}.log .text{font-weight:bold}

To let the user input just like they would in the console, you can use eval to evaluate their JS:为了让用户像在控制台中一样输入,您可以使用eval来评估他们的 JS:

 console.log = function(text){ document.body.insertAdjacentHTML('beforeend', `<div class='log'><div class='text'>${text}</div><div class='time'>${new Date().toLocaleString()}</div></div>`) } window.onerror = function(text){ document.body.insertAdjacentHTML('beforeend', `<div class='log danger'><div class='text'>${text}</div><div class='time'>${new Date().toLocaleString()}</div></div>`) } btn.addEventListener('click', function(){ eval(input.value) })
 .log{display:flex;justify-content:space-between;padding:15px 20px;border-bottom:1px solid grey;margin-bottom:5px}.log .text{font-weight:bold}.danger{background-color:tomato}
 <input id="input"><button id="btn">Execute</button> <br><br> Try entering something like: console.log('hi')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM