forked from remy/jsconsole
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote.js
223 lines (191 loc) · 6.36 KB
/
remote.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
;(function () {
// 1. create iframe pointing to script on jsconsole.com domain
// 2. create console object with: log, dir, etc?
// 3. console.log runs postMessage with json.stringified content
// 4. jsconsole.com/remote/?id.onMessage = send to server, and wait for response.
function sortci(a, b) {
return a.toLowerCase() < b.toLowerCase() ? -1 : 1;
}
// from console.js
function stringify(o, simple) {
var json = '', i, type = ({}).toString.call(o), parts = [], names = [];
if (type == '[object String]') {
json = '"' + o.replace(/\n/g, '\\n').replace(/"/g, '\\"') + '"';
} else if (type == '[object Array]') {
json = '[';
for (i = 0; i < o.length; i++) {
parts.push(stringify(o[i], simple));
}
json += parts.join(', ') + ']';
json;
} else if (type == '[object Object]') {
json = '{';
for (i in o) {
names.push(i);
}
names.sort(sortci);
for (i = 0; i < names.length; i++) {
parts.push(stringify(names[i]) + ': ' + stringify(o[names[i] ], simple));
}
json += parts.join(', ') + '}';
} else if (type == '[object Number]') {
json = o+'';
} else if (type == '[object Boolean]') {
json = o ? 'true' : 'false';
} else if (type == '[object Function]') {
json = o.toString();
} else if (o === null) {
json = 'null';
} else if (o === undefined) {
json = 'undefined';
} else if (simple == undefined) {
json = type + '{\n';
for (i in o) {
names.push(i);
}
names.sort(sortci);
for (i = 0; i < names.length; i++) {
parts.push(names[i] + ': ' + stringify(o[names[i]], true)); // safety from max stack
}
json += parts.join(',\n') + '\n}';
} else {
try {
json = o+''; // should look like an object
} catch (e) {}
}
return json;
}
// Plugins that inject are screwing this up :(
// function getLastChild(el) {
// return (el.lastChild && el.lastChild.nodeName != '#text') ? getLastChild(el.lastChild) : el;
// }
function getRemoteScript() {
var scripts = document.getElementsByTagName('script'),
remoteScript = scripts[scripts.length-1];
for (var i = 0; i < scripts.length; i++) {
if (/jsconsole\..*(:\d+)?\/remote.js/.test(scripts[i].src)) {
remoteScript = scripts[i];
break;
}
}
return remoteScript;
}
var last = getRemoteScript();
// if (last.getAttribute('id') == '_firebugConsole') { // if Firebug is open, this all goes to crap
// last = last.previousElementSibling;
// }
var lastSrc = last.getAttribute('src'),
id = lastSrc.replace(/.*\?/, ''),
origin = 'http://' + lastSrc.substr(7).replace(/\/.*$/, ''),
remoteWindow = null,
queue = [],
msgType = '';
var remoteFrame = document.createElement('iframe');
remoteFrame.style.display = 'none';
remoteFrame.src = origin + '/remote.html?' + id;
// this is new - in an attempt to allow this code to be included in the head element
document.documentElement.appendChild(remoteFrame);
window.addEventListener('message', function (event) {
if (event.origin != origin) return;
// eval the event.data command
try {
if (event.data.indexOf('console.log') == 0) {
eval('remote.echo(' + event.data.match(/console.log\((.*)\);?/)[1] + ', "' + event.data + '", true)');
} else {
remote.echo(eval(event.data), event.data, undefined); // must be undefined to work
}
} catch (e) {
remote.error(e, event.data);
}
}, false);
var timers = {} // timers for console.time and console.timeEnd
var remote = {
log: function () {
var argsObj = stringify(arguments.length == 1 ? arguments[0] : [].slice.call(arguments, 0));
var response = [];
[].forEach.call(arguments, function (args) {
response.push(stringify(args, true));
});
var msg = JSON.stringify({ response: response, cmd: 'remote console.log', type: msgType });
if (remoteWindow) {
remoteWindow.postMessage(msg, origin);
} else {
queue.push(msg);
}
msgType = '';
},
info: function () {
msgType = 'info';
remote.log.apply(this, arguments);
},
echo: function () {
var args = [].slice.call(arguments, 0),
plain = args.pop(),
cmd = args.pop(),
response = args;
var argsObj = stringify(response, plain),
msg = JSON.stringify({ response: argsObj, cmd: cmd });
if (remoteWindow) {
remoteWindow.postMessage(msg, origin);
} else {
queue.push(msg);
}
},
error: function (error, cmd) {
var msg = JSON.stringify({ response: error.message, cmd: cmd, type: 'error' });
if (remoteWindow) {
remoteWindow.postMessage(msg, origin);
} else {
queue.push(msg);
}
},
time: function(title){
if(typeof title !== 'string') return;
timers[title] = +new Date();
},
timeEnd: function(title){
if(typeof title !== 'string' || !timers[title]) return;
var execTime = +new Date() - timers[title];
delete timers[title];
var plain = title + ": " + execTime + "ms";
var msg = JSON.stringify({ response: plain, cmd: 'remote console.log', type: '' });
if (remoteWindow) {
remoteWindow.postMessage(msg, origin);
} else {
queue.push(msg);
}
}
};
// just for extra support
remote.debug = remote.dir = remote.log;
remote.warn = remote.info;
remoteFrame.onload = function () {
remoteWindow = remoteFrame.contentWindow;
remoteWindow.postMessage('__init__', origin);
remoteWindow.postMessage(stringify({ response: 'Connection established with ' + window.location.toString() + '\n' + navigator.userAgent, type: 'info' }), origin);
for (var i = 0; i < queue.length; i++) {
remoteWindow.postMessage(queue[i], origin);
}
};
window.remote = remote;
window.addEventListener && window.addEventListener('error', function (event) {
remote.error({ message: event.message }, event.filename + ':' + event.lineno);
}, false);
try {
window.console = remote;
} catch (e) {
console.log('cannot overwrite existing console object');
}
function warnUsage() {
var useSS = false;
try {
sessionStorage.getItem('foo');
useSS = true;
} catch (e) {}
if (!(useSS ? sessionStorage.jsconsole : window.name)) {
if (useSS) sessionStorage.jsconsole = 1; else window.name = 1;
alert('You will see this warning once per session.\n\nYou are using a remote control script on this site - if you accidently push it to production, anyone will have control of your visitor\'s browser. Remember to remove this script.');
}
}
warnUsage();
})();