Parse Greasemonkey metadata and / or grab comments from a function

function blah(_x) { console.info(_x.toSource().match(/\/\/\s*@version\s+(.*)\s*\n/i)); } function foobar() { // ==UserScript== // @version 1.2.3.4 // ==/UserScript== blah(arguments.callee); } foobar(); 

Is there a way to do this using JavaScript? I want to determine the version number / other attributes in a Greasemonkey script, but as I understand it .toSource() and .toString() .toSource() out comments 1 .

I do not want to wrap the title block in <><![CDATA[ ]><> if I can avoid it, and I want to avoid duplicating the title block outside of the comments, if possible.

Is it possible? Are there alternatives to toSource() / .toString() that will make this possible?

[1] - http://isc.sans.edu/diary.html?storyid=3231

+4
source share
2 answers

There is currently no really good way for a Greasemonkey script to know its own metadata (or comments). This is why every "autoupdate" script ( like this one ) requires setting additional variables so that the script knows its current version.

As aularon said, the only way to get comments from a JS function is to parse the source HTML of the <script> or file.

However, there is a trick that might work for you. You can read your own GM script as a resource , and then parse this source.

For instance:

+1
source

The Javascript engine ignores comments, the only way to do this is to execute the <script> innerHTML process or execute an AJAX string request that retrieves the .js file if it is an external file.

+1
source

All Articles