How to find out if your browser console is open?

Asked

Viewed 381 times

1

Is there any way to capture the information that the browser console is open? Currently I make a script that checks if the height of the browser has been changed... but in case it dislodges 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 deleted...

2 answers

2


According to that answer: Find out whether Chrome console is open. One of the ways to do it is the one you are using. The other is the following:

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.

  • cool, there is a way to close the console.log?

  • I believe this is not possible because you would be interfering in the way the browser works.

  • Does this work for other browsers? The question you linked mentions Chrome.

1

Complementing the @Julianonunes response, you can create a function for this, as the variable will continue with the value true if the user opens the console and then closes it. Thus, you can run it whenever 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 is also worth remembering that if the function console.log() is superscripted, 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 seems impossible, after all it would be a security failure of the browsers to let the developers perform this process. Formerly could block access to it, a feature that Facebook had created, but is already outdated.

  • 1

    thanks for the answer, but I didn’t need much of it, I called the method I needed within the devtools.toString = function() { ... }.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.