javascript - 如何检测浏览器控制台/检查器是否*打开*?

标签 javascript google-chrome webkit firebug web-inspector

确定用户是否拥有浏览器控制台(即 firebug、webkit 检查员,歌剧蜻蜓)打开

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

最佳答案

如果您愿意为用户接受干扰, 你可以使用 the debugger statement ,因为它在所有主流浏览器中都可用。

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

简而言之,该语句充当断点,只有在浏览器的开发工具打开时才会影响 UI。

这是一个示例测试:

<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>

免责声明:我最初为 this possibly duplicate question 发布了这个确切的答案

关于javascript - 如何检测浏览器控制台/检查器是否*打开*?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20507780/

相关文章:

javascript - 通过 ajax 刷新数据时覆盖表单 'Reset' 行为

javascript - Mongodb 显示每个条目的不同条目数

javascript - "更改为双引号

php - Chrome 的 JavaScript 问题

google-chrome - Chrome/WebKit 在使用背景、边框半径和顶部/底部边框时创建实心条

javascript - 如何在 Vue.js 中传递 $router.push 中的数组?

firefox - 更改 HTTP 请求 header (用户代理)- Chrome/Firefox

javascript - 以编程方式更改 google chrome 默认打印机

css - 使用 CSS3 列时如何摆脱 WebKit 添加的填充?

javascript - 将 console.log 分配给另一个对象(Webkit 问题)