Last active
January 29, 2024 10:04
-
-
Save oussamahamdaoui/36d2e154503aec714c04f5df1c6edef3 to your computer and use it in GitHub Desktop.
Detect devtools open and close events tested on Chrome and Safari
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
'use strict'; | |
const el = new Image(); | |
let consoleIsOpen = false; | |
let consoleOpened = false; | |
Object.defineProperty(el, 'id', { | |
get: () => { | |
consoleIsOpen = true; | |
} | |
}); | |
const verify = () => { | |
console.dir(el); | |
if (consoleIsOpen === false && consoleOpened === true) { | |
// console closed | |
window.dispatchEvent(new Event('devtools-opened')); | |
consoleOpened = false; | |
} else if (consoleIsOpen === true && consoleOpened === false) { | |
// console opened | |
window.dispatchEvent(new Event('devtools-closed')); | |
consoleOpened = true; | |
} | |
consoleIsOpen = false; | |
setTimeout(verify, 1000); | |
} | |
verify(); | |
})(); | |
// Use like this | |
// window.addEventListener('devtools-opened', ()=>{}); | |
// window.addEventListener('devtools-closed', ()=>{}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
interesting