2008-09-19 69 views
11

有没有什么办法可以让我的脚本检索在自己的头文件中声明的元数据值?我没有看到任何有希望的API,除了可能GM_getValue()。那当然会涉及一个特殊的名字语法。我试过了,例如:GM_getValue("@name")从脚本内访问Greasemonkey元数据?

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

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

回答

7

这个答案是过时的:作为的Greasemonkey 0.9 1.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了解更多信息。最后可以找到更强大的实现。

4

建立在雅典娜的答案,这里是我的广义解决方案,产生名称/值对的对象,每个代表一个元数据属性。请注意,某些属性可能具有多个值(@include,@exclude,@require,@resource),因此我的解析器将这些值捕获为数组 - 或者在@resource的情况下,作为名称/值对的从属对象。

 
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()不再接受一个正则表达式

4

使用the GM_info object,向其中加入在0.9.16版Greasemonkey的。

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

// ==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" 
}