Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1053929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:19:39+00:00 2026-05-16T17:19:39+00:00

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

  • 0
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 any way to do this using JavaScript? I want to detect the version number / other attributes in a Greasemonkey script but as I understand it, .toSource() and .toString() strip out comments1.

I don’t want to wrap the header block in <><![CDATA[ ]><> if I can avoid it, and I want to avoid having to duplicate the header block outside of the comments if possible.

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

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:19:40+00:00Added an answer on May 16, 2026 at 5:19 pm

    There is currently no really good way for a Greasemonkey script to know its own metadata (or comments either).   That is why every “autoupdate” script (like this one) requires you to set extra variables so that the script will know its current version.

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

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

    For example:

    1. Suppose your script was named MyTotallyKickassScript.user.js.

    2. Now add a resource directive to your script’s metadata block like so:
      // @resource MeMyself MyTotallyKickassScript.user.js
      Notice that there is no path information to the file, GM will use a relative path to copy the resource, one time, when the script is first installed.

    3. Then you can access the script’s code using GM_getResourceText(), like so:

      var ThisFileSource = GM_getResourceText ("MeMyself");  
      //Optional for Firebug users: console.log (ThisFileSource);
      
    4. You can parse ThisFileSource to get the comments you want.

    5. A script that parses Greasemonkey metadata from a source file is here. You should be able to adapt it with little effort.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of function callbacks, like this: class Blah { private var
I have an object defined like this: Blah = { hideTimer:null, setTimer: function() {
Wondering if there is an elegant way to listen for a function in JavaScript
*Is there a way to call a function defined inside another function in javaSCRIPT?
Is there any way to execute a method of a com object inside a
Here is my function, this works: my.SetTablepagerIndex = function() { var blah = $(#currentPage).text();
You could pattern-match against multiple arguments of a function by creating a tuple and
function Obj1(param) { this.test1 = param || 1; } function Obj2(param, par) { this.test2
Heres my Jquery $(.sectiontitle).click(function (e) { $(this).next('div').slideToggle(slow); el = $(this).find(.toggler > a.toggle); currBg =
A team member put this into our project $(function() { $(body).bind(ajaxError, function(event, XMLHttpRequest, ajaxOptions,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.