1

I've found a lot of questions like this one, this one, and this one, purporting to show how to "simulate a right click" or "trigger the context menu", etc. Then I found this one, which simply says it can't be done. I think the first 3 questions are all triggering custom context menus, i.e. menus that are attached to replace the built-in one. I'd like to just simulate a user right-click on a particular element of my choice.

I've tried all the suggestions in the first questions -- varying combinations of createEvent("MouseEvents" / "HTMLEvents") and initEvent / initMouseEvent("click" / "contextmenu"), jQuery's trigger, etc. Nothing makes the default context menu appear.

Is it truly impossible? Does the fact that I'm running "privileged" from a userscript (in Greasemonkey) make any difference? Could I do it from a full-weight Firefox extension?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Coderer
  • 25,844
  • 28
  • 99
  • 154

1 Answers1

1

You're right, the first three answers, that you linked, are for custom menus/controls.

The fourth answer is correct; you can't activate the browser's context menu(s) -- for the same reasons you can't activate the browser's normal menus... It would be a huge security hole that bad guys would exploit.

Likewise Greasemonkey scripts can't do this either. Greasemonkey scripts have some elevated privileges (only when running in sandboxed mode, as of GM 1.0), but the developers of Greasemonkey have not chosen to make context-menus available. (Nor have I heard of a pressing need for such a feature.)

I'm pretty sure that a full-fledged add-on, could activate context menus (or menu items), but I've never tried it and can't provide code, at the moment.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • Thanks, I think that's as close to a "never nowhere no-how" as I'm going to get. Oh well! – Coderer Sep 07 '12 at 10:35
  • You're welcome, and sorry. I'm browsing through [a search](http://stackoverflow.com/search?tab=votes&q=[firefox-addon]%20%22context%20menu%22) and I don't see (yet) that someone's even done this with an add-on/extension. (Doesn't mean it's not possible for an add-on, even if no one's asked about it on SO yet.) – Brock Adams Sep 07 '12 at 10:38