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.)
Building on Athena's answer, here is my generalized solution that yields an object of name/value pairs, each representing a metadata property. Note that certain properties can have multiple values, (@include, @exclude, @require, @resource), therefore my parser captures those as Arrays - or in the case of @resource, as a subordinate Object of name/value pairs.
This is working nicely in my scripts.
EDIT: Added @resource and @require, which were introduced in Greasemonkey 0.8.0.
EDIT: FF5+ compatibility, Array.filter() no longer accepts a regular expression
Use the
GM_info
object, which was added to Greasemonkey in version 0.9.16.For example, if You run this script:
It will output this object:
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:
See this thread on the greasemonkey-users group for more information. A more robust implementation can be found near the end.