GM.registerMenuCommand: Difference between revisions

From GreaseSpot Wiki
Jump to navigationJump to search
Marti (talk | contribs)
m Misc formatting and commenting
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.
 
(33 intermediate revisions by 7 users not shown)
Line 1: Line 1:
{{underscore|title=GM_registerMenuCommand}}
__NOTOC__
== 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
:Returns: Nothing
:Compatibility: [[Version_history#0.2.5|Greasemonkey 0.2.5+]]
 
:{| border="1" cellpadding="5"
|+ Parameters
!|'''Properties''' || !|'''Event Handlers'''
|-
| <code>[[#commandName |commandName]]</code> ||<code>[[#commandFunc|commandFunc]]</code>
|-
|<code>[[#accelKey |accelKey]]</code>
|-
|<code>[[#accelModifiers |accelModifiers]]</code>
|-
|<code>[[#accessKey |accessKey]]</code>
|}
:* All properties and event handlers are optional except [[#commandName|commandName]] and [[#commandFunc|commandFunc]].
 
[[#top|top]]
=== 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]].
 
[[#top|top]] | [[#Syntax|back]]''
 
==== <code>commandFunc</code> ====
:Value: Function
:Usage: <code>'''commandFunc''' = function(){ ''/* some code */'' };</code>
 
:* Function to call
 
[[#top|top]] | [[#Syntax|back]]
 
==== <code>accelKey</code> ====
:Value: String
:Usage: <code>'''accelKey''' = "g";</code>
 
:* A single character or keycode that can trigger the command.
 
[[#top|top]] | [[#Syntax|back]]


==== <code>accelModifiers</code> ====
=== Arguments ===
: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.
; <code>caption</code>
: <code>String</code> The caption to display on the menu item.
; <code>commandFunc</code>
: <code>Function</code> The function to call when this menu item is selected by the user.
; <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]


[[#top|top]] | [[#Syntax|back]]
==== History ====


==== <code>accessKey</code> ====
This method existed in a similar but different form before Greasemonkey 4.0.
: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]].
== Returns ==


[[#top|top]] | [[#Syntax|back]]
<code>undefined</code>


== Examples ==
== Examples ==
<code><pre>GM_registerMenuCommand( "Hello, world!", hello, "e", "control", "h" );</pre></code>


<code><pre>GM_registerMenuCommand( "Hello, world! (again)", hello2, "e", "shift alt", "w" );</pre></code>
<pre class='sample'>GM.registerMenuCommand("Hello, world (simple)", () => alert("Hello, world!"));</pre>


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


[[#top|top]]
[[Category:API_Reference|R]]


== Notes ==
== See Also ==


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

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.