如何检测浏览器控制台/检查器是否打开*? [英] How to detect if browser console / inspector is *open*?

查看:615
本文介绍了如何检测浏览器控制台/检查器是否打开*?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确定用户是否拥有浏览器控制台的最佳方式是什么(例如firebug,webkit
督察,Opera蜻蜓) open



(即我不感兴趣仅仅在脚本中检测到控制台对象的存在,我想知道用户何时实际打开了调试器面板。理想情况下,在主流浏览器(IE / Safari / Chrome / Firefox ...,甚至可能的移动浏览器)中都是如此。

>如果,您愿意接受对用户的干扰,
您可以使用调试器声明,因为它可在所有主要浏览器中使用。



注意:如果您的应用的用户对控制台的使用感兴趣,他们可能熟悉开发工具,并且不会对它的出现感到意外。



简而言之,这个陈述是作为breakpo int,并且仅当浏览器的开发工具在上时才会影响UI



以下是一个示例测试:

 < body> 
< p> Devtools是< span id ='test'> off< / span>< / p>
< script>
var minimalUserResponseInMiliseconds = 100;
var before = new Date()。getTime();
调试器;
var after = new Date()。getTime();
if(after - before> minimalUserResponseInMiliseconds){//用户必须通过打开的开发工具手动恢复脚本
document.getElementById('test')。innerHTML ='on';
}
< / script>

< / body>

免责声明:我最初发布了这可能是重复的问题


What's the best way to determine if the user has the browser console (i.e. firebug, webkit inspector, Opera dragonfly) open?

(I.e. I'm not interested in merely detecting the presence of the console object in script. I want to know when the user has actually opened the debugger panel. Ideally across the major browsers (IE/Safari/Chrome/Firefox... and even mobile browsers if possible)

解决方案

If you are willing to accept an interference for the user, you could use the debugger statement, as it is available in all major browsers.

Side note: If the users of your app are interested in console usage, they're probably familiar with dev tools, and will not be surprised by it showing up.

In short, the statement is acting as a breakpoint, and will affect the UI only if the browser's development tools is on.

Here's an example test:

<body>
<p>Devtools is <span id='test'>off</span></p>
<script>
  var minimalUserResponseInMiliseconds = 100;
  var before = new Date().getTime();
  debugger;
  var after = new Date().getTime();
  if (after - before > minimalUserResponseInMiliseconds) { // user had to resume the script manually via opened dev tools 
    document.getElementById('test').innerHTML = 'on';
  }
</script>

</body>

DISCLAIMER: I initially published this exact answer for this possibly duplicate question

这篇关于如何检测浏览器控制台/检查器是否打开*?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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