greasemonkey jquery and xmlhttprequest together

by Martin Monperrus Tags:

This post shows that one can use all AJAX features of JQuery in Greasemonkey. It is an update of the excellent post of Ryan Greenberg ([[http://ryangreenberg.com/archives/2010/03/greasemonkey_jquery.php]]).

In Greasemonkey, there is a [[http://wiki.greasespot.net/GM_xmlhttpRequest|function called GM_xmlhttpRequest for XHR]]. However, it does not comply with the XmlHttprequest interface. Hence, it is not possible to use it with JQuery. Ryan Greenberg made a first wrapper of xmlhttpRequest compatible with JQuery. I updated it in order to work with JQuery 1.5.:

JQuery AJAX in Greasemonley then looks like:
$.ajax({
  url: '/p/',// this even works for cross-domain requests by default
  xhr: function(){return new GM_XHR();},
  type: 'POST',
  success: function(val){
  .... 
  }
});

===== The script =====

Main changes compared to Ryan’s implementation: - implemented getAllResponseHeaders - removed onreadystatechange on GM_xmlhttpRequest which is useless