ReferenceError:未定义GM_xmlhttpRequest

Ken*_*rey 10 javascript greasemonkey gm-xmlhttprequest referenceerror

我在以下用户脚本代码中得到一个ReferenceError:

// ==UserScript==
// @name          ...
// @namespace     ...
// @description   ...
// @include       ...
// @grant         GM_xmlhttpRequest
// ==/UserScript==

console.log(GM_info);
try
{
    console.log(GM_xmlhttpRequest({ method: "GET", url: "http://google.ca/", synchronous: true }).readyState);
}
catch (e)
{
    console.log(e);
}
...
Run Code Online (Sandbox Code Playgroud)

它首先GM_info成功记录,然后记录ReferenceError.(我正在使用Firefox/Firebug.)

ReferenceError:未定义GM_xmlhttpRequest

为什么我会收到此错误?

小智 7

我遇到了同样的问题,为我解决的问题是在顶部添加了此问题:

// @grant        GM_xmlhttpRequest
Run Code Online (Sandbox Code Playgroud)


A-3*_*312 6

从新闻版(GM 4.0)开始,这个错误在你使用时发生,GM_xmlhttpRequest因为GM_xmlhttpRequest被替换为 : GM.xmlHttpRequest

新代码是:

// ==UserScript==
// @name          ...
// @namespace     ...
// @description   ...
// @include       ...
// @grant         GM.xmlHttpRequest
// ==/UserScript==

console.log(GM_info);
try
{
    console.log(GM.xmlHttpRequest({ method: "GET", url: "http://google.ca/", synchronous: true }).readyState);
}
catch (e)
{
    console.log(e);
}
//...
Run Code Online (Sandbox Code Playgroud)

Greasemonkey:带有新更新的“GM_xmlhttpRequest 未定义”

  • 应该保持向后兼容性。绝对愚蠢的变化。 (2认同)

Ken*_*rey 4

重新安装脚本解决了问题。我不需要重新启动 Firefox,但这可能对其他人有帮助。布罗克的答案对此类问题提供了有用的调试技巧。