从脚本中访问Greasemonkey元数据?

时间:2008-09-19 19:01:06

标签: javascript metadata greasemonkey

我的脚本有没有办法检索在自己的标头中声明的元数据值?除了GM_getValue()之外,我在API中看不到任何有希望的东西。那当然会涉及一个特殊的名称语法。我试过了,例如:GM_getValue("@name")

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

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

3 个答案:

答案 0 :(得分:7)

此答案已过期:自Greasemonkey 0.9。16(2012年2月)起,请参阅Brock's answer有关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); 

有关详细信息,请参阅this thread on the greasemonkey-users group。在最后可以找到更强大的实现。

答案 1 :(得分:6)

使用版本0.9.16中添加到Greasemonkey的the GM_info 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"
}

答案 2 :(得分:4)

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

var scriptMetadata = parseMetadata(.toString());

function parseMetadata(headerBlock)
{
    // split up the lines, omitting those not containing "// @"
    function isAGmParm(element) { return /\/\/ @/.test(element); }
    var lines = headerBlock.split(/[\r\n]+/).filter(isAGmParm);
    // initialize the result object with empty arrays for the enumerated properties
    var metadata = { include: [], exclude: [], require: [], resource: {} };
    for each (var line in lines)
    {
        [line, name, value] = line.match(/\/\/ @(\S+)\s*(.*)/);
        if (metadata[name] instanceof Array)
            metadata[name].push(value);
        else if (metadata[name] instanceof Object) {
            [rName, rValue] = value.split(/\s+/); // each resource is named
            metadata[name][rName] = rValue;
        }
        else
            metadata[name] = value;
    }
    return metadata;
}

// example usage
GM_log("version: " + scriptMetadata["version"]);
GM_log("res1: " + scriptMetadata["resource"]["res1"]);

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

编辑:添加了@resource和@require,这些都是在Greasemonkey 0.8.0中引入的。

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