检测所有浏览器控制台是否打开 [英] Detect all browser console open or not

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

问题描述

我正在尝试创建一个脚本,当任何浏览器控制台打开和关闭时运行。所以有什么办法可以通过javascript,jquery或任何其他客户端脚本来检测所有浏览器控制台(firefox / ie / chrome / safari)是否打开?

I'm trying to create a script which will run when any browser console open and close. so is there any way to detect all browser console (firefox/ie/chrome/safari) open or not via javascript, jquery or any other client side script?

推荐答案

这是不可能在任何官方的跨浏览器的方式,但如果偶然的假阳性是可以接受的,你可以检查 window.onresize 事件。在加载页面后调整窗口大小的用户有些不常见。如果你希望用户频繁打开控制台,意味着更少的误报百分比,它更有效。这将无法检测到用户访问页面时控制台是否已打开,或者用户在新窗口中打开控制台。

It's not possible in any official cross browser way, but if the occasional false positive is acceptable, you can check for a window.onresize event. Users resizing their windows after loading a page is somewhat uncommon. It's even more effective if you expect users will be frequently opening the console, meaning less false positives as a percentage. This will fail to detect if the console is already open when the user visits the page, or if the user opens the console in a new window.

window.onresize = function(){
    if ((window.outerHeight - window.innerHeight) > 100) {
        // console was opened (or screen was resized)
    }
}

贷记至http://stackoverflow.com/a/7809413/3774582

为了扩展这一点,如果你需要一个很低的误报容忍度,大多数窗口调整大小将触发这个事件几十次,因为它通常做为拖动动作,而打开控制台只会触发一次。如果你能检测到这一点,这种方法将变得更准确。

To expand on this, if you need a very low tolerance on false positives, most window resizes will trigger this event dozens of times because it is usually done as a drag action, while opening the console will only trigger this once. If you can detect this, the approach will become even more accurate.

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

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