How to re-enable the context menu in this case?

2020-08-13 06:05发布

document.addEventListener('contextmenu', function (e) {
    e.preventDefault()
    e.stopPropagation()
    e.returnValue = false
    e.cancleBubble = true
})

No way?

Edit: document.oncontextmenu = null does not work.

P.S. I cannot have the reference of the listener function since I am not the owner of the site preventing the context menu.

3条回答
淡お忘
2楼-- · 2020-08-13 06:35

Rather than disabling the context menu, why don't you assign the right click event?

http://abeautifulsite.net/2008/05/jquery-right-click-plugin/

查看更多
Animai°情兽
3楼-- · 2020-08-13 06:41

If you are really desperate, try adding this before the addEventListener is called. It works in both FF and Chrome. I didn't check anything else.

document.superListener = document.addEventListener;
document.addEventListener = function(type, listener, useCapture){
    if(type != 'contextmenu')
        document.superListener(type, listener, !!useCapture);
};

It may not be the best way to do things, but it should be the job done on your specific example :)

查看更多
Luminary・发光体
4楼-- · 2020-08-13 06:48

I use my bookmarklet in such cases:

javascript:(function(w){
    var arr = ['contextmenu','copy','cut','paste','mousedown','mouseup','beforeunload','beforeprint'];
    for(var i = 0, x; x = arr[i]; i++){
        if(w['on' + x])w['on' + x] = null;
        w.addEventListener(x, function(e){e.stopPropagation()}, true);
    };
    for(var j = 0, f; f = w.frames[j]; j++){try{arguments.callee(f)}catch(e){}}})(window);
查看更多
登录 后发表回答