问题
When my GM script does this:
var curTab = GM_openInTab(url);
it results in a 'GM_openInTab is not defined'
JavaScript error in the Browser Console.
I also tried using var curWin = window.open(url);
instead of GM_openInTab
but it had no affect.
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?
I'm using Greasemonkey 2.3 with Firefox 33.1.1 and Windows XP 32-bit.
回答1:
In order to use any of the GM_
functions, you must set a matching @grant directiveDoc (As of Greasemonkey version 2.0Release notes)
For example:
// ==UserScript==
// @name _YOUR_SCRIPT_NAME
// @include http://YOUR_SERVER.COM/YOUR_PATH/*
// @grant GM_openInTab
// ==/UserScript==
var curTab = GM_openInTab ("http://pwnthemall.com/");
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 emulates most of this behavior as of version 3.9Release notes. 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.
来源:https://stackoverflow.com/questions/28268847/gm-openintab-or-any-other-gm-function-is-not-defined