forked from chinchang/web-maker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreenlog.js
More file actions
279 lines (251 loc) · 6.6 KB
/
screenlog.js
File metadata and controls
279 lines (251 loc) · 6.6 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
let mainWindow = window.parent;
function sanitizeDomNode(node) {
const fakeNode = {
nodeName: node.nodeName,
nodeType: node.nodeType,
tagName: node.tagName,
childNodes: [...node.childNodes].map(child => sanitizeDomNode(child)),
textContent: node.textContent
};
if (node.attributes) {
fakeNode.attributes = [...node.attributes].map(attribute => ({
name: attribute.name,
value: attribute.value
}));
}
return fakeNode;
}
function sanitizeEvent(event) {
const fakeEvent = {};
// Doing this still does't show class name in inspector
// fakeEvent.__proto__.constructor = event.constructor
function isSerializable(thing) {
try {
JSON.stringify(thing);
} catch (e) {
return false;
}
return true;
}
const prohibitedKeys = ['srcElement', 'sourceCapabilities'];
for (var key in event) {
if (typeof event[key] === 'function' || prohibitedKeys.includes(key)) {
continue;
}
if (
event[key] instanceof HTMLElement ||
event[key] instanceof HTMLHtmlElement
) {
fakeEvent[key] = sanitizeDomNode(event[key]);
continue;
} else if (event[key] instanceof Window) {
fakeEvent[key] = 'Window';
continue;
} else if (event[key] instanceof Document) {
fakeEvent[key] = 'Document';
continue;
}
if (isSerializable(event[key])) {
fakeEvent[key] = event[key];
}
}
return fakeEvent;
}
function sendLog(...args) {
const sanitizedArgs = [...args].map(arg => {
if (arg && arg instanceof HTMLElement) {
return sanitizeDomNode(arg);
} else if (arg && arg instanceof Event) {
return sanitizeEvent(arg);
}
return arg;
});
mainWindow.postMessage({ logs: sanitizedArgs }, '*');
}
(function() {
var logEl,
isInitialized = false,
_console = {}; // backup console obj to contain references of overridden fns.
_options = {
bgColor: 'black',
logColor: 'lightgreen',
infoColor: 'blue',
warnColor: 'orange',
errorColor: 'red',
freeConsole: false,
css: '',
autoScroll: true,
proxyCallback: null,
noUi: false
};
function createElement(tag, css) {
var element = document.createElement(tag);
element.style.cssText = css;
return element;
}
function createPanel() {
var div = createElement(
'div',
'z-index:2147483647;font-family:Helvetica,Arial,sans-serif;font-size:10px;font-weight:bold;padding:5px;text-align:left;opacity:0.8;position:fixed;right:0;top:0;min-width:200px;max-height:50vh;overflow:auto;background:' +
_options.bgColor +
';' +
_options.css
);
return div;
}
function genericLogger(color) {
return function() {
if (_options.proxyCallback) {
_options.proxyCallback.apply(null, arguments);
}
if (_options.noUi) {
return;
}
var el = createElement(
'div',
'line-height:18px;min-height:18px;background:' +
(logEl.children.length % 2 ? 'rgba(255,255,255,0.1)' : '') +
';color:' +
color
); // zebra lines
var val = [].slice.call(arguments).reduce(function(prev, arg) {
return (
prev + ' ' + (typeof arg === 'object' ? JSON.stringify(arg) : arg)
);
}, '');
el.textContent = val;
logEl.appendChild(el);
// Scroll to last element, if autoScroll option is set.
if (_options.autoScroll) {
logEl.scrollTop = logEl.scrollHeight - logEl.clientHeight;
}
};
}
function clear() {
if (_options.noUi) {
mainWindow.clearConsole();
return;
}
logEl.innerHTML = '';
}
function log() {
return genericLogger(_options.logColor).apply(null, arguments);
}
function info() {
return genericLogger(_options.infoColor).apply(null, arguments);
}
function warn() {
return genericLogger(_options.warnColor).apply(null, arguments);
}
function error() {
return genericLogger(_options.errorColor).apply(null, arguments);
}
function setOptions(options) {
for (var i in options)
if (options.hasOwnProperty(i) && _options.hasOwnProperty(i)) {
_options[i] = options[i];
}
}
function init(options) {
if (isInitialized) {
return;
}
isInitialized = true;
if (options) {
setOptions(options);
}
if (!_options.noUi) {
logEl = createPanel();
document.body.appendChild(logEl);
}
if (!_options.freeConsole) {
// Backup actual fns to keep it working together
_console.log = console.log;
_console.clear = console.clear;
_console.info = console.info;
_console.warn = console.warn;
_console.error = console.error;
console.log = originalFnCallDecorator(log, 'log');
console.clear = originalFnCallDecorator(clear, 'clear');
console.info = originalFnCallDecorator(info, 'info');
console.warn = originalFnCallDecorator(warn, 'warn');
console.error = originalFnCallDecorator(error, 'error');
}
}
function destroy() {
isInitialized = false;
console.log = _console.log;
console.clear = _console.clear;
console.info = _console.info;
console.warn = _console.warn;
console.error = _console.error;
logEl.remove();
}
/**
* Checking if isInitialized is set
*/
function checkInitialized() {
if (!isInitialized) {
throw 'You need to call `screenLog.init()` first.';
}
}
/**
* Decorator for checking if isInitialized is set
* @param {Function} fn Fn to decorate
* @return {Function} Decorated fn.
*/
function checkInitDecorator(fn) {
return function() {
checkInitialized();
return fn.apply(this, arguments);
};
}
/**
* Decorator for calling the original console's fn at the end of
* our overridden fn definitions.
* @param {Function} fn Fn to decorate
* @param {string} fn Name of original function
* @return {Function} Decorated fn.
*/
function originalFnCallDecorator(fn, fnName) {
return function() {
fn.apply(this, arguments);
if (typeof _console[fnName] === 'function') {
_console[fnName].apply(console, arguments);
}
};
}
window.addEventListener('error', function() {
genericLogger(_options.errorColor).call(null, arguments[0].error.stack);
});
// Public API
window.screenLog = {
init: init,
log: originalFnCallDecorator(checkInitDecorator(log), 'log'),
clear: originalFnCallDecorator(checkInitDecorator(clear), 'clear'),
info: originalFnCallDecorator(checkInitDecorator(clear), 'info'),
warn: originalFnCallDecorator(checkInitDecorator(warn), 'warn'),
error: originalFnCallDecorator(checkInitDecorator(error), 'error'),
destroy: checkInitDecorator(destroy)
};
})();
screenLog.init({
noUi: true,
proxyCallback: function(...args) {
sendLog(...args);
}
});
window._wmEvaluate = function _wmEvaluate(expr) {
try {
var result = eval(expr);
} catch (e) {
sendLog(e.stack || e.message);
return;
}
sendLog(result);
};
window.addEventListener('message', e => {
if (e.data && e.data.exprToEval) {
_wmEvaluate(e.data.exprToEval);
}
});