3

This question was already asked in a simple way here: Trigger right click using pure JavaScript, however, none of the answers there is what I (and apparently the poster of that thread) seek.


First and foremost, this question is not about events. I am not interested in knowing how to listen to a right click event (that would be a 'contextemenu' event). This is about simulating a right click on any DOM element, just by using the browsers console (chrome, in my case), and therefore JavaScript/DOM manipulation.


That been said, I know that there is a simple function to do the same thing, except it triggers a left ("normal") click, on any element. For instance, the famous...

I'm not a robot captcha

I found out that if you look at the source code, and somehow get a reference to the checkbox element, or the "div" where it is contained, lets say you get the ID, so you create a variable like this...

myElement = document.getElementById('Id_here');

you can simulate a click just by typing this into the browsers console:

myElement.click();

And the result will be:

captcha solved!

So, we simulated a real click, solving the captcha, no mouse needed, and more important, no human needed (if you implement this on a script)


The question now is pretty simple...

Is there anything like i this but with the right click?

Something like myElement.rightClick(); or myElement.contextClick();?


DISCLAIMER

This is just for research and knowledge purposes only. Yes, I have searched in many sites, but they always talk about the event, nothing like what I'm asking for. I would appreciate it a lot if anyone could answer this question, even if the answer is "No, you can't ". However, if you are going to state that, please be 100% sure that it is not possible or plausible. As always, thanks in advance!

Community
  • 1
  • 1
J3STER
  • 1,027
  • 2
  • 11
  • 28
  • if jQuery is an option you could refer to the answer on [this post](http://stackoverflow.com/a/2725963/2675902). – Matthew Jan 24 '17 at 03:22

1 Answers1

2

Yap easy:

var element = document.getElementById('Id_here');
if (window.CustomEvent) {
    element.dispatchEvent(new CustomEvent('contextmenu'));
} else if (document.createEvent) {
    var ev = document.createEvent('HTMLEvents');
    ev.initEvent('contextmenu', true, false);
    element.dispatchEvent(ev);
} else { // Internet Explorer
    element.fireEvent('oncontextmenu');
}
Yaser
  • 5,609
  • 1
  • 15
  • 27