Git Product home page Git Product logo

detect-devtools-via-debugger-heartstop's Introduction

๐Ÿ’ž๐Ÿ› 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

detect-devtools-via-debugger-heartstop's People

Contributors

david-fong avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

detect-devtools-via-debugger-heartstop's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.