forked from godspeed1715/jquery.jsconsole.js
-
Notifications
You must be signed in to change notification settings - Fork 3
/
htaconsole.js
305 lines (281 loc) · 10.4 KB
/
htaconsole.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
;(function(context) {
var layout = '<div id="panel-box">' +
' <div id="panel-dragbar"></div>' +
' <div id="panel-header">' +
' <span style="padding-left: 5px; font-weight: 700; color: #555;">Console</span>' +
' <span id="panel-menu-close" style="float: right; margin-right: 8px; cursor: pointer; color: grey;" onclick="htaConsole.toggle();">✖</span>' +
' <span id="panel-menu-minimize" style="float: right; margin-top:1px;margin-right: 10px; cursor: pointer; color: grey;" onclick="htaConsole.minimize()">▼</span>' +
' </div>' +
' <div style="background:#fff">' +
' <div id="panel-console"></div>' +
' <span style="padding-left:4px; font-weight:700; font-size:16px; color:#62adea">></i></span><input id="commandline">' +
' </div>' +
' <div id="panel-navigation">' +
' <span style="cursor: pointer" onclick="htaConsole.clear()">⦸</span>' +
' <span id="panel-menu-transparent" style="cursor: pointer" onclick="htaConsole.transparent()">◐</span>' +
' </div>' +
'</div>'
var css = {
panelBox: {
'width': '100%',
'color': '#000',
'font-family': 'Consolas, Courier',
'font-size': '14px',
'padding': '3px 0px 3px 0px',
'position': 'fixed',
'bottom': '-4px',
'left': '0px',
'opacity': '1.0',
'display': 'block',
'z-index': '10000'
},
panelDragbar: {
'border-top': '3px solid #ededed',
'border-bottom': '1px solid #ccc',
'width': '100%',
'cursor': 'row-resize'
},
panelHeader: {
'height': '24px',
'background': '#ededed',
'border-top': '1px solid #fff',
'border-bottom': '1px solid #cacaca',
'text-align': 'left',
'font-family': 'Arial'
},
panelConsole: {
'overflow': 'auto',
'height': '150px',
'padding': ' 0 5px 0 5px',
'text-align': 'left',
'display': 'block',
'background': '#fff'
},
panelNavigation: {
'height': '21px',
'width': '100%',
'padding-left': '4px',
'line-height': '1',
'font-size': '18px',
'font-weight': '700',
'background': '#ededed',
'border-top': '1px solid #cacaca',
'font-family': 'Arial',
'color': 'grey'
},
commandline: {
'width': '97%',
'border': 'none',
'padding-left': '6px',
'outline-width': '0px'
}
}
var cmdHistory = []
var cmdHistoryPosition = 0
var camelCase = function(input) {
return input.toLowerCase().replace(/-(.)/g, function(match, group1) {
return group1.toUpperCase()
})
}
var hyphen = function(input) {
return input.replace(/([a-z][A-Z])/g, function (group) {
return group[0] + '-' + group[1].toLowerCase()
})
}
var censor = function(censor) {
var i = 0
return function(key, value) {
if (i !== 0 && typeof(censor) === 'object' && typeof(value) == 'object' && censor == value)
return '[Circular]'
if (i >= 29)
return '[Unknown]'
;++i
return value
}
}
var htaConsole = {}
htaConsole.init = function() {
var container = document.createElement('div')
container.innerHTML = layout
document.body.appendChild(container)
for (var cssClass in css) {
for (var cssProp in css[cssClass]) {
document.getElementById(hyphen(cssClass)).style[camelCase(cssProp)] = css[cssClass][cssProp]
}
}
// Dragbar
var dragHandler = function(evt) {
var height = "innerHeight" in window ? window.innerHeight : document.documentElement.offsetHeight
document.getElementById('panel-console').style.height = (height - 86 - evt.clientY) + 'px'
}
document.getElementById('panel-dragbar').addEventListener('mousedown', function(evt) {
evt.preventDefault()
document.addEventListener('mousemove', dragHandler)
})
document.addEventListener('mouseup', function(evt) {
document.removeEventListener('mousemove', dragHandler)
})
document.addEventListener('keydown', function(evt) {
if (evt.keyCode === 123) htaConsole.toggle()
}, true)
// Focus
document.getElementById('panel-console').addEventListener('click', function(evt) {
document.getElementById('commandline').focus()
})
// keypress
document.getElementById('commandline').addEventListener('keypress', function(evt) {
if (evt.keyCode === 13) {
var sCmd = this.value
if (sCmd === "clear") {
htaConsole.clear()
this.value = ''
} else if (sCmd === "history") {
for (var x = 0; x < cmdHistory.length; x++) console.log(cmdHistory[x])
this.value = ''
} else if (sCmd) {
htaConsole.log('<span style="color:#777"; font-weight:700">></span> <span>' + sCmd + '</span>')
cmdHistory.push(sCmd)
try {
var evalCmd = eval(sCmd)
if (typeof evalCmd === "object") {
try { console.log(JSON.stringify(evalCmd)) }
catch (e) {
try { console.log(JSON.stringify(evalCmd, censor(evalCmd))) }
catch (e) { throw e }
}
} else {
console.log(evalCmd)
}
this.value = ''
} catch (e) {
this.value = ''
throw e
}
}
cmdHistoryPosition = cmdHistory.length
}
})
document.getElementById('commandline').addEventListener('keydown', function(evt) {
if (evt.keyCode === 38) {
cmdHistoryPosition--
if (cmdHistoryPosition < 0) cmdHistoryPosition = 0
if (cmdHistory[cmdHistoryPosition] !== undefined && cmdHistory[cmdHistoryPosition] !== '') {
this.value = cmdHistory[cmdHistoryPosition]
return false
} else if (cmdHistoryPosition === cmdHistory.length) {
this.value = ''
return false
}
}
if (evt.keyCode === 40) {
cmdHistoryPosition++
if (cmdHistoryPosition >= cmdHistory.length) cmdHistoryPosition = cmdHistory.length
if (cmdHistory[cmdHistoryPosition] !== undefined && cmdHistory[cmdHistoryPosition] !== '') {
this.value = cmdHistory[cmdHistoryPosition]
return false
} else if (cmdHistoryPosition === cmdHistory.length) {
this.value = ''
return false
}
}
})
console = window || window.console
var method = [
"log", "info", "warn", "onerror", "debug", "trace", "dir", "group",
"groupCollapsed", "groupEnd", "time", "timeEnd", "profile", "profileEnd",
"dirxml", "assert", "count", "markTimeline", "timeStamp", "clear", "cmd"
]
// define undefined methods as noops to prevent errors
for (var i = 0; i < method.length; i++) {
if (!window.console[method[i]]) {
if (method[i] === 'onerror') {
console[method[i]] = function() {
var scriptURL = arguments[1]
var scriptName = scriptURL.split('/')[scriptURL.split('/').length - 1]
htaConsole.log(
'<span style="color:red">⊗' +
' <span>' + arguments[0] + '</span>' +
' <span class="pull-right" style="cursor:pointer; color:blue" onclick="htaConsole.showModal(\''+ scriptURL +'\')">' + scriptName + ":" + arguments[2] + '</span>' +
'</span>'
)
return true
}
} else {
console[method[i]] = function() { return true }
}
}
}
console = {
clear: function() {
htaConsole.clear()
},
cmd: function(arg) {
htaConsole.log('<span style="color:#777"; font-weight:700">></span> <span>' + arg + "</span>")
},
info: function(arg) {
htaConsole.log('<span style="color: RoyalBlue">ⓘ</span> <span>' + arg + "</span>")
},
log: function(arg) {
htaConsole.log('<span style="padding-left: 2px;">' + arg + '</span>')
},
warn: function(arg) {
htaConsole.log('<span style="color: orange">⚠</span> <span>' + arg + '</span>')
},
error: function(arg) {
throw arg
}
}
}
htaConsole.log = function(msg) {
var logContainer = document.createElement('div')
logContainer.innerHTML = msg + "<br>"
document.getElementById('panel-console').appendChild(logContainer)
this.consoleresize()
}
htaConsole.consoleresize = function() {
var panelConsole = document.getElementById('panel-console')
panelConsole.scrollTop = panelConsole.scrollHeight
}
htaConsole.clear = function() {
document.getElementById('panel-console').innerHTML = ''
}
htaConsole.transparent = function() {
var panelBox = document.getElementById('panel-box')
var panelTransparent = document.getElementById('panel-menu-transparent')
if (panelBox.style.opacity === "0.5") {
panelBox.style.opacity = '1.0'
panelTransparent.innerHTML = '◐'
} else {
panelBox.style.opacity = '0.5'
panelTransparent.innerHTML = '⚫'
}
}
htaConsole.minimize = function() {
var panelConsole = document.getElementById('panel-console')
var panelMinimize = document.getElementById('panel-menu-minimize')
if (panelConsole.style.display === "block") {
this.panelConsoleHeight = panelConsole.style.height
panelConsole.style.height = "0px"
panelConsole.style.display = "none"
panelMinimize.innerHTML = '▲'
} else {
panelConsole.style.height = this.panelConsoleHeight
panelConsole.style.display = "block"
panelMinimize.innerHTML = '▼'
this.consoleresize()
}
}
htaConsole.toggle = function() {
var panelBox = document.getElementById('panel-box')
if (panelBox.style.display === "block") {
panelBox.style.display = "none"
} else {
panelBox.style.display = "block"
this.consoleresize()
}
}
htaConsole.showModal = function(url){
window.open(url)
}
return context.htaConsole = htaConsole
})(window).init()