I have this website where i take notes from. Currently i have to manually type all my notes. They have disabled the right context menu. I have added a chrome extension that can run javascript on the page, but i am not able to override their function that overrides the context menu. Here is what they have on the page.
JavaScript
x
22
22
1
$(document)[0].oncontextmenu = function() { return false; }
2
$(document).mousedown(function(e){
3
if( e.button == 2 ){
4
alert('Sorry, right click is disabled! Please consider typing');
5
return false;
6
}else if(e.button == 27){
7
exitFullscreen();
8
}else{
9
return true;
10
}
11
});
12
13
$('body').bind('cut copy paste', function (e) {
14
e.preventDefault();
15
alert('Sorry, consider typing');
16
});
17
18
$("body").on("contextmenu",function(e){
19
alert('Sorry, consider typing');
20
return false;
21
});
22
How can i disable it using some simple javascript. I have tried to override the context menu and set it to null, but it does not seem to be working
Advertisement
Answer
You can use jQuery off() and a new function for document.oncontextmenu
that returns true instead of false
I’ve wrapped these in a button click handler just to show the original code blocking the context menu and keyboard copy and being reset when you click on the button
JavaScript
1
6
1
$('#reset-btn').click(() => {
2
// code to use in your extension
3
$('body').off('contextmenu cut copy paste')
4
document.oncontextmenu = () => true;
5
$(document).off('mousedown');
6
});
JavaScript
1
32
32
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
<div style="height:2000px; background:yellow">
3
<button id="reset-btn">Reset Context Menu</button>
4
<p>Some text to copy</p>
5
</div>
6
7
<script>
8
/******* Original site code *****/
9
$(document)[0].oncontextmenu = function() {
10
return false;
11
}
12
$(document).mousedown(function(e) {
13
if (e.button == 2) {
14
alert('Sorry, right click is disabled! Please consider typing');
15
return false;
16
} else if (e.button == 27) {
17
exitFullscreen();
18
} else {
19
return true;
20
}
21
});
22
23
$('body').bind('cut copy paste', function(e) {
24
e.preventDefault();
25
alert('Sorry, consider typing');
26
});
27
28
$("body").on("contextmenu", function(e) {
29
alert('Sorry, consider typing');
30
return false;
31
});
32
</script>