Advertisement
Guest User

Untitled

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