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.
var scriptMetadata = parseMetadata(.toString());
function parseMetadata(headerBlock)
{
// split up the lines, omitting those not containing "// @"
function isAGmParm(element) { return /\/\/ @/.test(element); }
var lines = headerBlock.split(/[\r\n]+/).filter(isAGmParm);
// initialize the result object with empty arrays for the enumerated properties
var metadata = { include: [], exclude: [], require: [], resource: {} };
for each (var line in lines)
{
[line, name, value] = line.match(/\/\/ @(\S+)\s*(.*)/);
if (metadata[name] instanceof Array)
metadata[name].push(value);
else if (metadata[name] instanceof Object) {
[rName, rValue] = value.split(/\s+/); // each resource is named
metadata[name][rName] = rValue;
}
else
metadata[name] = value;
}
return metadata;
}
// example usage
GM_log("version: " + scriptMetadata["version"]);
GM_log("res1: " + scriptMetadata["resource"]["res1"]);
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