2

Why this simple Greasemonkey script is not working for me https://jsfiddle.net/pghnsw8z/1/ ? I mean that instead of getting successful response I get error while making an ajax call.

// ==UserScript==
// @name        _Starter AJAX request in GM, TM, etc.
// @match       *://php.net/*
// @grant       GM_xmlhttpRequest
// @connect     php.net
// ==/UserScript==

GM_xmlhttpRequest ( {
    method:     'GET',
    url:        'http://php.net/',
    onload:     function (responseDetails) {
                    // DO ALL RESPONSE PROCESSING HERE...
                                alert(responseDetails);
                    console.log (
                        "GM_xmlhttpRequest() response is:\n",
                        responseDetails.responseText.substring (0, 80) + '...'
                    );
                }
} );

I found the script here https://stackoverflow.com/a/42592356/9483949 and it seems it worked well for someone earlier.

I'm using Firefox 59.0.1 and Greasemonkey 4.3

Restarting Firefox and reinstalling script didn't help.

user2226755
  • 12,494
  • 5
  • 50
  • 73
Peter
  • 21
  • 1
  • 3
  • 2
    GM4 has abandoned the classic API, see their documentation, now it's GM.* – wOxxOm Mar 19 '18 at 15:17
  • @wOxxOm In this case I get `GM.xmlhttpRequest is not a function` – Peter Mar 19 '18 at 15:39
  • 1
    The new API function uses uppercase H. – wOxxOm Mar 19 '18 at 16:14
  • @wOxxOm Perfect! thank's a lot. – Peter Mar 19 '18 at 16:20
  • 1
    Peter, as per GM's own recommendation, you should switch to Tampermonkey or Violentmonkey. The new Greasemonkey is severely degraded, not backwards compatible, and has significantly less utility than the other engines. – Brock Adams Mar 19 '18 at 18:56
  • @Brock Adams Thank's for pointing that out for me. – Peter Mar 20 '18 at 11:14
  • use GM.* in new scripts, add [polyfill](https://github.com/greasemonkey/gm4-polyfill) to old scripts. One line, too easy. A suggestion for users (just users) if a script was not updated by one of these methods is that [they might to switch to tampermonkey meanwhile](https://www.greasespot.net/2017/09/greasemonkey-4-for-users.html). GM is not dying. Why is not GM adding the polyfill automatically? Maybe wanted to force the change. – papo Dec 06 '19 at 16:16

1 Answers1

4

The doc : https://wiki.greasespot.net/GM.xmlHttpRequest

The GM API was changed. You have to use xmlHttpRequest property of GM class, it is compatibility: GM 4.0+.

Replace GM_xmlhttpRequest by : GM.xmlHttpRequest like this :

// ==UserScript==
// ...
// @grant         GM.xmlHttpRequest
// ==/UserScript==

GM.xmlHttpRequest({
user2226755
  • 12,494
  • 5
  • 50
  • 73