javascriptgoogle-chromefirebuggoogle-chrome-devtools

Find out whether Chrome console is open


I am using this little script to find out whether Firebug is open:

if (window.console && window.console.firebug) {
    //is open
};

And it works well. Now I was searching for half an hour to find a way to detect whether Google Chrome's built-in web developer console is open, but I couldn't find any hint.

This:

if (window.console && window.console.chrome) {
    //is open
};

doesn't work.

EDIT:

So it seems that it is not possible to detect whether the Chrome console is open. But there is a "hack" that works, with some drawbacks:

So, I am gonna choose Unsigned´s answer for now, but if some1 comes up with a brilliant idea, he is welcome to still answer and I change the selected answer! Thanks!


Solution

  • Leaving previous answers below for historical context.

    Debugger (2022)

    While not fool-proof, this debugger-based approach in another answer does appear to still work.

    requestAnimationFrame (Late 2019)

    Currently Muhammad Umer's approach works on Chrome 78, with the added advantage of detecting both close and open events.

    function toString (2019)

    Credit to Overcl9ck's comment on this answer. Replacing the regex /./ with an empty function object still works.

    var devtools = function() {};
    devtools.toString = function() {
      if (!this.opened) {
        alert("Opened");
      }
      this.opened = true;
    }
    
    console.log('%c', devtools);
    // devtools.opened will become true if/when the console is opened

    regex toString (2017-2018)

    Since the original asker doesn't seem to be around anymore and this is still the accepted answer, adding this solution for visibility. Credit goes to Antonin Hildebrand's comment on zswang's answer. This solution takes advantage of the fact that toString() is not called on logged objects unless the console is open.

    var devtools = /./;
    devtools.toString = function() {
      if (!this.opened) {
        alert("Opened");
      }
      this.opened = true;
    }
    
    console.log('%c', devtools);
    // devtools.opened will become true if/when the console is opened

    console.profiles (2013)

    Update: console.profiles has been removed from Chrome. This solution no longer works.

    Thanks to Paul Irish for pointing out this solution from Discover DevTools, using the profiler:

    function isInspectOpen() {
      console.profile();
      console.profileEnd();
      if (console.clear) {
        console.clear();
      }
      return console.profiles.length > 0;
    }
    function showIfInspectIsOpen() {
      alert(isInspectOpen());
    }
    <button onClick="showIfInspectIsOpen()">Is it open?</button>

    window.innerHeight (2011)

    This other option can detect the docked inspector being opened, after the page loads, but will not be able to detect an undocked inspector, or if the inspector was already open on page load. There is also some potential for false positives.

    window.onresize = function() {
      if ((window.outerHeight - window.innerHeight) > 100) {
        alert('Docked inspector was opened');
      }
    }