11

有什么方法可以让我的脚本检索在其自己的标头中声明的元数据值?我在 API 中看不到任何有希望的东西,除了GM_getValue(). 这当然会涉及特殊的名称语法。我试过了,例如:GM_getValue("@name").

这里的动机是避免冗余规范。

如果不能直接访问 GM 元数据,也许有一种方法可以读取脚本本身的正文。它肯定在内存中的某个地方,解析它不会太难"// @"。(在我的情况下这可能是必要的,因为我真正感兴趣的值是@version,这是userscripts.org读取的扩展值。)

4

3 回答 3

7

此答案已过时:截至 Greasemonkey 0.9.16(2012 年 2 月),请参阅Brock关于GM_info


是的。一个非常简单的例子是:

var metadata=<> 
// ==UserScript==
// @name           Reading metadata
// @namespace      http://www.afunamatata.com/greasemonkey/
// @description    Read in metadata from the header
// @version        0.9
// @include        https://stackoverflow.com/questions/104568/accessing-greasemonkey-metadata-from-within-your-script
// ==/UserScript==
</>.toString();

GM_log(metadata); 

有关更多信息,请参阅greasemonkey-users 组上的此线程。在接近尾声时可以找到更健壮的实现。

于 2008-09-19T19:28:40.303 回答
7

使用GM_info0.9.16 版本中添加到 Greasemonkey 的 object

例如,如果您运行此脚本:

// ==UserScript==
// @name            _GM_info demo
// @namespace       Stack Overflow
// @description     Tell me more about me, me, ME!
// @include         http://stackoverflow.com/questions/*
// @version         8.8
// ==/UserScript==

unsafeWindow.console.clear ();
unsafeWindow.console.log (GM_info);


它将输出这个对象:

{
    version:            (new String("0.9.18")),
    scriptWillUpdate:   false,
    script: {
        description:    "Tell me more about me, me, ME!",
        excludes:       [],
        includes:       ["http://stackoverflow.com/questions/*"],
        matches:        [],
        name:           "_GM_info demo",
        namespace:      "Stack Overflow",
        'run-at':       "document-end",
        unwrap:         false,
        version:        "8.8"
    },
    scriptMetaStr:      "// @name            _GM_info demo\r\n// @namespace       Stack Overflow\r\n// @description     Tell me more about me, me, ME!\r\n// @include         http://stackoverflow.com/questions/*\r\n// @version         8.8\r\n"
}
于 2012-05-07T00:05:35.517 回答
4

基于 Athena 的回答,这是我的通用解决方案,它产生一个名称/值对对象,每个对象代表一个元数据属性。请注意,某些属性可以有多个值(@include、@exclude、@require、@resource),因此我的解析器将它们捕获为数组 - 或者在 @resource 的情况下,作为名称/值对的从属对象。

var scriptMetadata = parseMetadata(.toString());

函数 parseMetadata(headerBlock)
{
    // 拆分行,省略那些不包含“// @”的行
    function isAGmParm(element) { return /\/\/ @/.test(element); }
    var lines = headerBlock.split(/[\r\n]+/).filter(isAGmParm);
    // 使用枚举属性的空数组初始化结果对象
    var metadata = { 包括:[],排除:[],需要:[],资源:{} };
    对于每个(var line in lines)
    {
        [行、名称、值] = line.match(/\/\/@(\S+)\s*(.*)/);
        if (metadata[name] instanceof 数组)
            元数据[名称].push(值);
        else if (metadata[name] instanceof Object) {
            [rName, rValue] = value.split(/\s+/); // 每个资源都被命名
            元数据[名称][rName] = rValue;
        }
        别的
            元数据[名称] = 值;
    }
    返回元数据;
}

// 示例用法
GM_log("版本:" + scriptMetadata["版本"]);
GM_log("res1:" + scriptMetadata["resource"]["res1"]);

这在我的脚本中运行良好。

编辑:添加了在 Greasemonkey 0.8.0 中引入的 @resource 和 @require。

编辑:FF5+ 兼容性,Array.filter() 不再接受正则表达式

于 2008-09-21T21:01:34.913 回答