如何重新启用右键,以便我可以检查 HTML 元素在 Chrome?

我正在看一个网页,它已经覆盖了右键按钮,以显示自己的弹出式 HTML 元素。

这阻止了我使用 Chrome 开发工具来检查元素。

有人知道我可以从 Chrome 控制台注入的 JavaScript 代码片段吗 重新启用右击?

我可以打破现有的“右键单击”功能,以便能够轻松地检查 HTML 元素。

145339 次浏览

Easiest thing to do is open the dev tools by pressing Cmd + Opt + I (Mac) or F12 (PC). You can then use the search (magnifying glass, top left on the dev tools toolbar) to select the element.

If they have just changed the oncontextmenu handler (which is the most straightforward way to do it), then you can remove their override thus:

window.oncontextmenu = null;

Otherwise, if it is attached to individual elements, you can get all the page's elements, and then remove the handler on each one:

var elements = document.getElementsByTagName("*");
for(var id = 0; id < elements.length; ++id) { elements[id].oncontextmenu = null; }

Or, it seems you can turn off such scripts; via an extension in Chrome or an option in Firefox - in the advanced box for javascript options, switch off 'Disable or replace context menus'.

Disabling the "SETTINGS > PRIVACY > don´t allow JavaScript" in Chrome will enable the right click function and allow the Firebug Console to work; but will also disable all the other JavaScript codes.

The right way to do this is to disable only the specific JavaScript; looking for any of the following lines of code:

  • Function disableclick
  • Function click … return false;
  • body oncontextmenu="return false;"

I just visited this site and it really bugged me,

apparently there are a couple ways to disable the mouse click:

1)

<script language="javascript">
document.onmousedown=disableclick;
status="Right Click Disabled";
function disableclick(event)
{
if(event.button==2)
{
alert(status);
return false;
}
}
</script>

and

<body oncontextmenu="return false">

in this case what you will have to do in the dev tools is :

document.removeEventListener("onmousedown",disableclick);
document.oncontextmenu = function(){}

2)

using flash as a content wrapper - no solution here except taking a screenshot

3)

some sites want to prevent downloading images via right click -> save image as

so what they do is put this:

<div style="background-image: url(YourImage.jpg);">
<img src="transparent.gif"/>
</div>

which is a transparent image spreding on the full width and height of the screen all you need to do is go to the elements inspector and find the div and delete it.

In my case #1 did the trick

Just Press F12

Go to Sources

There you will find Enable right click. Click on it.

Under this you will find web_accessible_resource.

Open it in this you will find index.js.

Press Ctrl + F and search for disabelRightClick. There you will find

        var disableRightClick = false;

this line. Replace this line with

        var disableRightClick = true;

Just press Ctrl + s

!! Done. Now your right click is enabled !!

you can use following code for re-enable mouse right click.

document.oncontextmenu = function(){}

and you can use shortcut key (Ctrl+Shift+i) to open inspect elements in chrome in windows OS.

Another possible way, when the blocking function is made with jquery, use:

$(document).unbind();

It will clear all the onmousedown and contextmenu events attributed dynamically, that can't be erased with document.contextmenu=null; etc.

You could use javascript:void(document.oncontextmenu=null); open Browser console and run the code above. It will turn off blockin' of mouse right button

The way I solved this was delete the event listeners on the page. After I did that, I was able to copy the text and paste it in to my processor of choice.

Tested in Chrome 60.0.3112.78.

Some of the above methods work, but the easiest in my opinion is:

  1. Open dev tools (Shift+Control+i).

  2. Select the "Elements" tab, and then the "Event Listeners" tab.

  3. Hover over the elements/listener. A "Remove" button will show up.

  4. Click "Remove".

E.g. see photo.

Remove event listener

The easiest way I found is to open the webpage in Read mode (browser that supports read mode like Safari, Firefox etc) and then copy as usual

Hi i have a shorter version. this does same as a best answer. (it works on chrome 74.03)

document.querySelectorAll('*').forEach(e => e.oncontextmenu = null)

This bookmarlet works in Google sites/Youtube as of Aug 2019 (tested in Chrome and Firefox):

function enableContextMenu(aggressive = false) {
void(document.ondragstart=null);
void(document.onselectstart=null);
void(document.onclick=null);
void(document.onmousedown=null);
void(document.onmouseup=null);
void(document.body.oncontextmenu=null);
enableRightClickLight(document);
if (aggressive) {
enableRightClick(document);
removeContextMenuOnAll("body");
removeContextMenuOnAll("img");
removeContextMenuOnAll("td");
} }


function removeContextMenuOnAll(tagName) {
var elements = document.getElementsByTagName(tagName);
for (var i = 0; i < elements.length; i++) {
enableRightClick(elements[i]);
}
}


function enableRightClickLight(el) {
el || (el = document);
el.addEventListener("contextmenu", bringBackDefault, true);
}


function enableRightClick(el) {
el || (el = document);
el.addEventListener("contextmenu", bringBackDefault, true);
el.addEventListener("dragstart", bringBackDefault, true);
el.addEventListener("selectstart", bringBackDefault, true);
el.addEventListener("click", bringBackDefault, true);
el.addEventListener("mousedown", bringBackDefault, true);
el.addEventListener("mouseup", bringBackDefault, true);
}


function restoreRightClick(el) {
el || (el = document);
el.removeEventListener("contextmenu", bringBackDefault, true);
el.removeEventListener("dragstart", bringBackDefault, true);
el.removeEventListener("selectstart", bringBackDefault, true);
el.removeEventListener("click", bringBackDefault, true);
el.removeEventListener("mousedown", bringBackDefault, true);
el.removeEventListener("mouseup", bringBackDefault, true);
}
function bringBackDefault(event) {
event.returnValue = true;
(typeof event.stopPropagation === 'function') &&
event.stopPropagation();
(typeof event.cancelBubble === 'function') &&
event.cancelBubble();
}


enableContextMenu();

For peskier sites, set/pass aggressive to true (this will disable most event handlers and hence disable interaction with the page):

function enableContextMenu(aggressive = true) {
void(document.ondragstart=null);
void(document.onselectstart=null);
void(document.onclick=null);
void(document.onmousedown=null);
void(document.onmouseup=null);
void(document.body.oncontextmenu=null);
enableRightClickLight(document);
if (aggressive) {
enableRightClick(document);
removeContextMenuOnAll("body");
removeContextMenuOnAll("img");
removeContextMenuOnAll("td");
}
}


function removeContextMenuOnAll(tagName) {
var elements = document.getElementsByTagName(tagName);
for (var i = 0; i < elements.length; i++) { enableRightClick(elements[i]);
}
}


function enableRightClickLight(el) {
el || (el = document);
el.addEventListener("contextmenu", bringBackDefault, true);
}


function enableRightClick(el) {
el || (el = document);
el.addEventListener("contextmenu", bringBackDefault, true);
el.addEventListener("dragstart", bringBackDefault, true);
el.addEventListener("selectstart", bringBackDefault, true);
el.addEventListener("click", bringBackDefault, true);
el.addEventListener("mousedown", bringBackDefault, true);
el.addEventListener("mouseup", bringBackDefault, true);
}


function restoreRightClick(el) {
el || (el = document);
el.removeEventListener("contextmenu", bringBackDefault, true);
el.removeEventListener("dragstart", bringBackDefault, true);
el.removeEventListener("selectstart", bringBackDefault, true);
el.removeEventListener("click", bringBackDefault, true);
el.removeEventListener("mousedown", bringBackDefault, true);
el.removeEventListener("mouseup", bringBackDefault, true);
}


function bringBackDefault(event) {
event.returnValue = true;
(typeof event.stopPropagation === 'function') &&
event.stopPropagation();
(typeof event.cancelBubble === 'function') &&
event.cancelBubble();
}


enableContextMenu();

I built upon @Chema solution and added resetting pointer-events and user-select. If they are set to none for an image, right-clicking it does not invoke the context menu for the image with options to view or save it.

javascript:function enableContextMenu(aggressive = true) { void(document.ondragstart=null); void(document.onselectstart=null); void(document.onclick=null); void(document.onmousedown=null); void(document.onmouseup=null); void(document.body.oncontextmenu=null); enableRightClickLight(document); if (aggressive) { enableRightClick(document); removeContextMenuOnAll('body'); removeContextMenuOnAll('img'); removeContextMenuOnAll('td'); } } function removeContextMenuOnAll(tagName) { var elements = document.getElementsByTagName(tagName); for (var i = 0; i < elements.length; i++) { enableRightClick(elements[i]); enablePointerEvents(elements[i]); } } function enableRightClickLight(el) { el || (el = document); el.addEventListener('contextmenu', bringBackDefault, true); } function enableRightClick(el) { el || (el = document); el.addEventListener('contextmenu', bringBackDefault, true); el.addEventListener('dragstart', bringBackDefault, true); el.addEventListener('selectstart', bringBackDefault, true); el.addEventListener('click', bringBackDefault, true); el.addEventListener('mousedown', bringBackDefault, true); el.addEventListener('mouseup', bringBackDefault, true); } function restoreRightClick(el) { el || (el = document); el.removeEventListener('contextmenu', bringBackDefault, true); el.removeEventListener('dragstart', bringBackDefault, true); el.removeEventListener('selectstart', bringBackDefault, true); el.removeEventListener('click', bringBackDefault, true); el.removeEventListener('mousedown', bringBackDefault, true); el.removeEventListener('mouseup', bringBackDefault, true); } function bringBackDefault(event) { event.returnValue = true; (typeof event.stopPropagation === 'function') && event.stopPropagation(); (typeof event.cancelBubble === 'function') && event.cancelBubble(); } function enablePointerEvents(el) {  if (!el) return; el.style.pointerEvents='auto'; el.style.webkitTouchCallout='default'; el.style.webkitUserSelect='auto'; el.style.MozUserSelect='auto'; el.style.msUserSelect='auto'; el.style.userSelect='auto'; enablePointerEvents(el.parentElement); } enableContextMenu();

If the page you are on has a text or textarea input, click into this input (as if you want to enter text) then right-click and select 'Inspect element'.

On the very left of the Chrome Developer Tools toolbar there is a button that lets you select an item to inspect regardless of context menu handlers. It looks like a square with arrow pointing to the center.

Open inspect mode before navigating to the page. It worked.hehe

If none of the other comments works, just do, open console line command and type:

document.oncontextmenu = null;

Not quite answering the question, but you can use ctrl/cmd + shift + c to start an "inspect on hover" mode in chrome.

Press Ctrl+Control+F12
Then select Elements > Event Listeners > Remove contextmenu events