Greasemonkey defines two functions, GM_setValue
and GM_getValue
, that allow user scripts to store and retrieve “private” data that only the user script can access. (Other scripts can't access them, not even other user scripts.) You can use
these functions to store script-specific configuration, to maintain a cache that persists between pages, or to keep a permanent
activity log.
GM_setValue
stores a script-specific configuration value, and GM_getValue
retrieves it.
function GM_setValue(key, value); function GM_getValue(key, defaultValue);
key
argument is a string of no fixed format. value
may be a string, boolean, or integer. The defaultValue
argument of GM_getValue
is optional; if present, it will be returned if the requested key
does not exist. If no defaultValue
is given and the requested key
does not exist, GM_getValue
will return undefined
.
These functions were introduced in Greasemonkey 0.3. You should test whether they exist and degrade gracefully if they do not.
Further reading
- MyPIPsTag prompts you for a username the first time you run it.
- POST Interceptor adds menu items (with
GM_registerMenuCommand
) to toggle whether the script is active. - MSDN Language Filter inserts its configuration options into the page itself.