-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxhr-x.js
116 lines (91 loc) · 3.37 KB
/
xhr-x.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
(function () {
'use strict';
// BEGIN Event declarations
class XHREvent {
constructor(context) {
this.context = context;
this.cancelled = false;
}
}
class XHROpenEvent extends XHREvent {
constructor(context, state) {
super(context);
this.state = state;
}
}
class XHRHeaderSetEvent extends XHREvent {
constructor(context, key, value) {
super(context);
this.key = key;
this.value = value;
}
}
class XHRSendEvent extends XHREvent {
constructor(context, state) {
super(context);
this.state = state;
}
}
class XHRFinishedEvent extends XHREvent {
constructor(context) {
super(context);
}
}
// END Event declarations
const XHRExt = {
openHandlers: [],
headerSetHandlers: [],
sendHandlers: [],
finishHandlers: []
};
window.XHRExt = XHRExt;
// BEGIN Prototype hacking
const xhr_open = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function () {
this.xMethod = arguments[0];
this.xUrl = arguments[1];
this.xAsync = arguments[2] || false;
this.xUsername = arguments[3];
this.xPassword = arguments[4];
let xhrEvent = new XHROpenEvent(this, "before");
XHRExt.openHandlers.forEach(handler => handler.call(null, xhrEvent));
if (xhrEvent.cancelled) return;
arguments[0] = this.xMethod;
arguments[1] = this.xUrl;
arguments[2] = this.xAsync;
arguments[3] = this.xUsername;
arguments[4] = this.xPassword;
xhr_open.apply(this, arguments);
XHRExt.openHandlers.forEach(handler => handler.call(null, new XHROpenEvent(this, "after")));
};
const xhr_setRequestHeader = XMLHttpRequest.prototype.setRequestHeader;
XMLHttpRequest.prototype.setRequestHeader = function () {
if (this.xHeaders == null) this.xHeaders = [];
this.xHeaders[arguments[0]] = arguments[1];
let xhrEvent = new XHRHeaderSetEvent(this, arguments[0], arguments[1]);
XHRExt.headerSetHandlers.forEach(handler => handler.call(null, xhrEvent));
if (xhrEvent.cancelled) return;
this.xHeaders[xhrEvent.key] = xhrEvent.value;
arguments[0] = xhrEvent.key;
arguments[1] = xhrEvent.value;
xhr_setRequestHeader.apply(this, arguments);
};
const xhr_send = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.send = function () {
this.xBody = arguments[0];
let xhrEvent = new XHRSendEvent(this, "before");
XHRExt.sendHandlers.forEach(handler => handler.call(null, xhrEvent));
if (xhrEvent.cancelled) return;
arguments[0] = this.xBody;
const xhr_onreadystatechange = this.onreadystatechange;
this.onreadystatechange = function () {
if (this.readyState === XMLHttpRequest.DONE) {
XHRExt.finishHandlers.forEach(handler => handler.call(null, new XHRFinishedEvent(this)));
}
if (xhr_onreadystatechange != null) xhr_onreadystatechange.apply(this, arguments);
};
xhr_send.apply(this, arguments);
XHRExt.sendHandlers.forEach(handler => handler.call(null, new XHRSendEvent(this, "after")));
};
// END Prototype hacking
})();