requestAnimationframe(2019年末)
这些先前的答案留在这里作为历史背景。目前,穆罕默德·乌默(Muhammad
Umer)的方法可在Chrome
78上运行,并具有检测关闭事件和打开事件的附加优势。
函数toString(2019)
var devtools = function(){};devtools.toString = function() { this.opened = true;}console.log('%c', devtools);// devtools.opened will become true if/when the console is opened正则表达式toString(2017-2018)
由于原始的问号者似乎不再存在,并且仍然是公认的答案,因此添加了此解决方案以提高可见性。该解决方案利用了以下事实:
toString()除非打开控制台,否则不会在已记录的对象上调用该事实。
var devtools = /./;devtools.toString = function() { this.opened = true;}console.log('%c', devtools);// devtools.opened will become true if/when the console is openedconsole.profiles(2013)
更新:
console.profiles已从Chrome中删除。该解决方案不再起作用。
使用探查器从DiscoverDevTools指出了该解决方案:
function isInspectOpen(){ console.profile(); console.profileEnd(); if (console.clear) console.clear(); return console.profiles.length > 0;}window.innerHeight(2011)
此其他选项可以在页面加载后检测到停靠的检查器,但无法检测到未停靠的检查器,或者无法在页面加载时检查器已经打开。误报也有可能。
window.onresize = function(){ if ((window.outerHeight - window.innerHeight) > 100) alert('Docked inspector was opened');}


