问题描述
当我的GM脚本执行此操作时:
When my GM script does this:
var curTab = GM_openInTab(url);
它在浏览器控制台中导致'GM_openInTab is not defined'
JavaScript错误.
it results in a 'GM_openInTab is not defined'
JavaScript error in the Browser Console.
我也尝试使用var curWin = window.open(url);
代替GM_openInTab
,但是没有任何影响.
I also tried using var curWin = window.open(url);
instead of GM_openInTab
but it had no affect.
我要使用此GM脚本进行的操作是:对于给定的网站(域名),遍历该域上的URL列表(数组)并查找感兴趣的项目.
What I'm trying to do with this GM script is: for a given website (domain name), go through a list (array) of URLs on this domain and look for items of interest.
我的代码或方法有什么问题?
What's wrong with my code or approach?
我将Greasemonkey 2.3与Firefox 33.1.1和Windows XP 32位一起使用.
I'm using Greasemonkey 2.3 with Firefox 33.1.1 and Windows XP 32-bit.
推荐答案
为了使用任何GM_
函数,必须设置匹配的 @grant
指令 (自 Greasemonkey 2.0版 )
In order to use any of the GM_
functions, you must set a matching @grant
directive (As of Greasemonkey version 2.0)
例如:
// ==UserScript==
// @name _YOUR_SCRIPT_NAME
// @include http://YOUR_SERVER.COM/YOUR_PATH/*
// @grant GM_openInTab
// ==/UserScript==
var curTab = GM_openInTab ("http://pwnthemall.com/");
请注意,这会重新打开Greasemonkey的沙箱具有副作用.另请参阅:
Note that this has the side effect of switching Greasemonkey's sandbox back on. See also:
- Error: Permission denied to access property 'handler'
- How to access `window` (Target page) objects when @grant values are set?
Tampermonkey 模仿版本3.9 .但是,如果未指定@grant
,则当前版本(3.9.202)仍会尝试猜测适当的值,因此您不一定会看到错误(尚未).
无论如何,始终使用@grant
,以实现最大的兼容性并确保您的代码将来可用.
Tampermonkey emulates most of this behavior as of version 3.9. But the current version (3.9.202) still attempts to guess appropriate values if @grant
is not specified, so you won't necessarily see an error (yet).
Always use @grant
anyway, for maximum compatibility and to future-proof your code.
这篇关于GM_openInTab(或任何其他GM_函数)未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!