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
How to detect Chrome Inspect Element is running or not?
提问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 World
alert.
例如,如果用户在 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.profiles
has been removed in Chrome 29.
更新这不再有效。该属性console.profiles
已在 Chrome 29 中删除。
The only solution that's left is checking the difference between window.outerHeight
and window.innerHeight
as suggested by @Gerben. There is a library devtools-detectbased on this method which adds devtoolschange
to the window
object.
剩下的唯一的解决办法是检查之间的差异window.outerHeight
,并window.innerHeight
通过@Gerben建议。有一个基于此方法的库devtools-detect添加devtoolschange
到window
对象中。
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;
}