-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcolorize.js
486 lines (416 loc) · 12 KB
/
colorize.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*!
* License: MIT
* Author: Mustafa İlhan, http://ilhan-mstf.github.io/
* Contributors:
* - Olivier Guimbal, https://github.com/oguimbal
* - Vikrant Sharma, https://github.com/svikrant2014
* - Kris Thom White, https://github.com/ktwbc
*/
'use strict'
/* global AnsiUp, localStorage */
const colors = ["#F9DBBD", "#CDC7E5", "#F6A5A2", "#FFF399", "#C4F4C7", "#E8E1EF", "#D9FFF8", "#ADFFE5", "#E6DBD0", "#C7FFDA", "#FCA17D", "#FCD0A1", "#FAFFD8", "#A3F7B5", "#D8B4E2"]
const ansiTransform = new AnsiUp()
delete window.AnsiUp // just delete it so its hidden from global space
function insertStylesheet () {
// dont know why, but all "spans" that are insterted in cwdb-ellipsis are blinking
// this class prevents that from happening
const style = document.createElement('style')
style.textContent = `
.ansiColorized span {
-webkit-animation-name: unset !important;
-moz-animation-name: unset !important;
-ms-animation-name: unset !important;
animation-name: unset !important;
}
/* The container */
.container-checkbox {
display: block;
position: relative;
padding-left: 20px;
cursor: pointer;
font-size: 13px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* Hide the browser's default checkbox */
.container-checkbox input {
position: absolute;
opacity: 0;
cursor: pointer;
}
/* Create a custom checkbox */
.container-checkbox .checkmark {
position: absolute;
top: 0;
left: 0;
height: 16px;
width: 16px;
background-color: #eee;
}
/* On mouse-over, add a grey background color */
.container-checkbox:hover input ~ .checkmark {
background-color: #ccc;
}
/* When the checkbox is checked, add a blue background */
.container-checkbox input:checked ~ .checkmark {
background-color: #2196F3;
}
/* Create the checkmark/indicator (hidden when not checked) */
.container-checkbox .checkmark:after {
content: "";
position: absolute;
display: none;
}
/* Show the checkmark when checked */
.container-checkbox input:checked ~ .checkmark:after {
display: block;
}
/* Style the checkmark/indicator */
.container-checkbox .checkmark:after {
left: 4px;
top: 0px;
width: 5px;
height: 10px;
border: solid white;
border-width: 0 3px 3px 0;
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
transform: rotate(45deg);
}
#logs-tweaker-panel {
position: fixed;
z-index: 1000000;
bottom: 30px;
right: 20px;
background: white;
border-radius: 4px;
box-shadow: 0 3px 6px rgba(0,0,0,0.16), 0 3px 6px rgba(0,0,0,0.23);
padding: 5px;
}
.cwdb-log-viewer-table-container.fullscreen .cwdb-log-viewer-table-body {
position: fixed;
background: white;
margin-top: 0;
top: 40px;
left: 0;
bottom: 0;
right: 0;
height: unset;
}
.logs__log-events-table__cell {
font-family: "Helvetica Neue", Roboto, Arial, sans-serif;
font-size: 14px;
}
.logs__log-events-table__cursor-text {
font-family: "Helvetica Neue", Roboto, Arial, sans-serif;
font-size: 14px;
}
.logs__log-events-table__timestamp-cell {
font-family: "Helvetica Neue", Roboto, Arial, sans-serif;
font-size: 14px;
}
`
document.head.appendChild(style)
}
function insertTools () {
const panel = document.createElement('div')
let panelHtml = `<div id="logs-tweaker-panel">`
if (isNewDesign()) {
panelHtml = `${ panelHtml }
<label class="container-checkbox"> Replace Fonts
<input type="checkbox" id="logs-tweaker-fonts" ${ fontsOn() ? 'checked="checked"' : '' }>
<span class="checkmark"></span>
</label>
`
} else {
panelHtml = `${ panelHtml }
<label class="container-checkbox"> Fullscreen
<input type="checkbox" id="logs-tweaker-fullscreen" ${ fullscreenOn() ? 'checked="checked"' : '' }>
<span class="checkmark"></span>
</label>
<label class="container-checkbox"> Follow tail
<input type="checkbox" id="logs-tweaker-autorefresh" ${ autorefreshOn() ? 'checked="checked"' : '' }>
<span class="checkmark"></span>
</label>
`
}
panel.innerHTML = panelHtml + '</div>'
document.body.append(panel)
if (isNewDesign()) {
setupNewToggles()
} else {
setupOldToggles()
}
}
function setupOldToggles () {
// toggle fullscreen
document.getElementById('logs-tweaker-fullscreen').onchange = t => {
const { checked } = t.target
if (checked) {
localStorage.setItem('logs-tweaker-fullscreen', 'yes')
} else {
localStorage.removeItem('logs-tweaker-fullscreen')
}
refreshFullscreen()
}
// toggle auto refresh
document.getElementById('logs-tweaker-autorefresh').onchange = t => {
const { checked } = t.target
if (checked) {
localStorage.setItem('logs-tweaker-autorefresh', 'yes')
} else {
localStorage.removeItem('logs-tweaker-autorefresh')
}
refreshAutoRefresh()
}
}
function setupNewToggles () {
// toggle replace fonts
document.getElementById('logs-tweaker-fonts').onchange = t => {
const { checked } = t.target
if (checked) {
localStorage.setItem('logs-tweaker-fonts', 'yes')
} else {
localStorage.removeItem('logs-tweaker-fonts')
}
refreshFonts()
}
}
function removeTools () {
const element = document.getElementById('logs-tweaker-panel')
if (element) {
element.parentNode.removeChild(element)
}
}
// add "auto refresh" & "fullscreen"
setInterval(() => {
if (window.location.hash.includes('#logEventViewer') || isNewDesign()) {
if (!isNewDesign()) {
refreshOldDesign()
} else {
refreshNewDesign()
}
if (document.getElementById('logs-tweaker-panel')) {
return
}
insertStylesheet()
insertTools()
} else {
removeTools()
}
}, 1000)
function refreshOldDesign () {
refreshAutoRefresh()
refreshFullscreen()
}
function refreshNewDesign () {
refreshAutoRefresh()
refreshFullscreen()
}
function fullscreenOn () {
return !!localStorage.getItem('logs-tweaker-fullscreen')
}
function fontsOn () {
return !!localStorage.getItem('logs-tweaker-fonts')
}
function refreshFullscreen () {
const elt = document.getElementsByClassName('cwdb-log-viewer-table-container')[0]
if (!elt) {
return
}
if (fullscreenOn()) {
if (!elt.classList.contains('fullscreen')) {
elt.classList.add('fullscreen')
}
} else {
elt.classList.remove('fullscreen')
}
}
function autorefreshOn () {
return !!localStorage.getItem('logs-tweaker-autorefresh')
}
let autorefreshInterval = null
function refreshAutoRefresh () {
if (autorefreshOn()) {
if (!autorefreshInterval) {
autorefreshInterval = setInterval(refreshTail, 3000)
refreshTail()
}
} else {
clearInterval(autorefreshInterval)
autorefreshInterval = null
}
}
function refreshFonts () {
const elements = getElements()
elements.forEach(element => changeFontElement(element, fontsOn() ? 'set' : 'clear'))
}
function refreshTail () {
const refresh = document.getElementsByClassName('cwdb-log-viewer-table-infinite-loader-bottom')[0]
if (!refresh) {
return
}
let a = refresh.firstElementChild
while (a && a.tagName !== 'A') {
a = a.nextElementSibling
}
if (a) {
a.click()
// scroll to bottom
const div = document.getElementsByClassName('cwdb-log-viewer-table-body')[0]
if (div) {
div.scrollTop = div.scrollHeight
}
}
}
function isCheckedForDecorated (element) {
return element.dataset.checkedForDecorated !== 'yes'
}
function setCheckedForDecorated (element) {
element.dataset.checkedForDecorated = 'yes'
return element
}
function isCheckedForBold (element) {
return element.dataset.checkedForBold !== 'yes'
}
function setCheckedForBold (element) {
element.dataset.checkedForBold = 'yes'
return element
}
function isStartLine (element) {
return element.innerHTML.includes('START RequestId:')
}
function isEndLine (element) {
return element.innerHTML.includes('REPORT RequestId:')
}
function isErrorLine (element) {
const text = element.innerHTML.toLowerCase()
return text.includes('error')
}
function isErrorOrEndLine (element) {
return isErrorLine(element) || isEndLine(element)
}
function isStartOrEnd (element) {
return isStartLine(element) || isEndLine(element)
}
function hasId (element, id) {
return element.innerHTML.includes(id)
}
function colorizeElement (element, color) {
element.style.backgroundColor = color
return element
}
function changeFontElement (element, action) {
if (element.dataset.isFontHandled !== 'yes' || action) {
element.dataset.isFontHandled = 'yes'
element.height = '20px'
element.lineHeight = '20px'
let subElements = element.getElementsByClassName('logs__log-events-table__cell')
for (let e of subElements) {
if (action === 'clear') {
e.style.fontFamily = null;
e.style.fontSize = null;
e.style.paddingLeft = null;
} else {
e.style.fontFamily = '"Helvetica Neue", Roboto, Arial, sans-serif'
e.style.fontSize = '0.9em'
e.style.paddingLeft = '5px'
}
}
}
return element
}
function makeBoldElement (element) {
element.style.fontWeight = 600
return element
}
function makeBold (elements) {
elements
.filter(isCheckedForBold)
.map(setCheckedForBold)
.filter(isErrorOrEndLine)
.forEach(makeBoldElement)
}
function getEventId (element) {
return element
.innerHTML
.replace(/\n/g, ' ')
.match(/\w{8}-\w{4}-\w{4}-\w{4}-\w{12}/g)[0]
}
function getUniqueEventIds (eventIds) {
return Array.from(
new Set(
eventIds
.filter(isCheckedForDecorated)
.map(setCheckedForDecorated)
.filter(isStartOrEnd)
.map(getEventId)))
}
function changeFontOnGroup (elements) {
elements.forEach(changeFontElement)
}
function colorizeGroup (elements) {
let color = colors[Math.floor((Math.random() * 10000)) % colors.length]
elements.forEach(element => colorizeElement(element, color))
}
function decorateGroups (elements) {
let eventIds = getUniqueEventIds(elements)
let newDesign = isNewDesign()
if (eventIds) {
eventIds.forEach(
id => {
colorizeGroup(elements.filter(element => hasId(element, id)))
if (newDesign && fontsOn()) changeFontOnGroup(elements)
})
}
}
function applyAnsiTransform (e) {
if (e) {
const txt = e.childNodes[0]
const textValue = txt.textContent || ''
if (/(^|\x1b)\[(\d+)m/.test(textValue)) {
e.classList.add('ansiColorized')
e.innerHTML = ansiTransform.ansi_to_html(textValue)
}
}
}
function colorizeAnsi (elements) {
for (let e of elements) {
if (e.dataset.isAnsiColorizedHandled !== 'yes') {
e.dataset.isAnsiColorizedHandled = 'yes'
applyAnsiTransform(e.getElementsByClassName("logs__log-events-table__cell")[1])
}
}
}
function getElements () {
let elements
const newDesignElements = document.querySelectorAll('iframe#microConsole-Logs')[0]
if (newDesignElements) {
elements = newDesignElements.contentDocument.getElementsByClassName('awsui-table-row')
} else {
elements = document.getElementsByClassName('cwdb-ellipsis')
}
return [].slice.call(elements)
}
function isNewDesign () {
return window.location.hash.includes('#logsV2:log-groups/log-group') && window.location.hash.includes('/log-events')
}
function colorizeAll () {
// console.time('cost-of-colorize')
// console.time('cost-of-getting-elements')
const elements = getElements()
// console.timeEnd('cost-of-getting-elements')
// console.time('cost-of-colorize-groups')
decorateGroups(elements)
// console.timeEnd('cost-of-colorize-groups')
// console.time('cost-of-colorize-ansi')
colorizeAnsi(elements)
// console.timeEnd('cost-of-colorize-ansi')
// console.time('cost-of-bold')
makeBold(elements)
// console.timeEnd('cost-of-bold')
// console.timeEnd('cost-of-colorize')
}
setInterval(colorizeAll, 1000)