GM.registerMenuCommand: Difference between revisions

From GreaseSpot Wiki
Jump to navigationJump to search
→‎Description: Changed "ctrl alt" to "control alt"
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.
 
(47 intermediate revisions by 7 users not shown)
Line 1: Line 1:
{{underscore|title=GM_registerMenuCommand}}
== Description ==


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


'''GM_registerMenuCommand(''' ''commandName'', ''commandFunc'', ''accelKey'', ''accelModifiers'', ''accessKey'' ''')'''
Compatibility: [[Version_history#4.11|Greasemonkey 4.11+]]


= Description =
== Syntax ==


Userscripts can call GM_registerMenuCommand to add a menu command to the "User Script Commands" submenu.
{{Function|GM.registerMenuCommand|caption, commandFunc, accessKey}}
The first two arguments are required; the others are optional.


; commandName : Name to display in the menu
=== Arguments ===
; commandFunc : Function to call
; accelKey : A single character (e.g. 'g') or keycode that can trigger the command
; accelModifiers: A string listing modifiers that must be pressed with the accelKey. If there's more than one, then they should be separated with spaces. For example, <code>'shift'</code> or <code>'control alt'</code>. Available modifiers are: shift, alt, meta, control, and accel.
; accessKey : A single character (e.g. 'g') that can be used to jump to the command when the menu is open. It should be a letter in commandName


= Examples =
; <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]


GM_registerMenuCommand( "Hello world!", hello, "e", "control", "h" );
==== History ====


GM_registerMenuCommand( "Hello world! (again)", hello2, "e", "shift alt", "w" );
This method existed in a similar but different form before Greasemonkey 4.0.


GM_registerMenuCommand( "Hello world (simple)", helloSimple );
== Returns ==


<code>undefined</code>


[[Category:API Reference|R]]
== Examples ==
 
<pre class='sample'>GM.registerMenuCommand("Hello, world (simple)", () => alert("Hello, world!"));</pre>
 
<pre class='sample'>function hello() { ... }
GM.registerMenuCommand("Hello, world!", hello, "h");</pre>
 
[[Category:API_Reference|R]]
 
== See Also ==
 
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.