Last active
November 25, 2022 15:58
-
-
Save ranaroussi/2079b286e348eed100cf73a3eb39e74d to your computer and use it in GitHub Desktop.
Detect if DevTools is open and its orientation
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> | |
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover"> | |
<script src="devtools.js"></script> | |
<script> | |
devTools.init((o) => { | |
if (o.open) { | |
alert('devtools opened'); | |
} else { | |
alert('devtools closed'); | |
} | |
}); | |
</head> | |
<body> | |
</body> | |
</html> |
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
const devTools = { | |
wasOpen: null, | |
open: false, | |
orientation: undefined, | |
check(cb) { | |
const w = window.outerWidth - window.innerWidth > 160; | |
const h = window.outerHeight - window.innerHeight > 160; | |
const firebug = (window.Firebug && window.Firebug.chrome && window.Firebug.chrome.isInitialized); | |
if ([firebug, w, h].some(x => x)) { | |
this.open = true; | |
this.orientation = w ? 'vertical' : 'horizontal'; | |
} else { | |
this.open = false; | |
this.orientation = undefined; | |
} | |
if (this.wasOpen !== this.open) { | |
cb({ | |
open: this.open, | |
orientation: this.orientation, | |
}); | |
} | |
this.wasOpen = this.open; | |
}, | |
init(cb, interval = 500) { | |
setInterval(() => { | |
this.check(cb); | |
}, interval); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment