解析 Greasemonkey 元数据和/或从函数内获取注释

发布于 2024-09-17 18:03:27 字数 716 浏览 5 评论 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();

有没有办法使用 JavaScript 来做到这一点?我想检测 Greasemonkey 脚本中的版本号/其他属性,但据我了解, .toSource().toString() 删除了注释1

如果可以避免的话,我不想将标头块包装在 <><> 中,并且我想避免重复如果可能的话,标题块位于注释之外。

这可能吗?是否有 toSource() / .toString() 的替代方案可以实现这一点?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情痴 2024-09-24 18:03:27

目前,Greasemonkey 脚本没有真正好的方法来了解其自己的元数据(或注释)。  这就是为什么每个“自动更新”脚本(像这个)都需要您设置额外的变量,以便该脚本将知道其当前版本。

正如 aularon 所说,从 JS 函数获取注释的唯一方法是解析

不过,有一个技巧可能对您有用。您可以读入您自己的 GM 脚本作为资源,然后解析该源。

例如:

  1. 假设您的脚本名为 MyTotallyKickassScript.user.js

  2. 现在将 resource 指令添加到脚本的元数据块,如下所示:
    // @resource MeMyself MyTotallyKickassScript.user.js
    请注意,没有文件的路径信息,GM 将使用相对路径复制资源,一次,当脚本第一次安装时。

  3. 然后您可以使用GM_getResourceText()访问脚本的代码,像这样:

    var ThisFileSource = GM_getResourceText ("MeMyself");  
    //Firebug 用户可选:console.log (ThisFileSource);
    
  4. 您可以解析 ThisFileSource 来获取您想要的注释。

  5. 这里有一个从源文件解析 Greasemonkey 元数据的脚本。您应该能够轻松地适应它。

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.

∞觅青森が 2024-09-24 18:03:27

Javascript 引擎将忽略注释,唯一的方法是字符串处理

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文