解析 Greasemonkey 元数据和/或从函数内获取注释
function blah(_x)
{
console.info(_x.toSource().match(/\/\/\s*@version\s+(.*)\s*\n/i));
}
function foobar()
{
// ==UserScript==
// @version 1.2.3.4
// ==/UserScript==
blah(arguments.callee);
}
foobar();
有没有办法使用 JavaScript 来做到这一点?我想检测 Greasemonkey 脚本中的版本号/其他属性,但据我了解, .toSource()
和 .toString()
删除了注释1 。
如果可以避免的话,我不想将标头块包装在 <><>
中,并且我想避免重复如果可能的话,标题块位于注释之外。
这可能吗?是否有 toSource()
/ .toString()
的替代方案可以实现这一点?
function blah(_x)
{
console.info(_x.toSource().match(/\/\/\s*@version\s+(.*)\s*\n/i));
}
function foobar()
{
// ==UserScript==
// @version 1.2.3.4
// ==/UserScript==
blah(arguments.callee);
}
foobar();
Is there any way to do this using JavaScript? I want to detect the version number / other attributes in a Greasemonkey script but as I understand it, .toSource()
and .toString()
strip out comments1.
I don't want to wrap the header block in <><![CDATA[ ]><>
if I can avoid it, and I want to avoid having to duplicate the header block outside of the comments if possible.
Is this possible? Are there alternatives to toSource()
/ .toString()
that would make this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目前,Greasemonkey 脚本没有真正好的方法来了解其自己的元数据(或注释)。 这就是为什么每个“自动更新”脚本(像这个)都需要您设置额外的变量,以便该脚本将知道其当前版本。
正如 aularon 所说,从 JS 函数获取注释的唯一方法是解析
标记或文件的源 HTML。
不过,有一个技巧可能对您有用。您可以读入您自己的 GM 脚本作为资源,然后解析该源。
例如:
假设您的脚本名为
MyTotallyKickassScript.user.js
。现在将
resource
指令添加到脚本的元数据块,如下所示:// @resource MeMyself MyTotallyKickassScript.user.js
请注意,没有文件的路径信息,GM 将使用相对路径复制资源,一次,当脚本第一次安装时。
然后您可以使用
GM_getResourceText()
访问脚本的代码,像这样:您可以解析
ThisFileSource
来获取您想要的注释。这里有一个从源文件解析 Greasemonkey 元数据的脚本。您应该能够轻松地适应它。
There is currently no really good way for a Greasemonkey script to know its own metadata (or comments either). That is why every "autoupdate" script (like this one) requires you to set extra variables so that the script will know its current version.
As aularon said, the only way to get the comments from a JS function is to parse the source HTML of the
<script>
tag or of the file.However, there is a trick that might work for you. You can read in your own GM script as a resource and then parse that source.
For example:
Suppose your script was named
MyTotallyKickassScript.user.js
.Now add a
resource
directive to your script's metadata block like so:// @resource MeMyself MyTotallyKickassScript.user.js
Notice that there is no path information to the file, GM will use a relative path to copy the resource, one time, when the script is first installed.
Then you can access the script's code using
GM_getResourceText()
, like so:You can parse
ThisFileSource
to get the comments you want.A script that parses Greasemonkey metadata from a source file is here. You should be able to adapt it with little effort.
Javascript 引擎将忽略注释,唯一的方法是字符串处理
的
innerHTML
,或字符串处理AJAX
请求获取.js
文件(如果它是外部文件)。Javascript engine will ignore comments, the only way to do that is to string process
<script>
'sinnerHTML
, or string process anAJAX
request that fetches the.js
file, if it was an external file.