Accessing Greasemonkey metadata from within your script?

greasemonkey, javascript, metadata

Solution

This answer is out of date : As of Greasemonkey 0.9.16 (Feb 2012) please see Brock's answer regarding `GM_info`

Yes. A very simple example is:

var metadata=<> 
// ==UserScript==
// @name           Reading metadata
// @namespace      http://www.afunamatata.com/greasemonkey/
// @description    Read in metadata from the header
// @version        0.9
// @include        https://stackoverflow.com/questions/104568/accessing-greasemonkey-metadata-from-within-your-script
// ==/UserScript==
</>.toString();

GM_log(metadata); 

See this thread on the greasemonkey-users group for more information. A more robust implementation can be found near the end.

Problem

Is there any way that my script can retrieve metadata values that are declared in its own header? I don't see anything promising in the API, except perhaps `GM_getValue()`. That would of course involve a special name syntax. I have tried, for example: `GM_getValue("@name")`. The motivation here is to avoid redundant specification. If GM metadata is not directly accessible, perhaps there's a way to read the body of the script itself. It's certainly in memory somewhere, and it wouldn't be too awfully hard to parse for `"// @"`. (That may be necessary in my case any way, since the value I'm really interested in is `@version`, which is an extended value read by userscripts.org.)

Original source