GM.registerMenuCommand: Difference between revisions

From GreaseSpot Wiki
Jump to navigationJump to search
m (Text replace - "{{Core samp" to "{{Samp")
m (Arantius moved page GM registerMenuCommand to GM.registerMenuCommand: New GM-4 name with dot, not underscore.)
(24 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{DISPLAYTITLE:GM_registerMenuCommand}}
== Description ==
== Description ==


This [[API_reference|API]] method allows user scripts to add a menu command to the [[Greasemonkey_Manual:Monkey_Menu#The_Menu|"User Script Commands" submenu]].
This method allows user scripts to add an item to the [[Greasemonkey_Manual:Monkey_Menu#The_Menu|User Script Commands]] menu.


[[#Examples|Examples]] | [[#Notes|Notes]]
Compatibility: [[Version_history#4.11|Greasemonkey 4.11+]]


== Syntax ==
== Syntax ==


'''GM_registerMenuCommand(''' ''commandName'', ''commandFunc'', ''accelKey'', ''accelModifiers'', ''accessKey'' ''')'''
{{Function|GM.registerMenuCommand|caption, commandFunc, accessKey}}


:Value: Function
=== Arguments ===
:Returns: undefined
:Compatibility: [[Version_history#0.2.5|Greasemonkey 0.2.5+]]


:{| cellpadding="5" style="border-style:solid; background:#FFFFE0;"
; <code>caption</code>
|+ Parameters
: <code>String</code> The caption to display on the menu item.
!style="background:#CC9900;"|'''Properties''' || style="background:#CC9900;"|'''Event Handlers'''
; <code>commandFunc</code>
|-
: <code>Function</code> The function to call when this menu item is selected by the user.
| <code><span style="background:#FFFFE0;">[[#commandName |commandName]]</span></code> ||<code><span style="background:#FFFFE0;">[[#commandFunc|commandFunc]]</span></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><span style="background:#FFFFE0;">[[#accelKey |accelKey]]</span></code>
|-
|<code><span style="background:#FFFFE0;">[[#accelModifiers |accelModifiers]]</span></code>
|-
|<code><span style="background:#FFFFE0;">[[#accessKey |accessKey]]</span></code>
|}
:* All properties and event handlers are optional except [[#commandName|commandName]] and [[#commandFunc|commandFunc]].


[[#top|top]]
==== History ====
=== Properties ===
----
==== <code>commandName</code> ====
:Value: String
:Usage: <code>'''commandName''' = "Some Name";</code>


:* Name to display in the [[Greasemonkey_Manual:Monkey_Menu#The_Menu|"User Script Commands" submenu]].
This method existed in a similar but different form before Greasemonkey 4.0.


[[#top|top]] | [[#Syntax|back]]''
== Returns ==


==== <code>commandFunc</code> ====
<code>undefined</code>
:Value: Function
:Usage: <code>'''commandFunc''' = function(){ ''/* some code */'' };</code>


:* Function to call
== Examples ==


[[#top|top]] | [[#Syntax|back]]
<pre class='sample'>GM_registerMenuCommand("Hello, world (simple)", () => alert("Hello, world!"));</pre>


==== <code>accelKey</code> ====
<pre class='sample'>function hello() { ... }
:Value: String
GM_registerMenuCommand("Hello, world!", hello, "h");</pre>
:Usage: <code>'''accelKey''' = "g";</code>


:* A single character or keycode that can trigger the command.
[[Category:API_Reference|R]]
 
[[#top|top]] | [[#Syntax|back]]
 
==== <code>accelModifiers</code> ====
:Value: String
:Usage: <code>'''accelModifiers''' = "control alt";</code>
 
:* A string listing modifiers that must be pressed with the [[#accelKey|accelKey]]. If there's more than one, then they should be separated with spaces. Available modifiers are: shift, alt, meta, control, and accel.
 
[[#top|top]] | [[#Syntax|back]]


==== <code>accessKey</code> ====
== See Also ==
:Value: String
:Usage: <code>'''accessKey''' = "g";</code>


:* A single character that can be used to jump to the command when the menu is open. It should be a letter in [[#commandName|commandName]].
The [[GM_config]] <code>@require</code> library is frequently used with <code>GM.registerMenuCommand</code>.
 
[[#top|top]] | [[#Syntax|back]]
 
== Examples ==
{{Samp |1=<pre style="border: none; margin: inherit;">GM_registerMenuCommand("Hello, world (simple)", helloSimple);</pre>}}
 
{{Samp |1=<pre style="border: none; margin: inherit;">GM_registerMenuCommand("Hello, world!", hello, "e", "control", "h");</pre>}}
 
{{Samp |1=<pre style="border: none; margin: inherit;">GM_registerMenuCommand("Hello, world! (again)", hello2, "e", "shift alt", "w");</pre>}}
 
[[#top|top]]
 
[[Category:API_Reference|R]]

Revision as of 01:10, 28 January 2021

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 GM_config @require library is frequently used with GM.registerMenuCommand.