Is there any way that my script can retrieve metadata values that are declared in its own header? I don't see anything promising in the API, except perhaps GM_getValue()
. That would of course involve a special name syntax. I have tried, for example: GM_getValue("@name")
.
我的脚本是否有任何方法可以检索在其自己的标头中声明的元数据值?除了GM_getValue()之外,我在API中看不到任何有希望的东西。那当然会涉及一个特殊的名称语法。我试过,例如:GM_getValue(“@ name”)。
The motivation here is to avoid redundant specification.
这里的动机是避免冗余的规范。
If GM metadata is not directly accessible, perhaps there's a way to read the body of the script itself. It's certainly in memory somewhere, and it wouldn't be too awfully hard to parse for "// @"
. (That may be necessary in my case any way, since the value I'm really interested in is @version
, which is an extended value read by userscripts.org.)
如果无法直接访问GM元数据,也许有一种方法可以读取脚本本身。它肯定在某个地方的内存中,并且解析“// @”并不是太难。 (在我的情况下,这可能是必要的,因为我真正感兴趣的是@version,这是userscripts.org读取的扩展值。)
3 个解决方案
#1
7
This answer is out of date : As of Greasemonkey 0.9.16 (Feb 2012) please see Brock's answer regarding GM_info
这个答案已经过时了:截至Greasemonkey 0。0。16(2012年2月),请参阅Brock关于GM_info的回答
Yes. A very simple example is:
是。一个非常简单的例子是:
var metadata=<>
// ==UserScript==
// @name Reading metadata
// @namespace http://www.afunamatata.com/greasemonkey/
// @description Read in metadata from the header
// @version 0.9
// @include https://*.com/questions/104568/accessing-greasemonkey-metadata-from-within-your-script
// ==/UserScript==
</>.toString();
GM_log(metadata);
See this thread on the greasemonkey-users group for more information. A more robust implementation can be found near the end.
有关更多信息,请参阅greasemonkey-users组中的此主题。在最后可以找到更强大的实现。
#2
6
Use the GM_info
object, which was added to Greasemonkey in version 0.9.16.
使用GM_info对象,该对象已添加到版本0.9.16中的Greasemonkey。
For example, if You run this script:
例如,如果您运行此脚本:
// ==UserScript==
// @name _GM_info demo
// @namespace Stack Overflow
// @description Tell me more about me, me, ME!
// @include http://*.com/questions/*
// @version 8.8
// ==/UserScript==
unsafeWindow.console.clear ();
unsafeWindow.console.log (GM_info);
It will output this object:
它将输出此对象:
{
version: (new String("0.9.18")),
scriptWillUpdate: false,
script: {
description: "Tell me more about me, me, ME!",
excludes: [],
includes: ["http://*.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://*.com/questions/*\r\n// @version 8.8\r\n"
}
#3
4
Building on Athena's answer, here is my generalized solution that yields an object of name/value pairs, each representing a metadata property. Note that certain properties can have multiple values, (@include, @exclude, @require, @resource), therefore my parser captures those as Arrays - or in the case of @resource, as a subordinate Object of name/value pairs.
基于Athena的答案,这是我的通用解决方案,它产生一个名称/值对的对象,每个对象代表一个元数据属性。请注意,某些属性可以有多个值(@ 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"]);
This is working nicely in my scripts.
这在我的脚本中运行良好。
EDIT: Added @resource and @require, which were introduced in Greasemonkey 0.8.0.
编辑:添加了@resource和@require,它们是在Greasemonkey 0.8.0中引入的。
EDIT: FF5+ compatibility, Array.filter() no longer accepts a regular expression
编辑:FF5 +兼容性,Array.filter()不再接受正则表达式
#1
7
This answer is out of date : As of Greasemonkey 0.9.16 (Feb 2012) please see Brock's answer regarding GM_info
这个答案已经过时了:截至Greasemonkey 0。0。16(2012年2月),请参阅Brock关于GM_info的回答
Yes. A very simple example is:
是。一个非常简单的例子是:
var metadata=<>
// ==UserScript==
// @name Reading metadata
// @namespace http://www.afunamatata.com/greasemonkey/
// @description Read in metadata from the header
// @version 0.9
// @include https://*.com/questions/104568/accessing-greasemonkey-metadata-from-within-your-script
// ==/UserScript==
</>.toString();
GM_log(metadata);
See this thread on the greasemonkey-users group for more information. A more robust implementation can be found near the end.
有关更多信息,请参阅greasemonkey-users组中的此主题。在最后可以找到更强大的实现。
#2
6
Use the GM_info
object, which was added to Greasemonkey in version 0.9.16.
使用GM_info对象,该对象已添加到版本0.9.16中的Greasemonkey。
For example, if You run this script:
例如,如果您运行此脚本:
// ==UserScript==
// @name _GM_info demo
// @namespace Stack Overflow
// @description Tell me more about me, me, ME!
// @include http://*.com/questions/*
// @version 8.8
// ==/UserScript==
unsafeWindow.console.clear ();
unsafeWindow.console.log (GM_info);
It will output this object:
它将输出此对象:
{
version: (new String("0.9.18")),
scriptWillUpdate: false,
script: {
description: "Tell me more about me, me, ME!",
excludes: [],
includes: ["http://*.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://*.com/questions/*\r\n// @version 8.8\r\n"
}
#3
4
Building on Athena's answer, here is my generalized solution that yields an object of name/value pairs, each representing a metadata property. Note that certain properties can have multiple values, (@include, @exclude, @require, @resource), therefore my parser captures those as Arrays - or in the case of @resource, as a subordinate Object of name/value pairs.
基于Athena的答案,这是我的通用解决方案,它产生一个名称/值对的对象,每个对象代表一个元数据属性。请注意,某些属性可以有多个值(@ 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"]);
This is working nicely in my scripts.
这在我的脚本中运行良好。
EDIT: Added @resource and @require, which were introduced in Greasemonkey 0.8.0.
编辑:添加了@resource和@require,它们是在Greasemonkey 0.8.0中引入的。
EDIT: FF5+ compatibility, Array.filter() no longer accepts a regular expression
编辑:FF5 +兼容性,Array.filter()不再接受正则表达式