w35l3y

XMLHttpRequest

May 17th, 2014
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Made from scratch based on https://github.com/ShiftSpace/gmrequest/blob/master/GMRequest.js
  2.  
  3. XMLHttpRequest = function () {
  4.     var _params = {
  5.         context : this,
  6.         headers : {},
  7.         onreadystatechange  : function (detail) {
  8.             this.status = detail.status;
  9.             this.statusText = detail.statusText;
  10.             this.responseHeaders = detail.responseHeaders;
  11.             this.responseText = detail.responseText;
  12.             this.readyState = detail.readyState;
  13.  
  14.             this.onreadystatechange && this.onreadystatechange();
  15.         }.bind(this),
  16.     },
  17.     _exec,
  18.     _send = function (data, binary) {
  19.         var t = {
  20.             data    : data,
  21.             binary  : binary,
  22.         };
  23.         for (var key in _params) {
  24.             t[key] = _params[key];
  25.         }
  26.         for (var key in this) {
  27.             if (!(key in t)) {
  28.                 if (this[key] instanceof Function) {
  29.                     t[key] = function (xhr) {
  30.                         this[key].call(this, xhr);
  31.                     }.bind(this);
  32.                 } else {
  33.                     t[key] = this[key];
  34.                 }
  35.             }
  36.         }
  37.  
  38.         _exec = GM_xmlhttpRequest(t);
  39.     }.bind(this);
  40.    
  41.     this.setRequestHeader = function (key, value) {
  42.         _params.headers[key] = value;
  43.     };
  44.  
  45.     this.getResponseHeader = function (key) {
  46.         return this.responseHeaders[key];
  47.     };
  48.    
  49.     this.getAllResponseHeaders = function () {
  50.         return this.responseHeaders;
  51.     };
  52.    
  53.     this.overrideMimeType = function (value) {
  54.         _params.overrideMimeType = value;
  55.     };
  56.    
  57.     this.open = function (_method, _url, _async, _user, _passw) {
  58.         this.abort();
  59.  
  60.         _params.method = _method.toUpperCase();
  61.         _params.url = _url;
  62.         _params.synchronous = !_async;
  63.         _params.user = _user;
  64.         _params.password = _passw;
  65.     };
  66.    
  67.     this.abort = function () {
  68.         _exec && _exec.abort();
  69.     }
  70.  
  71.     this.send = function (data) {
  72.         _send(data, false);
  73.     };
  74.  
  75.     this.sendAsBinary = function (data) {
  76.         _send(data, true);
  77.     }
  78. };
Advertisement
Add Comment
Please, Sign In to add comment