How to remove contextmenu event without using getEventListeners() in bookmarklet?
This question was
migrated from Super User because it can be answered on Stack Overflow.
Migrated yesterday .
I've got a piece of code which successfully removes the block on right-clicking
I need to make it work in Chrome only so getEventListeners() is fine
I can't install my own extensions
I have code which works in the Console, but it doesn't work in a bookmarklet, telling me that getEventListeners() is not defined
Is there any way around this? Clearly a bookmarklet runs with a slightly different Javascript engine from the one in the Console.
Top Answer/Comment:
getEventListeners() is a DevTools-only function . It isn't available to normal page JavaScript, so a bookmarklet can not use it.
If the goal is just to re-enable right click you can usually bypass the site's handler instead.
document.oncontextmenu = null;
window.addEventListener(
"contextmenu",
event => event.stopImmediatePropagation(),
true
);
or as a bookmarklet:
javascript:(()=>{document.oncontextmenu=null;window.addEventListener("contextmenu",event=>event.stopImmediatePropagation(),true)})()
That should work on most sites. There is no reliable bookmarklet API for listing or removing every listener added with addEventListener(). You would need DevTools or an extension for that.
상단 광고의 [X] 버튼을 누르면 내용이 보입니다