Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <pre> 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"]); </pre> <p>This is working nicely in my scripts.</p> <p>EDIT: Added @resource and @require, which were introduced in Greasemonkey 0.8.0.</p> <p>EDIT: FF5+ compatibility, Array.filter() no longer accepts a regular expression</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload