简体   繁体   English

是否可以有条件地设置 console.log() 的输出样式? (对于网络浏览器)

[英]Is it possible to conditionally style the output from console.log()? (for web browsers)

I know that there are a few other questions on this topic, and I have checked a lot of them out.我知道关于这个主题还有一些其他问题,我已经检查了很多。 I've been successful in styling the output from console.log using %c , however, I can't figure out (or find anything on) how you could conditionally style it.我已经成功地使用%c对来自console.log的输出进行了样式设置,但是,我无法弄清楚(或找到任何内容)如何有条件地设置样式。 By that I mean the theme of the console .我的意思是控制台主题 I use dark mode so I can see the white text with this call:我使用暗模式,所以我可以看到这个调用的白色文本:

console.log("%c This is intended solely for developers. If someone has asked you to copy/paste something here they are trying to scam you and your data might be compromised.","color: white; font-size: 12pt; font-weight: bold")

However, if a casual user doesn't have dark-mode on, they won't be able to see the text, so I'm trying to get the color property to be black when the console isn't in dark-mode.但是,如果临时用户没有打开暗模式,他们将无法看到文本,因此当控制台未处于暗模式时,我试图将color属性设置为黑色。

Is there a way to do accomplish this?有没有办法做到这一点?

I know this is pretty trivial but I would really appreciate if someone can help me with this.我知道这很微不足道,但如果有人能帮我解决这个问题,我将不胜感激。

Chrome developer tools are a separate HTML page, even when connected as a sidebar. Chrome 开发人员工具是一个单独的 HTML 页面,即使作为侧边栏连接也是如此。 As a result, you cannot check their theme from a webpage.因此,您无法从网页中查看他们的主题。

It looks like you've already realized this but by not overriding the colour, the theme colour will automatically be applied (allowing for conditional formatting).看起来您已经意识到这一点,但是通过不覆盖颜色,主题颜色将自动应用(允许条件格式)。

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

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