How do I find out if the browser console is open?

1

Is there any way to capture information that the browser console is open? Currently I make a script that checks if the height of the browser has changed ... but in case it strips the browser console, or even, modify to the side, the script does not capture.

The idea is that when the person opens the console, the content of the page will be erased ...

    
asked by anonymous 29.05.2018 / 21:57

2 answers

2

According to this answer: Find out whether Chrome console is open . One of the ways you do is the one you are using. The other is as follows:

var devtools = /./;
devtools.toString = function() {
    this.opened = true;
}

console.log('%c', devtools);
// devtools.opened vai se tornar true quando o console estiver aberto

According to him, this solution uses the fact that the toString () method is not called when you use the browser with the console closed.

    
29.05.2018 / 22:07
1

Complementing @JulianoNunes's response, you can create a function for this, because the variable will continue with the true value if the user opens the console and then closes it. So you can run it every time you check whether the Console is open or not. So you can do:

// set interval que testa se o console está aberto
setInterval(function(){
  document.body.innerText = isDevtoolsOpened();
}, 1000);

// função que faz a verificação
function isConsoleOpened() {

  let devtools = /./;
  devtools.toString = function() {
      this.opened = true;
  }

  console.log("%c", devtools);

  return devtools.opened ? true : false;

}

It should also be noted that if the console.log() function is overwritten, as in stacksnippets , this will not work, since the function can be triggered at any time, regardless of whether the Console is open or not.

Close the console does not seem possible , after all it would be a security flaw for browsers to let developers do this process. In the past could block access to it, a feature which Facebook had created, but is already out of date.

    
29.05.2018 / 22:57