Advertisement
Guest User

Untitled

a guest
Feb 15th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1.  
  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.onload = function(e) {
  15.  
  16. }
  17.  
  18. this.onerror = function(e) {
  19.  
  20. }
  21.  
  22. this.onprogress = function(e) {
  23.  
  24. }
  25.  
  26. this.abort = function() {
  27. this._aborted = true;
  28. };
  29.  
  30. this.addEventListener = function(event, callback) {
  31. if(event === "load") {
  32. this._loadcallbacks.push(callback);
  33. } else if(event == "error") {
  34. this._errorcallbacks.push(callback);
  35. }
  36. };
  37.  
  38. this.getAllResponseHeaders = function() {
  39. return null;
  40. };
  41.  
  42. this.open = function (method, url, async, user, password) {
  43. this._method = method;
  44. this._url = url;
  45. this._user = user;
  46. this._password = password;
  47. };
  48.  
  49. this.overrideMimeType = function(mime_type) {
  50. this._overrideMime = mime_type;
  51. };
  52.  
  53. this.send = function() {
  54. if(!this._url) {
  55. return;
  56. }
  57.  
  58. let this_ref = this;
  59. requester.request(this._url).then(function(http) {
  60. this_ref.readyState = 1;
  61. this_ref.onreadystatechange();
  62. this_ref.readyState = 2;
  63. this_ref.onreadystatechange();
  64. this_ref.readyState = 3;
  65. this_ref.onreadystatechange();
  66.  
  67. this_ref.response = http.response;
  68. this_ref.responseText = http.responseText;
  69. this_ref.responseURL = this_ref._url;
  70. this_ref.status = http.status;
  71. this_ref.statusText = http.statusText;
  72.  
  73. let is_xml = false;
  74. if(this_ref._overrideMime) {
  75. if(this_ref._overrideMime === "application/xml" || this_ref._overrideMime == "text/xml") {
  76. is_xml = true;
  77. }
  78. } else {
  79. let headers = http.getAllResponseHeaders().toLowerCase();
  80.  
  81. if(headers.includes("content-type: application/xml") || headers.includes("content-type: text/xml")) {
  82. is_xml = true;
  83. }
  84. }
  85.  
  86. if(is_xml) {
  87. parser = new DOMParser();
  88. this_ref.responseXML = parser.parseFromString(this_ref.responseText, "text/xml");
  89. }
  90.  
  91. this_ref.readyState = 4;
  92. this_ref.onreadystatechange();
  93.  
  94. this_ref.onload(http);
  95. for(callback of this_ref._loadcallbacks) {
  96. callback.call(this_ref, http);
  97. }
  98. }, function(exception) {
  99.  
  100. this_ref.onerror(exception);
  101. for(callback of this_ref._errorcallbacks) {
  102. callback.call(this_ref, exception);
  103. }
  104. });
  105. };
  106. };
  107.  
  108. XMLHttpRequest.prototype = {
  109. 'UNSENT': 0,
  110. 'OPENED': 1,
  111. 'HEADERS_RECIEVED': 2,
  112. 'LOADING': 3,
  113. 'DONE': 4,
  114. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement