ReferenceError: GM_xmlhttpRequest is not defined

北战南征 提交于 2019-12-09 15:21:26

问题


I get a ReferenceError in the following userscript code:

// ==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);
}
...

It first logs GM_info successfully, then logs the ReferenceError. (I'm using Firefox/Firebug.)

ReferenceError: GM_xmlhttpRequest is not defined

Why do I get this error?


回答1:


I had the same problem, and what fixed it for me was adding this at the top:

// @grant        GM_xmlhttpRequest



回答2:


Reinstalling the script fixed the problem. I didn't need to restart Firefox, but it may be helpful for other people. Brock's answer has helpful debugging tips for problems like this.




回答3:


Since the news version (GM 4.0) this error happened when you use GM_xmlhttpRequest because GM_xmlhttpRequest was replaced by : GM.xmlHttpRequest.

The new code is :

// ==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);
}
//...

Greasemonkey: "GM_xmlhttpRequest is not defined" with the new update



来源:https://stackoverflow.com/questions/16736320/referenceerror-gm-xmlhttprequest-is-not-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!