-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconsole.js
146 lines (126 loc) · 4.93 KB
/
console.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
(function () {
var CSS_PATH = 'https://rawgithub.com/wtfil/mobile-console/master/console.css',
element;
function createElement(cls, content, name) {
var elem = document.createElement(name || 'div');
if (content) {
elem.innerHTML = content;
}
elem.classList.add('mobile-console__' + cls);
return elem;
}
function scrollToBottom() {
element.scrollTop = element.scrollHeight;
}
function escapeHTML(html) {
return String(html)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/\//g, '/');
}
function inspect(obj, key, enumerable) {
var content = createElement('log'),
top = createElement('top'),
node = createElement('node', '', 'span'),
elemsCreated = false,
keyNode, text, props;
if (arguments.length === 2) {
enumerable = true;
}
if (key) {
keyNode = createElement(enumerable ? 'enumerable-key' : 'not-enumerable-key', key + ':', 'span');
top.appendChild(keyNode);
}
content.appendChild(top);
top.appendChild(node);
if (typeof obj === 'number') {
node.innerHTML = obj;
node.classList.add('number');
} else if (typeof obj === 'string') {
node.innerHTML = '"' + escapeHTML(obj) + '"';
node.classList.add('string');
} else if (obj === undefined) {
node.innerHTML = 'undefined';
node.classList.add('null');
} else if (obj === null) {
node.innerHTML = 'null';
node.classList.add('null');
} else if (obj === false || obj === true) {
node.innerHTML = '' + obj;
} else if (obj instanceof Function) {
text = obj.toString();
if (text.indexOf('[native code]') !== -1) {
node.innerHTML = text;
} else {
node.innerHTML = text.split(/(\{)/).slice(0, 2).join('');
}
} else {
node.innerHTML = obj.constructor.name || obj.constructor.toString().replace(/\[|\]|object\s/g, '');
content.classList.add('inspect');
if (Array.isArray(obj)) {
node.innerHTML = '[' + obj.length + ']';
}
props = createElement('props');
top.addEventListener('click', function () {
var keys = [],
basicKeys, elem, key;
if (content.classList.contains('inspect')) {
if (!elemsCreated) {
for (key in obj) {
keys.push(key);
}
Object.getOwnPropertyNames(obj)
.concat(keys)
.filter(function (key, index, arr) {
return arr.indexOf(key) === index;
})
.sort()
.concat('__proto__')
.forEach(function (key) {
var enumerable = Object.getOwnPropertyDescriptor(obj, key);
enumerable = enumerable ? enumerable.enumerable : false;
elem = inspect(obj[key], key, enumerable);
props.appendChild(elem);
});
elemsCreated = true;
}
content.classList.remove('inspect');
content.classList.add('opened');
props.classList.add('visible');
} else {
content.classList.add('inspect');
content.classList.remove('opened');
props.classList.remove('visible');
}
}, false);
content.appendChild(props);
}
return content;
}
function createConsoleBlock() {
element = createElement('holder');
document.body.appendChild(element);
}
function addStyles() {
style = document.createElement('link');
style.setAttribute('rel', 'stylesheet');
style.setAttribute('type', 'text/css');
style.setAttribute('href', CSS_PATH);
document.querySelector('head').appendChild(style);
}
if(/android|webos|iphone|ipad|ipod|blackberry|window\sphone/i.test(navigator.userAgent)) {
window.addEventListener('load', function () {
addStyles();
console.error = console.log = function (message) {
if (!element) {
createConsoleBlock();
}
element.appendChild(inspect(message));
scrollToBottom();
};
}, false);
}
}());