Search code examples
javascriptgreasemonkeytampermonkeyuserscripts

What is the `accessKey` parameter of GM_registerMenuCommand() and how to use it?


I was looking at the GM_registerMenuCommand function in Tampermonkey and I noticed a third parameter called accessKey and thought it is to pass a hotkey to trigger this command.

What's the format of the parameter passed and am I wrong in assuming this is what it does?

I couldn't find documentation on the parameter anywhere. Does anyone here ever used it before?


Solution

  • The syntax for that function is:

    GM_registerMenuCommand (menuName, callbackFunction, accessKey)

    From Menu Design Guidelines(Number 8):

    Dropdowns (both menus and boxes) should support not only mouse input, but keys as well. In dropdown menus, access keys should enable users to quickly select a visible option without using the mouse. In a dropdown box, users should be able to type a letter and quickly navigate to options starting with that letter.

    (Emphasis added.)

    Important: Access keys are different from, and in addition to, any command shortcuts ("hotkeys").


    Here's a sample working Tampermonkey script that illustrates the use:

    // ==UserScript==
    // @name     _GM_registerMenuCommand demo
    // @match    https://stackoverflow.com/questions/56024629/*
    // @match    *://YOUR_SERVER.COM/YOUR_PATH/*
    // @grant    GM_registerMenuCommand
    // ==/UserScript==
    
    /*-- GM_registerMenuCommand (menuName, callbackFunction, accessKey)
    */
    GM_registerMenuCommand ("Say Hello", helloFunc, "H");
    
    function helloFunc () {
        console.log ("Say hello to my little script.");
    }
    

    If you install and run this via Tampermonkey you will see (click for larger image):

    Tampermonkey screenshot


    Notes:

    1. Violentmonkey ignores the accessKey parameter, for now.
    2. Greasemonkey 4+ has borked this feature.  Per usual.
    3. You can set a keyboard command to activate the Tampermonkey icon/menu in Chrome.
    4. Keyboard icon activation currently does not seem to work in Firefox, but a workaround is scheduled for Firefox version 67.