1

Is it possible to detect if browser's inspector window is open?

We tried to detect it by comparing the window.outerHeight and window.innerHeight, but this does not work when inspector window is not attached to browser (is floating).

window.outerHeight - window.innerHeight > 100

Thanks, Khachatur

Khachatur
  • 921
  • 1
  • 12
  • 30
  • Can you not just detach the inspector from that window? – Dogoku Nov 01 '13 at 08:57
  • 3
    Why do you need to know? If it is to allow the console statement in IE8, just do `window.console && console.log("whatever")` each time – mplungjan Nov 01 '13 at 08:57
  • @Dogoku — The question is asking how to detect when that is the case. – Quentin Nov 01 '13 at 08:59
  • See this question http://stackoverflow.com/questions/7798748/find-out-whether-chrome-console-is-open – Dogoku Nov 01 '13 at 09:17
  • We send JS crash reports to our server for feature analyses. But we needn't do this if inspector is opened (a developer is working on this). – Khachatur Nov 01 '13 at 09:35
  • 1
    Have the developer add &debug=1 to the URL and test it where you send – mplungjan Nov 01 '13 at 09:37
  • 1
    *Anyone* can open the inspector, not just your developers. @mplungjan's suggestion is a much better solution to your problem. – Spudley Nov 01 '13 at 10:06
  • sure, everyone can open inspector. But the product is in dev stage for now, and only our developers and our testers are working on it. – Khachatur Nov 01 '13 at 10:10
  • 1
    @Khachatur In reply to your comment to a deleted comment: Yes, you can detect whether the devtools is open using the [`chrome.devtools`](https://developer.chrome.com/extensions/devtools.html) extension API, and notify the page. – Rob W Nov 01 '13 at 11:30

1 Answers1

3

I'm looking for a more clear way to do it but here is one hacky way i'm using currently:

Normally the time spent between two new Date() calls is less than 100ms. So if you put debugger between them, user will at least spend more than 100ms there and we'll know that they opened the console.

Here is a simple implementation:

function isConsoleOpen() {
  var startTime = new Date();
  debugger;
  var endTime = new Date();

  return endTime - startTime > 100;
}

$(function() {
  $(window).resize(function() {
    if(isConsoleOpen()) {
        // do something
    }
  });
});
Yasin Uslu
  • 546
  • 6
  • 17