Last active
May 6, 2019 05:57
-
-
Save kevgathuku/b6fcc0bf1d5e478f4ed6876b0cc01c0c to your computer and use it in GitHub Desktop.
Detect if Chrome Devtools is open (Doesn't work on Firefox)
This file contains hidden or 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
// https://stackoverflow.com/a/30638226 | |
// When printing an “Element” Chrome developer tools will request the 'id' property | |
// If the 'id' property is fetched, then the devtools is open | |
let checkStatus; | |
let customElement = document.createElement('p'); | |
document.body.appendChild(customToString); | |
let element = new Image(); | |
Object.defineProperty(element, 'id', { | |
get: () => { | |
checkStatus = 'on'; | |
customElement.textContent = checkStatus; | |
}, | |
}); | |
// Disadvantage: Called too many times :( | |
requestAnimationFrame(function loop() { | |
checkStatus = 'off'; | |
console.log('%c', element); | |
customElement.textContent = checkStatus; | |
requestAnimationFrame(loop); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment