javascript 如何检测 Chrome Inspect Element 是否正在运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7527442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 00:25:02  来源:igfitidea点击:

How to detect Chrome Inspect Element is running or not?

javascriptgoogle-chromeconsole

提问by Mohammed Shannaq

Is there any way to detect whether the Chrome Inspect Element window is running?

有什么办法可以检测 Chrome Inspect Element 窗口是否正在运行?

For example if the user clicks "Inspect Element" in Chrome, the window shows a Hello Worldalert.

例如,如果用户在 Chrome 中单击“检查元素”,则窗口会显示Hello World警报。

Is that possible?

那可能吗?

采纳答案by Gerben

window.onresize = function(){
 if((window.outerHeight-window.innerHeight)>100)
   alert('hello');
}

In action: http://jsbin.com/ediquk/

在行动:http: //jsbin.com/ediquk/

Note that it seems like the resize event gets fired twice, so you should check whether you alerted the use already.

请注意,似乎 resize 事件被触发了两次,因此您应该检查是否已提醒使用。

回答by Dmitry Pashkevich

UPDATEThis no longer works. The property console.profileshas been removed in Chrome 29.

更新这不再有效。该属性console.profiles已在 Chrome 29 中删除。

The only solution that's left is checking the difference between window.outerHeightand window.innerHeightas suggested by @Gerben. There is a library devtools-detectbased on this method which adds devtoolschangeto the windowobject.

剩下的唯一的解决办法是检查之间的差异window.outerHeight,并window.innerHeight通过@Gerben建议。有一个基于此方法的库devtools-detect添加devtoolschangewindow对象中。

Alternatively, there is an effort in progress to create a Chrome extension using a more robust detection method, see this Google Group.

或者,正在努力使用更强大的检测方法创建 Chrome 扩展程序,请参阅此Google Group



Here's how they check if DevTools are open in the first challenge of Discover DevToolsinteractive course:

以下是他们在Discover DevTools互动课程的第一个挑战中检查 DevTools 是否打开的方法:

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