Advertisement
Guest User

GM_JQUERY_AJAX

a guest
Nov 1st, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // allows using all Jquery AJAX methods in Greasemonkey
  2. // inspired from http://ryangreenberg.com/archives/2010/03/greasemonkey_jquery.php
  3. // works with JQuery 1.5
  4. // (c) 2011 Martin Monperrus
  5. // (c) 2010 Ryan Greenberg
  6. //
  7. // Usage:
  8. //   $.ajax({
  9. //     url: '/p/',
  10. //     xhr: function(){return new GM_XHR();},
  11. //     type: 'POST',
  12. //     success: function(val){
  13. //        ....
  14. //     }
  15. //   });
  16. function GM_XHR() {
  17.     this.type = null;
  18.     this.url = null;
  19.     this.async = null;
  20.     this.username = null;
  21.     this.password = null;
  22.     this.status = null;
  23.     this.headers = {};
  24.     this.readyState = null;
  25.  
  26.     this.abort = function() {
  27.         this.readyState = 0;
  28.     };
  29.  
  30.     this.getAllResponseHeaders = function(name) {
  31.       if (this.readyState!=4) return "";
  32.       return this.responseHeaders;
  33.     };
  34.  
  35.     this.getResponseHeader = function(name) {
  36.       var regexp = new RegExp('^'+name+': (.*)$','im');
  37.       var match = regexp.exec(this.responseHeaders);
  38.       if (match) { return match[1]; }
  39.       return '';
  40.     };
  41.  
  42.     this.open = function(type, url, async, username, password) {
  43.         this.type = type ? type : null;
  44.         this.url = url ? url : null;
  45.         this.async = async ? async : null;
  46.         this.username = username ? username : null;
  47.         this.password = password ? password : null;
  48.         this.readyState = 1;
  49.     };
  50.    
  51.     this.setRequestHeader = function(name, value) {
  52.         this.headers[name] = value;
  53.     };
  54.  
  55.     this.send = function(data) {
  56.         this.data = data;
  57.         var that = this;
  58.         // http://wiki.greasespot.net/GM_xmlhttpRequest
  59.         GM_xmlhttpRequest({
  60.             method: this.type,
  61.             url: this.url,
  62.             headers: this.headers,
  63.             data: this.data,
  64.             onload: function(rsp) {
  65.                 // Populate wrapper object with returned data
  66.                 // including the Greasemonkey specific "responseHeaders"
  67.                 for (var k in rsp) {
  68.                     that[k] = rsp[k];
  69.                 }
  70.                 // now we call onreadystatechange
  71.                 that.onreadystatechange();
  72.             },
  73.             onerror: function(rsp) {
  74.                 for (var k in rsp) {
  75.                     that[k] = rsp[k];
  76.                 }
  77.             }
  78.         });
  79.     };
  80. };
  81. $.ajaxSetup({
  82.     xhr: function(){return new GM_XHR;}
  83. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement