Advertisement
Guest User

Untitled

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