-3

I wrote this script a while back that's worked better for me than any other right-click-enabling extension.

function rightClickEvent(event) {
  event.stopPropagation();
  return true;
}

function enableRightClick(elements) {
  if (elements.length === 0) return;
  Array.prototype.slice.call(elements, 0).forEach(element => {
    element.addEventListener('contextmenu', rightClickEvent, true);
    enableRightClick(element.children);
  });
}

enableRightClick(document.children);

It seems like overkill because it adds an action listener to every single element on the whole page. Is there any way to achieve similar results, like a way to intercept every event for oncontextmenu, without having to traverse the DOM of the entire page?

To clarify, this is for ENABLING right click in environments where a simple change of contextmenu on the button doesn't work.

m0meni
  • 16,006
  • 16
  • 82
  • 141
  • Do you mean *dis*able? – Matt Burland Dec 10 '15 at 21:54
  • Possible duplicate of [disable right click in javascript](http://stackoverflow.com/questions/30965212/disable-right-click-in-javascript) – Matt Burland Dec 10 '15 at 21:55
  • @MattBurland no I mean enable. There's several pages I visit frequently that have right clicking disabled, and I use this (without the fat arrow) to enable it. – m0meni Dec 10 '15 at 21:55
  • Can someone explain all the downvotes? – m0meni Dec 10 '15 at 21:56
  • 1
    @MattBurland - the OP is trying to ***enable*** right clicking, not disable it - therefore it's hard to see how this could be a dup of your reference post which is about disabling? – jfriend00 Dec 10 '15 at 22:23
  • 2
    How to work around a page that disables right clicking depends entirely upon how the page implements the disable. If you want a method that handles the most possible ways it might disable it, then you HAVE to handle it at the leaf nodes and on every node like you are doing - there is no other way. Any simpler option that uses event propagation to handle the event at a higher level may be blocked by the JS in the page. – jfriend00 Dec 10 '15 at 22:26
  • 1
    FYI, it might be simpler to just use `document.getElementsByTagName("*")` to get a nodelist of all elements in the document. No particular need to walk the hierarchy recursively yourself. And, for completeness, you may have to monitor for DOM elements inserted into the page dynamically after you hook up. – jfriend00 Dec 10 '15 at 22:27
  • @jfriend00 thank you! I appreciate that you actually read my question as it had been pretty upsetting seeing people downvote and misconstrue it. I'm very grateful. I know the answer was pretty simple, but could you post it so I can accept and upvote it? – m0meni Dec 10 '15 at 22:28
  • 1
    I think maybe people didn't understand your particular objective and question and that's why you got the downvotes. Some things to watch out for on this site are using any phrasing that sounds like you're asking for "best" way to do something because that usually involves "opinions" which can sometimes be off-topic here (though pretty much all good answers do have opinions in them, just not only opinions). I'm guessing you would have received a better response if you started by describing the problem you were trying to solve and why and then showed what you had so far. – jfriend00 Dec 10 '15 at 22:37
  • @jfriend00 thank you for the continued explanations and support. People like you are why StackOverflow is amazing. – m0meni Dec 10 '15 at 22:38

1 Answers1

1

I'd suggest that you just use document.getElementsByTagName("*") to fetch a nodeList of all the elements in the document rather than recursively walking the hierarchy yourself.

And, for completeness, you may have to monitor for DOM elements being inserted into the page dynamically after you hook up.

How to work around a page that disables right clicking depends entirely upon how the page implements the disable. If you want a method that handles the most possible ways it might disable it, then you HAVE to handle it at the leaf nodes and on every node like you are doing - there is no other way. Any simpler option that uses event propagation to handle the event at a higher level may be blocked by the JS in the page so if you're trying to avoid being blocked by that, then you have to handle it at the leaf node.

jfriend00
  • 683,504
  • 96
  • 985
  • 979