Parse Greasemonkey metadata and/or grab comments from within a function

佐手、 提交于 2019-12-05 16:52:36

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.

Javascript engine will ignore comments, the only way to do that is to string process <script>'s innerHTML, or string process an AJAX request that fetches the .js file, if it was an external file.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!