Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Dom Right Click Function

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!

like image 855
J3STER Avatar asked Feb 18 '26 19:02

J3STER


1 Answers

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');
}
like image 106
Yaser Avatar answered Feb 21 '26 09:02

Yaser



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!