-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXMLHttpRequest.js
55 lines (48 loc) · 1.25 KB
/
XMLHttpRequest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
(function(global){
var XMLHttpRequestOriginal = global.XMLHttpRequest;
var XMLHttpRequestShim = function(){
var xhr = new XMLHttpRequestOriginal();
var events = {};
addAddEventListener(xhr, events);
return xhr;
};
function isProperty(propName, value){
return (
(Object.prototype.hasOwnProperty.call(value, propName))
||
(propName in value)
);
}
function addAddEventListener(xhr, events){
if(!xhr.addEventListener){
xhr.addEventListener = function(eventName, callback, bubble){
if(!isProperty(eventName, events)){
events[eventName] = [];
}
events[eventName].push(callback);
xhr.onreadystatechange = function(){
if(this.readyState === 4){
if(this.status === 200){
if(isProperty("load", events)){
events.load.forEach(function(callback){
callback({
"target": this
});
}, this);
}
}else if(this.status > 399){
if(isProperty("error", events)){
events.load.forEach(function(callback){
callback({
"target": this
});
}, this);
}
}
}
}.bind(this);
}.bind(xhr);
}
}
global.XMLHttpRequest = XMLHttpRequestShim
})(Function('return this')() || (42, eval)('this'));