Code Monkey home page Code Monkey logo

devtools-detect's Introduction

devtools-detect

Detect if DevTools is open and its orientation

Useful for when you want something special to happen when DevTools is open. Like pausing canvas, adding style debug info, etc.

This package has a lot of flaws. It used to work better, but browsers changed, and the detection now has too many false-positives.

Install

npm install devtools-detect

Usage

<script type="module">
import devtools from './node_modules/devtools-detect/index.js';

// Check if it's open
console.log('Is DevTools open:', devtools.isOpen);

// Check it's orientation, `undefined` if not open
console.log('DevTools orientation:', devtools.orientation);

// Get notified when it's opened/closed or orientation changes
window.addEventListener('devtoolschange', event => {
	console.log('Is DevTools open:', event.detail.isOpen);
	console.log('DevTools orientation:', event.detail.orientation);
});
</script>

React usage

import {useState, useEffect} from 'react';
import devtoolsDetect from 'devtools-detect';

export function useDevToolsStatus() {
	const [isDevToolsOpen, setIsDevToolsOpen] = useState(devtoolsDetect.isOpen);

	useEffect(() => {
		const handleChange = event => {
			setIsDevToolsOpen(event.detail.isOpen);
		};

		window.addEventListener('devtoolschange', handleChange);

		return () => {
			window.removeEventListener('devtoolschange', handleChange);
		};
	}, []);

	return isDevToolsOpen;
}
import {useDevToolsStatus} from './useDevToolsStatus.js';

export default function App() {
	const isDevToolsOpen = useDevToolsStatus();
	return isDevToolsOpen ? 'OPEN' : 'CLOSED';
}

Support

  • Chrome DevTools
  • Safari DevTools
  • Firefox DevTools
  • Opera DevTools

Caveats

Doesn't work if DevTools is undocked and will show false positive if you toggle any kind of sidebar.

devtools-detect's People

Contributors

a-axton avatar artfulhacker avatar arthurvr avatar bendingbender avatar devbymak avatar egorvoronov avatar richienb avatar samyk avatar sindresorhus 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  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  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

devtools-detect's Issues

maybe, there's a better way to detect it.

RT

<script type="text/javascript">
var el = new Image();
Object.defineProperty(el, 'id', {
    get: function() {
        alert('active');
    }
});
console.log('%cHello', el);
</script>

Not working on new wnidow

Overall, it works well in the same page.
However, if the code should run on the new window, then it won't work.
And I found that if the devtool opens as the new window, the code can't feagure out whether it opened or closed.

New versions of Firebug are not detected

It seems that new versions of Firebug doesn't modify 'window' object with 'Firebug', so it's useless to check for 'window.Firebug'. It instead adds new function called 'window.console.exception' which can be used for detection.

DevTools Undocked

I there's a polyfill or an idea to fix when DevTools is undocked, it should be added to this thread.

Windows 7 Chrome not working

i check this script in windows 7 chrome browser but this could not detect
if dev tools is close still getting developer console is open
please check and revert me as soon as posible

Not working on IE12

Upon enabling developer tools, a JS error appears on the test page:

SCRIPT445: Object doesn't support this action
File: devtools-detect.js, Line: 13, Column: 3

False negative on page load

When I load the page with devtools open I get this result:

import devtoolsDetect from 'devtools-detect';

console.log(devtoolsDetect.isOpen) // false

I think its because the code if delayed by 500ms.

Could we maybe make the first run be run immediately or add an event for when the devtools detection code has actually been run?

Sites detecting devtools

How can I hide the fact that I opened dev tools?

Some site I want to check out finds it out and does stuff to prevent me from using it for example block access to the site.

This is really shit. Where can I get an anti dev tools detection?

Pure Javascript

Hi, can you convert your code in Pure Javascript plz ?

Cause i don t have node and can t install

Thank you

Zooming in chrome

Zooming in Google Chrome (using CTRL + or -) seems to make the script think that the console was opened. This does not occur in Firefox.

Does not detect dev tools in another window

I usually have devtools open in another window on another monitor, this script doesn't detect that scenario. The devtools window has to be attached to the browser.

Tested in Chrome build 32.0.1700.102

Not work for Edge.

Not work for Edge.

Edge version : 42.17134.1098.0

Error message in console:
SCRIPT5009: SCRIPT5009: 'globalThis' is not defined

[enhancement] Add missing bower.json.

Hey, maintainer(s) of sindresorhus/devtools-detect!

We at VersionEye are working hard to keep up the quality of the bower's registry.

We just finished our initial analysis of the quality of the Bower.io registry:

7530 - registered packages, 224 of them doesnt exists anymore;

We analysed 7306 existing packages and 1070 of them don't have bower.json on the master branch ( that's where a Bower client pulls a data ).

Sadly, your library sindresorhus/devtools-detect is one of them.

Can you spare 15 minutes to help us to make Bower better?

Just add a new file bower.json and change attributes.

{
  "name": "sindresorhus/devtools-detect",
  "version": "1.0.0",
  "main": "path/to/main.css",
  "description": "please add it",
  "license": "Eclipse",
  "ignore": [
    ".jshintrc",
    "**/*.txt"
  ],
  "dependencies": {
    "<dependency_name>": "<semantic_version>",
    "<dependency_name>": "<Local_folder>",
    "<dependency_name>": "<package>"
  },
  "devDependencies": {
    "<test-framework-name>": "<version>"
  }
}

Read more about bower.json on the official spefication and nodejs semver library has great examples of proper versioning.

NB! Please validate your bower.json with jsonlint before commiting your updates.

Thank you!

Timo,
twitter: @versioneye
email: [email protected]
VersionEye - no more legacy software!

Examples

Create some examples on how it can be useful.

Ideas:

  • Pause canvas when devtools is open
  • Add style info inline
  • ?

How do you import this using browserify?

After installing it using NPM, I could not do

import devtools from "devtools-detect";

or

var devtools = require('devtools-detect');

I had to install using Bower and then copy the devtools-detect.js file to /libs directory and then using browserify I could do:

import devtools from './libs/devtools-detect.js';

And then it worked.

Am I doing something wrong?

Detection bug

devtools-detect have a problem with detection, when the bookmarks bar and the downloads panel are enabled
bug

IE11 errors on new CustomEvent

On line 13 new CustomEvent(...) raises an error in IE11.

"Object doesn't support this action"

Not sure what the work around is... :-/

Fix the zoom always return true

const widthThreshold = window.outerWidth - window.innerWidth * window.devicePixelRatio > threshold
const heightThreshold = window.outerHeight - window.innerHeight * window.devicePixelRatio > threshold

//yes, just add `* window.devicePixelRatio`

Misdetect on iPhone11

Devtools-detect detects DevTools use in Mobile Safari or Chrome on iPhone 11.
But this is misdetect.Is there any solution?

Official browser API?

I'm the PM on Edge DevTools, and I was wondering if we could help this extension, by setting a property on the global object when the DevTools are opened?

The current approach seems rather hacky, and I imagine it would be easy to get implemented in most engines

interface DevTools {
    attribute Boolean isOpen;
    attribute String dockState; 
}

window.devTools.isOpen
window.devTools.dockState // 'left, right, top, bottom, inline'

Internet Explorer issue doesnt work

Hi

Doesnt seem to work in internet explorer is get this error

window.dispatchEvent(new CustomEvent('devtoolschange', {
            detail: {
                open: state
            }
        }));

Its says
Object doesnt support this action

Any suggestions?

Thanks

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.