Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
403 views
in Technique[技术] by (71.8m points)

javascript - 检测是否在所有浏览器中打开了console / devtools(Detect if console/devtools is open in all browsers)

I'm trying to create a script which will run when any browser console is opened or closed.

(我正在尝试创建一个脚本,该脚本将在打开或关闭任何浏览器控制台时运行。)

Is there any way to detect if the browser console in all browsers (Firefox/IE/Chrome/Safari/Opera) is open or not via JavaScript, jQuery, or any other client-side script?

(是否可以通过JavaScript,jQuery或任何其他客户端脚本检测所有浏览器(Firefox / IE / Chrome / Safari / Opera)中的浏览器控制台是否打开?)

  ask by Super User translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

(如果您愿意接受用户的干扰,则可以使用debugger语句 ,因为该语句所有主流浏览器中可用。)

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 .

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

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...