npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

detect-devtools-via-debugger-heartstop

v2.0.0

Published

Detect whether browser devtools are open

Downloads

139

Readme

💞🐛 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 and waits for a reply.
  2. Worker thread's message handler encounters a debugger statement.
  3. If devtools are closed, the worker will immediately send an acknowledgement to the main thread, and the main thread will do a sanity check that the timestamp of the acknowledgement is recent enough that devtools were probably closed.
  4. If devtools are opened, the worker will enter a debugging session, and the main thread will timeout waiting for the response, 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.

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

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.

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.

This has the benefit over other implementations that it doesn't depend on whether the devtools pane is attached to the browser window, or other deeply browser-internal behaviours such as lazy console logging of complex objects, which are much more subject to change.

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.

Usage

See the typings file, or visit the demo page.

Alternatives

  • https://github.com/dsa28s/detect-browser-devtools - uses somewhat specific browser behaviours
  • https://github.com/sindresorhus/devtools-detect - compares the page dimensions to the window dimensions

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

  • https://stackoverflow.com/questions/7798748/find-out-whether-chrome-console-is-open
  • https://bugs.chromium.org/p/chromium/issues/detail?id=672625