Is it possible to remove or disable "Inspect Element" context menu in Chrome App via Javascript?
I have searched through several forums but there are no definite answer.
Is it possible to remove or disable "Inspect Element" context menu in Chrome App via Javascript?
I have searched through several forums but there are no definite answer.
I have one requirement for one page. On that page, I want to block the user from performing the below actions,
For this, I googled and finally got the below link,
http://andrewstutorials.blogspot.in/2014/03/disable-ways-to-open-inspect-element-in.html
I tested it with Chrome & Firefox. It's working for my requirement.
Right Click
<body oncontextmenu="return false">
Keys
document.onkeydown = (e) => {
if (e.key == 123) {
e.preventDefault();
}
if (e.ctrlKey && e.shiftKey && e.key == 'I') {
e.preventDefault();
}
if (e.ctrlKey && e.shiftKey && e.key == 'C') {
e.preventDefault();
}
if (e.ctrlKey && e.shiftKey && e.key == 'J') {
e.preventDefault();
}
if (e.ctrlKey && e.key == 'U') {
e.preventDefault();
}
};
It is possible to prevent the user from opening the context menu by right clicking like this (javascript):
document.addEventListener('contextmenu', function(e) {
e.preventDefault();
});
By listening to the contextmenu
event 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).
You can't.
Everything on a web page is rendered by the browser, and they want web sites to properly work on them or their users would despise them. As a result, browsers want to expose the lower level ticks of everything to the web developers with tools like code inspectors.
You could try to prevent the user from entering the menu with a key event. Something like this:
// Disable inspect element
$(document).bind("contextmenu",function(e) {
e.preventDefault();
});
$(document).keydown(function(e){
if(e.which === 123){
return false;
}
});
But if a user want to see the code, he will do it in another way. He will just need a little longer.
Short: If you do not want people to get something in their browser, you shouldn't send it to their browser in the first place.
It is kinda possible.
Firstly, use ramakrishna's solution to block the devtools shortcut keys.
Add devtools-detect to your website. Here is a quick link for the devtools.js file from his github.
Then, finally, add the following:
if (devtools.isOpen) {
setInterval(() => {
var $all = document.querySelectorAll("*");
for (var each of $all) {
each.classList.add(`asdjaljsdliasud8ausdijaisdluasdjasildahjdsk${Math.random()}`);
}
}, 5);
}
or maybe this:
if (devtools.isOpen) {
while (true) {
console.log("access denied")
}
}
It will basically overload the DOM and make it impossible to interact with it via the devtools.
Additionally, this is a mere example; you can use much more elaborate ways to overload the DOM instead of just adding classes.
I don't think it will work flawlessly but it should be enough to offer at least some extra minor layer of "security".
"put this script before ending your body tag"
<script>
document.addEventListener('contextmenu', event=> event.preventDefault());
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 == 'J'.charCodeAt(0)){
return false;
}
if(e.ctrlKey && e.keyCode == 'U'.charCodeAt(0)){
return false;
}
}
</script>
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.
I've searched for <meta>
tags or HTTP headers which might be controlling this, but nothing stands out
Yes, there is, some of the above scripts successfully disable one but can't close the other so below's the summary of the above.
document.addEventListener('contextmenu',(e)=>{
e.preventDefault();
}
);
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;
}
}
Use this and you are all secure, now nobody can steal ur code or use js to crack open ur database!
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).
Add this to the html tag of your page
<html oncontextmenu="return false">
document.addEventListener('keydown', function() {
if (event.keyCode == 123) {
alert("You Can not Do This!");
return false;
} else if (event.ctrlKey && event.shiftKey && event.keyCode == 73) {
alert("You Can not Do This!");
return false;
} else if (event.ctrlKey && event.keyCode == 85) {
alert("You Can not Do This!");
return false;
}
}, false);
if (document.addEventListener) {
document.addEventListener('contextmenu', function(e) {
alert("You Can not Do This!");
e.preventDefault();
}, false);
} else {
document.attachEvent('oncontextmenu', function() {
alert("You Can not Do This!");
window.event.returnValue = false;
});
}
No F12 No Rightclick
Hi I use this codes. Have fun♥
well yes its possible to stop inspecting the website into the browser. as you know that there are three ways to inspect a website into the browser
i have solution for the 1 and the 2 ways here is the JavaScript code but user user 3rd way is very rare cases
// take body to change the content
const body = document.getElementsByTagName('body');
// stop keyboard shortcuts
window.addEventListener("keydown", (event) => {
if(event.ctrlKey && (event.key === "S" || event.key === "s")) {
event.preventDefault();
body[0].innerHTML = "sorry, you can't do this "
}
if(event.ctrlKey && (event.key === "C")) {
event.preventDefault();
body[0].innerHTML = "sorry, you can't do this "
}
if(event.ctrlKey && (event.key === "E" || event.key === "e")) {
event.preventDefault();
body[0].innerHTML = "sorry, you can't do this "
}
if(event.ctrlKey && (event.key === "I" || event.key === "i")) {
event.preventDefault();
body[0].innerHTML = "sorry, you can't do this ";
}
if(event.ctrlKey && (event.key === "K" || event.key === "k")) {
event.preventDefault();
body[0].innerHTML = "sorry, you can't do this ";
}
if(event.ctrlKey && (event.key === "U" || event.key === "u")) {
event.preventDefault();
body[0].innerHTML = "sorry, you can't do this ";
}
});
// stop right click
document.addEventListener('contextmenu', function(e) {
e.preventDefault();
});
<script language="javascript">
document.onmousedown=disableclick;
status="Right Click Disabled";
function disableclick(event)
{
if(event.button==2)
{
alert(status);
return false;
}
}
</script>
In case it helps anyone, Inspect Element can be disabled for an individual element by adding the style pointer-events: none;
to it.
This is very useful if you have any elements that are purely for the purpose of aligning child elements that necessarily overlap a large area of more useful elements; it lets you prevent the aligner elements from responding to the Inspect Element command.
You can not block it, but you can stop some keys:
Add this to your script:
<script>
document.addEventListener('contextmenu', function(e) {
e.preventDefault();
});
document.addEventListener('keydown', function(e) {
if (event.keyCode == 123) {
return false;
}
if (e.ctrlKey && e.shiftKey) {
return false;
}
if (event.ctrlKey && event.keyCode == 85) {
return false;
}
});
/* other script code */
</script>
Above Answer Is Perfect but has some issue when pression ctrl+shift+I. in previous answer it still opens inspect. By Adding event.preventdefault() solve this problem
document.addEventListener('keydown', function() {
if (event.keyCode == 123) {
alert("You Can not Do This!");
return false;
} else if (event.ctrlKey && event.shiftKey && event.keyCode == 73) {
alert("You Can not Do This!");
event.preventDefault();
return false;
} else if (event.ctrlKey && event.keyCode == 85) {
alert("You Can not Do This!");
return false;
}
}, false);
if (document.addEventListener) {
document.addEventListener('contextmenu', function(e) {
alert("You Can not Do This!");
e.preventDefault();
}, false);
} else {
document.attachEvent('oncontextmenu', function() {
alert("You Can not Do This!");
window.event.returnValue = false;
});
}
Try this:
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 == 'J'.charCodeAt(0)){
return false;
}
if(e.ctrlKey && e.keyCode == 'U'.charCodeAt(0)){
return false;
}
if(e.ctrlKey && e.keyCode == 'C'.charCodeAt(0)){
return false;
}
if(e.ctrlKey && e.keyCode == 'X'.charCodeAt(0)){
return false;
}
if(e.ctrlKey && e.keyCode == 'Y'.charCodeAt(0)){
return false;
}
if(e.ctrlKey && e.keyCode == 'Z'.charCodeAt(0)){
return false;
}
if(e.ctrlKey && e.keyCode == 'V'.charCodeAt(0)){
return false;
}
if (e.keyCode == 67 && e.shiftKey && (e.ctrlKey || e.metaKey)){
return false;
}
if (e.keyCode == 'J'.charCodeAt(0) && e.altKey && (e.ctrlKey || e.metaKey)){
return false;
}
if (e.keyCode == 'I'.charCodeAt(0) && e.altKey && (e.ctrlKey || e.metaKey)){
return false;
}
if ((e.keyCode == 'V'.charCodeAt(0) && e.metaKey) || (e.metaKey && e.altKey)){
return false;
}
if (e.ctrlKey && e.shiftKey && e.keyCode == 'C'.charCodeAt(0)){
return false;
}
if(e.ctrlKey && e.keyCode == 'S'.charCodeAt(0)){
return false;
}
if(e.ctrlKey && e.keyCode == 'H'.charCodeAt(0)){
return false;
}
if(e.ctrlKey && e.keyCode == 'A'.charCodeAt(0)){
return false;
}
if(e.ctrlKey && e.keyCode == 'F'.charCodeAt(0)){
return false;
}
if(e.ctrlKey && e.keyCode == 'E'.charCodeAt(0)){
return false;
}
}
if (document.addEventListener) {
document.addEventListener('contextmenu', function(e) {
e.preventDefault();
}, false);
}else{
document.attachEvent('oncontextmenu', function() {
window.event.returnValue = false;
});
}
in nextJs you can put this code in the _app.tsx
or _app.js
file and use e.key instead of e.keyCode as it is deprecated
useEffect(() => {
typeof window !== undefined &&
window.document.addEventListener("contextmenu", (e) => {
e.preventDefault();
});
}, []);
document.onkeydown = function(e) {
console.log(e.key)
if(e.key === 'F12') {
return false;
}
if(e.ctrlKey && e.shiftKey && e.key === 'I') {
return false;
}
if(e.ctrlKey && e.shiftKey && e.key === 'C') {
return false;
}
if(e.ctrlKey && e.shiftKey && e.key === 'J') {
return false;
}
if(e.ctrlKey && e.key === 'u') {
return false;
}
}