Skip to content

david-fong/detect-devtools-via-debugger-heartstop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

💞🐛 Detect Devtools Via Debugger Heartstop

npm version

Detects whether the browser's devtools are open. (demo)

How It Works

  1. Main thread sends a message to a webworker thread.
  2. Worker thread replies with an opening heartbeat.
  3. Main thread reacts by starting a timer to expect the closing heartbeat.
  4. Worker thread's message handler encounters a debugger statement.
  5. If devtools are closed, the worker will immediately send an acknowledgement to the main thread, and the main thread will conclude that devtools are closed.
  6. If devtools are opened, the worker will enter a debugging session, and the main thread will notice that the Worker has not responded sufficiently quickly, concluding that the debugger must be open. The main thread will not be blocked by the worker's debugging session, but it's timeout response will be blocked by any heavy processing in the main thread ahead of it in the event queue.

It assumes that the browser always enters debugging when devtools are open for any thread that encounters a debugging statement. Please read on to find out about browser support.

This was a fun challenge to tackle. If this solution sounds overly complex, take a look through the listed alternatives. It's a pretty fascinating rabbit hole.

Pros and Cons

Pros

This is well suited for devs who want to do silly/weird things to users such as rickrolling people who open devtools in a browser game, and don't mind absolutely destroying the usability/ergonomics of the devtools. In fact, this was the very kind of spirit for which I created this.

It doesn't depend on whether the devtools pane is attached to the browser window, or some other browser-internal behaviours such as lazy console logging of complex objects, which are not part of any web spec.

Though the design involves timing program execution, it is written such that the detection should never trigger false positives due to busy threads, given a reasonable main thread timeout value.

Cons

Like all solutions involving the debugger statement, the devtools user can bypass it simply by disabling breakpoints.

On FireFox, this only works when the debugger is the active tab. Chrome (tested for v92) always enters debugging no matter what the active devtools tab is.

🚨 To devs who want some custom browser hooks for their own purposes, this is not for you. You will hate it. It will enter debugging for the worker thread whenever devtools are opened, which (in most browsers) also causes the console context to change to the worker's context. Simply continuing the debugger will result in the debugger activating again, and the only way around this is to use the browser's inbuilt mechanism to disable all breakpoints (which may need to be done each time opening the devtools depending on whether your browser remembers the setting). Scroll down for links to alternatives.

It can get messed up when certain debugger statements are placed in the main thread. I have not yet tested out what the rules for this are, nor am I really interested in doing so 😅.

Usage

See the typings file, or visit the demo page.

Alternatives

You may also have luck sifting through the below StackOverflow thread. For example, one simple but non-robust way to do it is to hook into keyboard shortcuts.

Some History Readings