16

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?

Kendall Frey
  • 43,130
  • 20
  • 110
  • 148
  • Can't duplicate. What are your specs? (OS, FF version, GM version, etc.) This is most likely due to an invalid metadata block. – Brock Adams May 24 '13 at 20:31
  • Win7 SP1 x64, FF 21.0, GM 1.9. The metadata block is from [this file](https://ninjaecho.codeplex.com/SourceControl/latest#ninjaecho.user.js), with only `@grant` modified. – Kendall Frey May 24 '13 at 21:53
  • Tested on same system (less SP1). No problem found. Does your metadata block have leading whitespace? Is the file encoded in anything but ANSI or UTF? Pastebin the *exact* script that duplicates the problem. Do step 4 from [this answer](http://stackoverflow.com/a/16315994/331508). – Brock Adams May 25 '13 at 00:48
  • A reinstall of the script fixed it. – Kendall Frey May 27 '13 at 13:53

3 Answers3

12

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

// @grant        GM_xmlhttpRequest
8

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

user2226755
  • 12,494
  • 5
  • 50
  • 73
  • 4
    Should have kept backwards compatibility. Absolutely moronic changes. – neverMind9 May 25 '19 at 03:02
  • GM object's methods are async (as opposed to functions containing "_"). They will not slow down the page. Sometimes there is no progress without breaking something else. Do your new scripts the new way, for old scripts GM offered fast-fix. Just add: `// @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js` [reference](https://github.com/greasemonkey/gm4-polyfill) – papo Dec 06 '19 at 16:13
4

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.

Community
  • 1
  • 1
Kendall Frey
  • 43,130
  • 20
  • 110
  • 148