Javascript 是否可以删除“检查元素”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28690564/
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
Is it possible to remove "Inspect Element"?
提问by Kim Honoridez
Is it possible to remove or disable "Inspect Element" context menu in Chrome App via Javascript?
是否可以通过 Javascript 删除或禁用 Chrome 应用程序中的“检查元素”上下文菜单?
I have searched through several forums but there are no definite answer.
我已经搜索了几个论坛,但没有明确的答案。
采纳答案by Guillaume
It is possible to prevent the user from opening the context menu by right clicking like this (javascript):
可以通过像这样(javascript)右键单击来阻止用户打开上下文菜单:
document.addEventListener('contextmenu', function(e) {
e.preventDefault();
});
By listening to the contextmenuevent and preventing the default behavior which is "showing the menu", the menu won't be shown.
But the user will still be able to inspect code through the console (by pressing F12 in Chrome for example).
通过侦听contextmenu事件并阻止“显示菜单”的默认行为,将不会显示菜单。但是用户仍然可以通过控制台检查代码(例如在 Chrome 中按 F12)。
回答by ramakrishna
I have one requirement for one page. In that page I want to block the user to perform below actions,
我对一页有一个要求。在该页面中,我想阻止用户执行以下操作,
- Right Click
- F12
- Ctrl + Shift + I
- Ctrl + Shift + J
- Ctrl + Shift + C
- Ctrl + U
- 右键点击
- F12
- Ctrl + Shift + I
- Ctrl + Shift + J
- Ctrl + Shift + C
- Ctrl + U
For this I googled, finally got the below link,
为此我用谷歌搜索,终于得到了下面的链接,
http://andrewstutorials.blogspot.in/2014/03/disable-ways-to-open-inspect-element-in.html
http://andrewstutorials.blogspot.in/2014/03/disable-ways-to-open-inspect-element-in.html
I tested with Chrome browser & fire foz. It's working for my requirement.
我使用 Chrome 浏览器和 fire foz 进行了测试。它适合我的要求。
Right Click
右键点击
`<body oncontextmenu="return false;">`
Keys
钥匙
document.onkeydown = function(e) {
if(event.keyCode == 123) {
return false;
}
if(e.ctrlKey && e.shiftKey && e.keyCode == 'I'.charCodeAt(0)) {
return false;
}
if(e.ctrlKey && e.shiftKey && e.keyCode == 'C'.charCodeAt(0)) {
return false;
}
if(e.ctrlKey && e.shiftKey && e.keyCode == 'J'.charCodeAt(0)) {
return false;
}
if(e.ctrlKey && e.keyCode == 'U'.charCodeAt(0)) {
return false;
}
}
回答by childofsoong
Nope. Closest you can do is capture right clicks, and make them not open the context menu, but the savvy user will know the keyboard combos or menu options to access it anyway, defeating the point. That's a feature of the browser, so nothing you do in your page is going to defeat it (short of installing malware on their computer).
不。您可以做的最接近的事情是捕获右键单击,并使它们不打开上下文菜单,但精明的用户将知道无论如何都可以访问它的键盘组合或菜单选项,从而击败了这一点。这是浏览器的一项功能,因此您在页面中所做的任何事情都不会打败它(除非在他们的计算机上安装恶意软件)。
回答by cloakedninjas
While not an answer, Chrome certainly has a way to do this, I appear to be in an A/B test as my work account can't inspect in Gmail, meanwhile my personal account can.
虽然不是答案,但 Chrome 肯定有办法做到这一点,我似乎在进行 A/B 测试,因为我的工作帐户无法在 Gmail 中检查,而我的个人帐户可以。
I've searched for <meta>tags or HTTP headers which might be controlling this, but nothing stands out
我已经搜索了<meta>可能控制这一点的标签或 HTTP 标头,但没有任何突出之处
回答by Waruna Manjula
<script language="javascript">
document.onmousedown=disableclick;
status="Right Click Disabled";
function disableclick(event)
{
if(event.button==2)
{
alert(status);
return false;
}
}
</script>


