GM.registerMenuCommand: Difference between revisions

From GreaseSpot Wiki
Jump to navigationJump to search
→‎Arguments: documentation, mostly as links to the underlying Mozilla attributes
LWChris (talk | contribs)
→‎See Also: Rephrase GM_config reference to make it even clearer that this is a 3rd party tool, not a native feature/function.
 
(16 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{DISPLAYTITLE:GM_registerMenuCommand}}
== Description ==
== Description ==


This method allows user scripts to add an item to the [[Greasemonkey_Manual:Monkey_Menu#The_Menu|User Script Commands]] menu.
This method allows user scripts to add an item to the [[Greasemonkey_Manual:Monkey_Menu#The_Menu|User Script Commands]] menu.


Compatibility: [[Version_history#0.2.5|Greasemonkey 0.2.5+]]
Compatibility: [[Version_history#4.11|Greasemonkey 4.11+]]


== Syntax ==
== Syntax ==


{{Function|GM_registerMenuCommand|caption, commandFunc, accelKey, accelModifiers, accessKey}}
{{Function|GM.registerMenuCommand|caption, commandFunc, accessKey}}


=== Arguments ===
=== Arguments ===
Line 17: Line 15:
; <code>commandFunc</code>
; <code>commandFunc</code>
: <code>Function</code> The function to call when this menu item is selected by the user.
: <code>Function</code> The function to call when this menu item is selected by the user.
; <code>accelKey</code>
: <code>String</code> or <codeNumber</code> A single character or keycode that can trigger the command.  (See accelerator notes section.)
; <code>accelModifiers</code>
: <code>String</code>  A string listing modifiers that may/must be pressed with the <code>accelKey</code>.  (See accelerator notes section.)
; <code>accessKey</code>
; <code>accessKey</code>
: <code>String</code> A single character that can be used to select command when the menu is open.  It should be a letter in the <code>caption</code>. [https://developer.mozilla.org/en/XUL/Attribute/accesskey]
: <code>String</code> A single character that can be used to select command when the menu is open.  It should be a letter in the <code>caption</code>. [https://developer.mozilla.org/en/XUL/Attribute/accesskey]


==== Accelerator Notes ====
==== History ====


The <code>accelKey</code> and <code>accelModifiers</code> are used verbatim for a [https://developer.mozilla.org/en/XUL/key XUL key] element.
This method existed in a similar but different form before Greasemonkey 4.0.
The <code>accelKey</code> is used as the <code>key</code> (when a string) or <code>keycode</code> (when a number) attribute.
The <code>accelModifiers</code> is used as the <code>modifiers</code> attribute.
The behavior is as described in [https://developer.mozilla.org/en/XUL/key#Details_on_key.2c_keycode.2c_and_modifiers_attributes Mozilla's documentation for these attributes].


== Returns ==
== Returns ==
Line 37: Line 28:
== Examples ==
== Examples ==


<pre class='sample'>GM_registerMenuCommand("Hello, world (simple)", helloSimple);</pre>
<pre class='sample'>GM.registerMenuCommand("Hello, world (simple)", () => alert("Hello, world!"));</pre>
 
<pre class='sample'>function hello() { ... }
GM.registerMenuCommand("Hello, world!", hello, "h");</pre>


<pre class='sample'>GM_registerMenuCommand("Hello, world!", hello, "e", "control", "h");</pre>
[[Category:API_Reference|R]]


<pre class='sample'>GM_registerMenuCommand("Hello, world! (again)", hello2, "e", "shift alt", "w");</pre>
== See Also ==


[[Category:API_Reference|R]]
The third-party <code>@require</code> library "[[GM_config]]" by ''sizzlemctwizzle'' is frequently used with <code>GM.registerMenuCommand</code>.

Latest revision as of 02:11, 13 December 2023

Description

This method allows user scripts to add an item to the User Script Commands menu.

Compatibility: Greasemonkey 4.11+

Syntax

function GM.registerMenuCommand( caption, commandFunc, accessKey )

Arguments

caption
String The caption to display on the menu item.
commandFunc
Function The function to call when this menu item is selected by the user.
accessKey
String A single character that can be used to select command when the menu is open. It should be a letter in the caption. [1]

History

This method existed in a similar but different form before Greasemonkey 4.0.

Returns

undefined

Examples

GM.registerMenuCommand("Hello, world (simple)", () => alert("Hello, world!"));
function hello() { ... }
GM.registerMenuCommand("Hello, world!", hello, "h");

See Also

The third-party @require library "GM_config" by sizzlemctwizzle is frequently used with GM.registerMenuCommand.