-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole.js
More file actions
84 lines (77 loc) · 3.53 KB
/
console.js
File metadata and controls
84 lines (77 loc) · 3.53 KB
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
// Modify the responseText and paste it into your browser console
(function() {
// create XMLHttpRequest proxy object
var oldXMLHttpRequest = XMLHttpRequest;
// define constructor for my proxy object
XMLHttpRequest = function() {
var actual = new oldXMLHttpRequest();
var self = this;
function modResponse(actualResponse) {
// Modify the response here and return it
console.log("actual response: " + actualResponse);
return actualResponse.replace('<ORIGINAL_DATA>', '<NEW_DATA>');
}
// generic function for modifying the response that can be used by
// multiple event handlers
function handleResponse(prop) {
return function() {
if (this.responseText && self[prop]) {
self.responseText = modResponse(this.responseText);
}
// call callback that was assigned on our object
if (self[prop]) {
return self[prop].apply(self, arguments);
}
}
}
function handleLoadEvent(fn, capture) {
return actual.addEventListener("load", function(e) {
if (this.responseText) {
self.responseText = modResponse(this.responseText);
}
return fn.apply(self, arguments);
}, capture);
}
// properties we don't proxy because we override their behavior
this.onreadystatechange = null;
this.responseText = null;
this.onload = null;
if (actual.addEventListener) {
this.addEventListener = function(event, fn, capture) {
if (event === "load") {
return handleLoadEvent(fn, capture);
} else {
return actual.addEventListener.apply(actual, arguments);
}
}
}
// this is the actual handler on the real XMLHttpRequest object
actual.onreadystatechange = handleResponse("onreadystatechange");
actual.onload = handleResponse("onload");
// iterate all properties in actual to proxy them according to their type
// For functions, we call actual and return the result
// For non-functions, we make getters/setters
// If the property already exists on self, then don't proxy it
for (var prop in actual) {
// skip properties we already have - this will skip both the above defined properties
// that we don't want to proxy and skip properties on the prototype belonging to Object
if (!(prop in self)) {
// create closure to capture value of prop
(function(prop) {
if (typeof actual[prop] === "function") {
// define our own property that calls the same method on the actual
Object.defineProperty(self, prop, {
value: function() {return actual[prop].apply(actual, arguments);}
});
} else {
// define our own property that just gets or sets the same prop on the actual
Object.defineProperty(self, prop, {
get: function() {return actual[prop];},
set: function(val) {actual[prop] = val;}
});
}
})(prop);
}
}
}
})();