Chr*_*Noe 11 javascript greasemonkey metadata
我的脚本有没有办法检索在自己的头文件中声明的元数据值?除了也许,我在API中看不到任何有希望的东西GM_getValue().那当然会涉及一个特殊的名称语法.我试过,例如:GM_getValue("@name").
这里的动机是避免冗余的规范.
如果无法直接访问GM元数据,也许有一种方法可以阅读脚本本身.它肯定在某个地方的内存中,并且解析起来也不会太难"// @".(在我的情况下,这可能是必要的,因为我真正感兴趣的是@version,这是userscripts.org读取的扩展值.)
这个答案是过时的:由于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);
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅greasemonkey-users组中的此主题.在最后可以找到更强大的实现.
使用在0.9.16版本中添加到Greasemonkey 的GM_info对象.
例如,如果您运行此脚本:
// ==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);
Run Code Online (Sandbox Code Playgroud)
它将输出此对象:
{
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"
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1814 次 |
| 最近记录: |