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!

1
  • if jQuery is an option you could refer to the answer on this post. Commented Jan 24, 2017 at 3:22

1 Answer 1

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');
}
Sign up to request clarification or add additional context in comments.

1 Comment

This code doesn't work for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.