了解Chrome控制台是否已打开 [英] Find out whether Chrome console is open

查看:290
本文介绍了了解Chrome控制台是否已打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  if(window.console&& window.console.firebug){
//打开
};

它运作良好。现在我搜索了半个小时,找到一种方法来检测Google Chrome的内置Web开发者控制台是否已打开,但我找不到任何提示。



这:

  if(window.console&&window.console.chrome){
//打开
};

无效。

编辑:

因此,似乎无法检测Chrome控制台是否处于打开状态。但是有一个破解工作,有一些缺点:$ b​​
$ b


  • 在控制台未连接时不起作用

  • 控制台在页面加载时打开



所以,我现在要选择Unsigned's的答案,但如果some1出现了辉煌的想法,他欢迎仍然回答,我改变选定的答案!

解决方案

toString(2017-2018)



由于原来的提问者似乎不再存在,这仍然是公认的答案,增加了这种解决方案的可见性。请参阅 Antonin Hildebrand zswang >评论 HREF = https://stackoverflow.com/a/30638226/629493>回答。这个解决方案利用了这样一个事实:除非控制台打开,否则 toString()不会在日志对象上调用。

  var devtools = /./; 
devtools.toString = function(){
this.opened = true;
}

console.log('%c',devtools);
// devtools.opened将在控制台打开时变为true



控制台。配置文件(2013年)



更新: console.profiles 已从Chrome中移除。此解决方案不再有效。



感谢 Paul Irish 指出这个解决方案来自发掘DevTools ,使用profiler:

 函数isInspectOpen()
{
console.profile();
console.profileEnd();
if(console.clear)console.clear();
返回console.profiles.length> 0;
}



window.innerHeight(2011)



另一个选项可以检测在页面加载之后打开的停靠检查器,但无法检测到卸载的检查器,或者检查器在页面加载时已打开。还有一些潜在的误报。

  window.onresize = function()
{
if ((window.outerHeight - window.innerHeight)> 100)
alert('Docked inspector was opened');
}


I am using this little script to find out whether Firebug is open:

if (window.console && window.console.firebug) {
    //is open
};

And it works well. Now I was searching for half an hour to find a way to detect whether Google Chrome's built-in web developer console is open, but I couldn't find any hint.

This:

if (window.console && window.console.chrome) {
    //is open
};

doesn't work.

EDIT:

So it seems that it is not possible to detect whether the Chrome console is open. But there is a "hack" that works, with some drawbacks:

  • will not work when console is undocked
  • will not work when console is open on page load

So, I am gonna choose Unsigned´s answer for now, but if some1 comes up with a brilliant idea, he is welcome to still answer and I change the selected answer! Thanks!

解决方案

toString (2017-2018)

Since the original asker doesn't seem to be around anymore and this is still the accepted answer, adding this solution for visibility. Credit goes to Antonin Hildebrand's comment on zswang's answer. This solution takes advantage of the fact that toString() is not called on logged objects unless the console is open.

var devtools = /./;
devtools.toString = function() {
  this.opened = true;
}

console.log('%c', devtools);
// devtools.opened will become true if/when the console is opened

console.profiles (2013)

Update: console.profiles has been removed from Chrome. This solution no longer works.

Thanks to Paul Irish for pointing out this solution from Discover DevTools, using the profiler:

function isInspectOpen()
{
    console.profile(); 
    console.profileEnd(); 
    if (console.clear) console.clear();
    return console.profiles.length > 0;
}

window.innerHeight (2011)

This other option can detect the docked inspector being opened, after the page loads, but will not be able to detect an undocked inspector, or if the inspector was already open on page load. There is also some potential for false positives.

window.onresize = function()
{
    if ((window.outerHeight - window.innerHeight) > 100)
        alert('Docked inspector was opened');
}

这篇关于了解Chrome控制台是否已打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆