Advertisement
Guest User

Untitled

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