-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxhrm.js
74 lines (71 loc) · 2.59 KB
/
xhrm.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var XHRM = {
counter : 0,
log : {},
init: function () {
// initialization function
if(typeof XMLHttpRequest != "undefined") {
this.xhrattach(XMLHttpRequest.prototype.open);
}
},
xhrattach: function (open) {
// wrap the prototype.open method in our own method
XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {
XHRM.counter++;
if(typeof this.addEventListener == "function") {
try {
this.tempMetrics = XHRM.log;
this.tempMetrics[XHRM.counter] = {
'url': "",
'method': "",
'async': "",
'start': 0,
'connect': 0,
'received': 0,
'processing': 0,
'ready': 0,
'total': 0,
'status': "",
'text': "",
'size': 0,
'complete': false
};
this.tempMetrics[XHRM.counter]['url'] = url;
this.tempMetrics[XHRM.counter]['method'] = method;
this.tempMetrics[XHRM.counter]['async'] = async;
this.tempMetrics[XHRM.counter]['start'] = new Date().getTime();
// add an Event handler for every readystatechange event
this.addEventListener("readystatechange", function () {
switch(this.readyState)
{
case 0:
// this never get called
this.tempMetrics[XHRM.counter]['start'] = new Date().getTime();
break;
case 1:
this.tempMetrics[XHRM.counter]['connect'] = new Date().getTime();
break;
case 2:
this.tempMetrics[XHRM.counter]['received'] = new Date().getTime();
break;
case 3:
this.tempMetrics[XHRM.counter]['processing'] = new Date().getTime();
break;
case 4:
this.tempMetrics[XHRM.counter]['ready'] = new Date().getTime();
this.tempMetrics[XHRM.counter]['status'] = this.status;
this.tempMetrics[XHRM.counter]['text'] = this.statusText;
this.tempMetrics[XHRM.counter]['size'] = this.responseText.toString().length;
this.tempMetrics[XHRM.counter]['total'] = this.tempMetrics[XHRM.counter]['ready'] - this.tempMetrics[XHRM.counter]['start'];
this.tempMetrics[XHRM.counter]['complete'] = true;
break;
}
}, false);
}
catch(err) {
document.getElementById("errors").innerHTML = err.message;
}
}
return open.apply(this, [method, url, async, user, pass]);
};
}
};