Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var XMLHttpRequestInternal = XMLHttpRequest;
- XMLHttpRequest = function() {
- this.onreadystatechange = function() { };
- this.readyState = 0;
- this.response = null;
- this.responseText = null;
- this.responseType = "";
- this.responseURL = null;
- this.responseXML = null;
- this.status = 0;
- this.statusText = "";
- this.timeout = 0;
- this._loadcallbacks = [];
- this._errorcallbacks = [];
- this._progresscallbacks = [];
- this._loadstartcallbacks = [];
- this._loadendcallbacks = [];
- this._timeoutcallbacks = [];
- this._abortcallbacks = [];
- this._headers = [];
- this.onload = function(e) {
- }
- this.onerror = function(e) {
- }
- this.onprogress = function(e) {
- }
- this.onloadstart = function(e) {
- }
- this.onloadend = function(e) {
- }
- this.ontimeout = function(e) {
- }
- this.onabort = function(e) {
- }
- this.abort = function() {
- if(this._aborted == false) {
- this._aborted = true;
- }
- };
- this.addEventListener = function(event, callback) {
- if(event === "load") {
- this._loadcallbacks.push(callback);
- } else if(event == "error") {
- this._errorcallbacks.push(callback);
- } else if(event == "progress") {
- this._progresscallbacks.push(callback);
- } else if(event == "loadstart") {
- this._loadstartcallbacks.push(callback);
- } else if(event == "loadend") {
- this._loadendcallbacks.push(callback);
- } else if(event == "timeout") {
- this._timeoutcallbacks.push(callback);
- } else if(event == "abort") {
- this._abortcallbacks.push(callback);
- } else {
- console.log("Unknown event");
- debugger;
- }
- };
- this.getAllResponseHeaders = function() {
- return null;
- };
- this.setRequestHeader = function(header, value) {
- this._headers.push([header, value]);
- }
- this.open = function (method, url, async, user, password) {
- this._method = method;
- this._url = url;
- if(async != null) {
- this._async = async;
- } else {
- this._async = true;
- }
- this._user = user;
- this._password = password;
- };
- this.overrideMimeType = function(mime_type) {
- this._overrideMime = mime_type;
- };
- this.send = function(post_data) {
- if(!this._url) {
- return;
- }
- let this_ref = this;
- if((this._url.includes("vod.dori") == false && this._url.includes("video.dori") == false) || this._async == false) {
- let xhr = new XMLHttpRequestInternal();
- xhr.responseType = this.responseType;
- if(this._overrideMime) {
- xhr.overrideMimeType(this._overrideMime);
- }
- this.abort = function() {
- xhr.abort();
- }
- this_ref.getAllResponseHeaders = function() {
- return xhr.getAllResponseHeaders();
- }
- this_ref.getResponseHeader = function(name) {
- return xhr.getResponseHeader(name);
- }
- var event = { target: this_ref };
- for(callback of this_ref._loadstartcallbacks) {
- callback.call(this_ref);
- }
- this_ref.onloadstart(event);
- xhr.onload = function(event) {
- if(xhr.readyState == 4) {
- this_ref.readyState = 1;
- this_ref.onreadystatechange.call(this_ref, event);
- this_ref.readyState = 2;
- this_ref.onreadystatechange.call(this_ref, event);
- this_ref.status = xhr.status;
- this_ref.statusText = xhr.statusText;
- this_ref.readyState = 3;
- this_ref.onreadystatechange.call(this_ref, event);
- this_ref.response = xhr.response;
- this_ref.responseText = xhr.responseText;
- this_ref.responseXML = xhr.responseXML;
- this_ref.responseURL = xhr.responseURL;
- this_ref.readyState = 4;
- this_ref.onreadystatechange.call(this_ref, event);
- this_ref.onload(event);
- for(callback of this_ref._loadcallbacks) {
- callback.call(this_ref, event);
- }
- this_ref.onloadend(event);
- for(callback of this_ref._loadendcallbacks) {
- callback.call(this_ref, event);
- }
- }
- else {
- this_ref.readyState = xhr.readyState;
- this_ref.onreadystatechange.call(this_ref, event);
- }
- }
- xhr.onerror = function(exception) {
- this_ref.onerror(exception);
- for(callback of this_ref._errorcallbacks) {
- callback.call(this_ref, exception);
- }
- this_ref.onloadend(exception);
- for(callback of this_ref._loadendcallbacks) {
- callback.call(this_ref, exception);
- }
- }
- xhr.onprogress = function(event) {
- this_ref.onprogress(event);
- for(callback of this_ref._progresscallbacks) {
- callback.call(this_ref, event);
- }
- }
- xhr.ontimeout = function(event) {
- this_ref.ontimeout(event);
- for(callback of this_ref._timeoutcallbacks) {
- callback.call(this_ref, event);
- }
- }
- xhr.onabort = function(event) {
- this_ref.onabort(event);
- for(callback of this_ref._abortcallbacks) {
- callback.call(this_ref, event);
- }
- }
- xhr.open(this_ref._method, this_ref._url, this_ref._async, this_ref._user, this_ref._password);
- let num_headers = this_ref._headers.length;
- for(let index = 0; index < num_headers; index += 1) {
- xhr.setRequestHeader(this._headers[index][0], this._headers[index][1]);
- }
- if(this_ref._async) {
- xhr.timeout = this_ref.timeout;
- }
- xhr.send(post_data);
- if(!this_ref._async) {
- this_ref.status = xhr.status;
- this_ref.statusText = xhr.statusText;
- this_ref.response = xhr.response;
- this_ref.responseText = xhr.responseText;
- this_ref.responseXML = xhr.responseXML;
- this_ref.responseURL = xhr.responseURL;
- }
- } else {
- var link = document.createElement("a");
- link.href = this._url;
- let url = link.protocol+"//"+link.host+link.pathname+link.search+link.hash;
- if(!this_ref._async) {
- console.log("XHR attempting to do a synchronous request but webrtc doesn't support it");
- debugger;
- }
- let has_content_type = false;
- let headers = [];
- let num_headers = this_ref._headers.length;
- for(let index = 0; index < num_headers; index += 1) {
- headers.push(this._headers[index][0] + ": ", this._headers[index][1]);
- if(this._headers[index][0].toLowerCase() == "content-type") {
- has_content_type = false;
- }
- }
- if(has_content_type == false) {
- if(typeof(post_data) === "string") {
- headers.push("Content-Type: application/text");
- }
- }
- var event = { target: this_ref, currentTarget: this_ref, type: "loadstart" };
- for(callback of this_ref._loadstartcallbacks) {
- callback.call(this_ref);
- }
- this_ref.onloadstart(event);
- requester.request(this_ref._method, url, post_data, headers).then(function(http) {
- this_ref.readyState = 1;
- this_ref.onreadystatechange.call(this_ref, event);
- this_ref.readyState = 2;
- this_ref.onreadystatechange.call(this_ref, event);
- this_ref.status = http.status;
- this_ref.statusText = http.statusText;
- this_ref.readyState = 3;
- this_ref.onreadystatechange.call(this_ref, event);
- let is_xml = false;
- if(this_ref._overrideMime) {
- if(this_ref._overrideMime === "application/xml" || this_ref._overrideMime == "text/xml") {
- is_xml = true;
- }
- } else {
- let headers = http.getAllResponseHeaders().toLowerCase();
- if(headers.includes("content-type: application/xml") || headers.includes("content-type: text/xml")) {
- is_xml = true;
- }
- }
- if(is_xml) {
- parser = new DOMParser();
- this_ref.responseXML = parser.parseFromString(this_ref.responseText, "text/xml");
- }
- this_ref.getAllResponseHeaders = function() { http.getAllResponseHeaders(); };
- this_ref.getResponseHeader = function(get_header)
- {
- let header_str = http.getAllResponseHeaders().toLowerCase();
- let headers = header_str.split("\n");
- let test_header = get_header.toLowerCase();
- for(header of headers) {
- let data = header.split(":");
- if(data[0] == test_header) {
- return data[1].trim();
- }
- }
- return null;
- };
- this_ref.response = http.response;
- this_ref.responseText = http.responseText;
- this_ref.responseURL = this_ref._url;
- this_ref.readyState = 4;
- this_ref.onreadystatechange.call(this_ref, event);
- event.type = "progress";
- event.lengthComputable = true;
- event.loaded = this_ref.response.size;
- event.total = this_ref.response.size;
- this_ref.onprogress(event);
- for(callback of this_ref._loadcallbacks) {
- callback.call(this_ref, event);
- }
- event.type = "load";
- this_ref.onload(event);
- for(callback of this_ref._loadcallbacks) {
- callback.call(this_ref, event);
- }
- event.type = "loadend";
- this_ref.onloadend(event);
- for(callback of this_ref._loadendcallbacks) {
- callback.call(this_ref, event);
- }
- }, function(exception) {
- this_ref.onerror(exception);
- for(callback of this_ref._errorcallbacks) {
- callback.call(this_ref, exception);
- }
- this_ref.onloadend(exception);
- for(callback of this_ref._loadendcallbacks) {
- callback.call(this_ref, exception);
- }
- });
- }
- };
- };
- XMLHttpRequest.prototype = {
- 'UNSENT': 0,
- 'OPENED': 1,
- 'HEADERS_RECIEVED': 2,
- 'LOADING': 3,
- 'DONE': 4,
- };
Add Comment
Please, Sign In to add comment