Detect if DevTools is open
Find a file
2025-08-26 22:22:07 +08:00
.vscode fix: #74 2024-08-13 09:52:43 +08:00
example fix: #74 2024-08-13 09:52:43 +08:00
src fix: enable elementIdChecker in the default checkers list 2025-08-26 22:22:05 +08:00
.editorconfig finish 2018-01-25 19:13:01 +08:00
.gitignore fix: CSP exception 2024-07-12 15:04:01 +08:00
.prettierrc finish 2018-01-25 19:13:01 +08:00
HOW_IT_WORKS.md docs: update devtoolsFormatters link and clarify user activation requirements in HOW_IT_WORKS.md 2025-05-04 13:16:28 +08:00
LICENSE Create LICENSE 2020-12-28 14:31:27 +08:00
package.json 2.0.25 2025-08-26 22:22:07 +08:00
pnpm-lock.yaml fix: not compatible with brave browser 2024-06-10 21:57:49 +08:00
README.MD docs: reorder "How it works" section in README.MD for better visibility 2025-05-03 20:14:57 +08:00
tsconfig.json fix: type files emit error 2019-01-25 23:23:47 +08:00
tslint.json refactor: 解耦各个浏览器开发工具检查器和整个检测器的关系 2019-01-23 16:10:52 +08:00
webpack.config.js 修复: 页面缩放时导致 devtools docked 检查错误 2018-03-31 03:40:36 +08:00

devtools-detector devtools-detector

Installation

npm install devtools-detector --save

Usage

DEMO

ES6 & TypeScript

import { addListener, launch } from 'devtools-detector';

const view = document.createElement('div');
document.body.appendChild(view);

// 1. Add listener
addListener((isOpen) => {
  view.innerText = isOpen ? 'DevTools status: open' : 'DevTools status: closed';
});

// 2. Launch detection
launch();

AMD

require(['devtools-detector'], function (devtoolsDetector) {
  const view = document.createElement('div');
  document.body.appendChild(view);

  devtoolsDetector.addListener(function (isOpen) {
    view.innerText = isOpen
      ? 'DevTools status: open'
      : 'DevTools status: closed';
  });
  devtoolsDetector.launch();
});

No Module System

<script src="node_modules/devtools-detector/lib/devtools-detector.js"></script>
<script>
  const view = document.createElement('div');
  document.body.appendChild(view);

  devtoolsDetector.addListener(function (isOpen) {
    view.innerText = isOpen
      ? 'DevTools status: open'
      : 'DevTools status: closed';
  });
  devtoolsDetector.launch();
</script>

Browser Support

  • IE9+ (requires Promise polyfill)
  • Edge
  • Chrome
  • Firefox
  • Safari
  • Opera

Types & API

DevtoolsDetail

interface DevtoolsDetail {
  isOpen: boolean;
  checkerName: string;
}

Listener

type DevtoolsDetectorListener = (
  isOpen: boolean,
  detail?: DevtoolsDetail
) => void;

Methods

  • launch(): Start detection

  • isLaunch(): Returns true if detection is active, false otherwise

  • stop(): Stop detection

  • addListener(listener: DevtoolsDetectorListener): Add a listener

  • removeListener(listener: DevtoolsDetectorListener): Remove a listener

  • setDetectDelay(value: number): Set detection loop delay time. If value <= 0, detection stops.

  • crashBrowserCurrentTab(): Crash the current browser tab (tested only on Chrome)

    // Example: crash the current browser tab 2 seconds after DevTools is opened
    import { addListener, crashBrowserCurrentTab } from 'devtools-detector';
    
    addListener(function (isOpen) {
      if (isOpen) {
        setTimeout(crashBrowserCurrentTab, 2000);
      }
    });
    
  • crashBrowser(): Crash all browser tabs (tested only on Chrome)

How it works

How it works

Caveats

  1. In Firefox, if DevTools is undocked, it's only detected when switching to the Console Panel.
  2. Ensure that devtools-detector is loaded before other scripts.

References

License

MIT © AEPKILL