Advertisement
Guest User

Untitled

a guest
Feb 16th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.82 KB | None | 0 0
  1.  
  2. var XMLHttpRequestInternal = XMLHttpRequest;
  3. XMLHttpRequest = function() {
  4. this.onreadystatechange = function() { };
  5. this.readyState = 0;
  6. this.response = null;
  7. this.responseText = null;
  8. this.responseType = "";
  9. this.responseURL = null;
  10. this.responseXML = null;
  11. this.status = 0;
  12. this.statusText = "";
  13. this._loadcallbacks = [];
  14. this._errorcallbacks = [];
  15. this._headers = [];
  16. this.onload = function(e) {
  17.  
  18. }
  19.  
  20. this.onerror = function(e) {
  21.  
  22. }
  23.  
  24. this.onprogress = function(e) {
  25.  
  26. }
  27.  
  28. this.abort = function() {
  29. this._aborted = true;
  30. };
  31.  
  32. this.addEventListener = function(event, callback) {
  33. if(event === "load") {
  34. this._loadcallbacks.push(callback);
  35. } else if(event == "error") {
  36. this._errorcallbacks.push(callback);
  37. }
  38. };
  39.  
  40. this.getAllResponseHeaders = function() {
  41. return null;
  42. };
  43.  
  44. this.setRequestHeader = function(header, value) {
  45. this._headers.push([header, value]);
  46. }
  47.  
  48. this.open = function (method, url, async, user, password) {
  49. this._method = method;
  50. this._url = url;
  51.  
  52. if(async != null) {
  53. this._async = async;
  54. } else {
  55. this._async = true;
  56. }
  57.  
  58. this._user = user;
  59. this._password = password;
  60. };
  61.  
  62. this.overrideMimeType = function(mime_type) {
  63. this._overrideMime = mime_type;
  64. };
  65.  
  66. this.send = function(post_data) {
  67. if(!this._url) {
  68. return;
  69. }
  70.  
  71. let this_ref = this;
  72. if(this._url.includes("vod.dori") == false || true) {
  73. let xhr = new XMLHttpRequestInternal();
  74. xhr.responseType = this.responseType;
  75.  
  76. if(this._overrideMime) {
  77. xhr.overrideMimeType(this._overrideMime);
  78. }
  79.  
  80. this.abort = function() {
  81. xhr.abort();
  82. }
  83.  
  84.  
  85. this_ref.getAllResponseHeaders = function() {
  86. return xhr.getAllResponseHeaders;
  87. }
  88.  
  89. this_ref.getResponseHeader = function(name) {
  90. return xhr.getResponseHeader(name);
  91. }
  92.  
  93. xhr.onload = function() {
  94. if(xhr.readyState == 4) {
  95. this_ref.readyState = 1;
  96. this_ref.onreadystatechange();
  97. this_ref.readyState = 2;
  98. this_ref.onreadystatechange();
  99.  
  100. this_ref.status = xhr.status;
  101. this_ref.statusText = xhr.statusText;
  102.  
  103. this_ref.readyState = 3;
  104. this_ref.onreadystatechange();
  105.  
  106. this_ref.response = xhr.response;
  107. this_ref.responseText = xhr.responseText;
  108. this_ref.responseXML = xhr.responseXML;
  109. this_ref.responseURL = xhr.responseURL;
  110.  
  111. this_ref.readyState = 4;
  112. this_ref.onreadystatechange();
  113.  
  114. var event = { target: this_ref };
  115.  
  116. this_ref.onload(event);
  117. for(callback of this_ref._loadcallbacks) {
  118. callback.call(this_ref, event);
  119. }
  120. }
  121. else {
  122. this_ref.readyState = xhr.readyState;
  123. this_ref.onreadystatechange();
  124. }
  125. }
  126.  
  127. xhr.onerror = function(exception) {
  128. this_ref.onerror(exception);
  129. for(callback of this_ref._errorcallbacks) {
  130. callback.call(this_ref, exception);
  131. }
  132. }
  133.  
  134. xhr.open(this_ref._method, this_ref._url, this_ref._async, this_ref._user, this_ref._password);
  135.  
  136. let num_headers = this_ref._headers.length;
  137. for(let index = 0; index < num_headers; index += 1) {
  138. xhr.setRequestHeader(this._headers[index][0], this._headers[index][1]);
  139. }
  140.  
  141. xhr.send(post_data);
  142.  
  143. } else {
  144. let headers = [];
  145. let num_headers = this_ref._headers.length;
  146. for(let index = 0; index < num_headers; index += 1) {
  147. headers.push(this._headers[index][0] + ": ", this._headers[index][1]);
  148. }
  149.  
  150. requester.request(this._url, post_data, headers).then(function(http) {
  151. this_ref.readyState = 1;
  152. this_ref.onreadystatechange();
  153. this_ref.readyState = 2;
  154. this_ref.onreadystatechange();
  155.  
  156. this_ref.status = http.status;
  157. this_ref.statusText = http.statusText;
  158.  
  159. this_ref.readyState = 3;
  160. this_ref.onreadystatechange();
  161.  
  162. let is_xml = false;
  163. if(this_ref._overrideMime) {
  164. if(this_ref._overrideMime === "application/xml" || this_ref._overrideMime == "text/xml") {
  165. is_xml = true;
  166. }
  167. } else {
  168. let headers = http.getAllResponseHeaders().toLowerCase();
  169.  
  170. if(headers.includes("content-type: application/xml") || headers.includes("content-type: text/xml")) {
  171. is_xml = true;
  172. }
  173. }
  174.  
  175. if(is_xml) {
  176. parser = new DOMParser();
  177. this_ref.responseXML = parser.parseFromString(this_ref.responseText, "text/xml");
  178. }
  179.  
  180. this_ref.response = http.response;
  181. this_ref.responseText = http.responseText;
  182. this_ref.responseURL = this_ref._url;
  183.  
  184. this_ref.readyState = 4;
  185. this_ref.onreadystatechange();
  186.  
  187. var event = { target: this_ref };
  188.  
  189. this_ref.onload(event);
  190. for(callback of this_ref._loadcallbacks) {
  191. callback.call(this_ref, event);
  192. }
  193. }, function(exception) {
  194.  
  195. this_ref.onerror(exception);
  196. for(callback of this_ref._errorcallbacks) {
  197. callback.call(this_ref, exception);
  198. }
  199. });
  200. }
  201. };
  202. };
  203.  
  204. XMLHttpRequest.prototype = {
  205. 'UNSENT': 0,
  206. 'OPENED': 1,
  207. 'HEADERS_RECIEVED': 2,
  208. 'LOADING': 3,
  209. 'DONE': 4,
  210. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement