Jakub Josef | 9576519 | 2017-08-09 20:29:16 +0200 | [diff] [blame] | 1 | // ------ Jenkins ANSI color pure JS plugin https://github.com/jakubjosef/jenkins-ansi-color-js ------ // |
Jakub Josef | 52c3f12 | 2017-08-09 13:14:07 +0200 | [diff] [blame] | 2 | function DOMReady(fn) { |
Jakub Josef | 1aa4788 | 2017-08-09 20:21:57 +0200 | [diff] [blame] | 3 | if (document.attachEvent ? document.readyState === "complete" : document.readyState !== "loading") { |
| 4 | fn(); |
| 5 | } else { |
| 6 | document.addEventListener('DOMContentLoaded', fn); |
| 7 | } |
Jakub Josef | 52c3f12 | 2017-08-09 13:14:07 +0200 | [diff] [blame] | 8 | } |
Jakub Josef | 1aa4788 | 2017-08-09 20:21:57 +0200 | [diff] [blame] | 9 | |
Jakub Josef | 52c3f12 | 2017-08-09 13:14:07 +0200 | [diff] [blame] | 10 | function addScript(src, callback, async) { |
Jakub Josef | 1aa4788 | 2017-08-09 20:21:57 +0200 | [diff] [blame] | 11 | var s = document.createElement('script'); |
| 12 | s.setAttribute('src', src); |
| 13 | s.onload = callback; |
| 14 | if (async) s.async = true |
| 15 | document.body.appendChild(s); |
Jakub Josef | 52c3f12 | 2017-08-09 13:14:07 +0200 | [diff] [blame] | 16 | } |
| 17 | |
Jakub Josef | 1aa4788 | 2017-08-09 20:21:57 +0200 | [diff] [blame] | 18 | DOMReady(function() { |
| 19 | var consoleOutputSelector = ".console-output"; |
| 20 | if (document.querySelector(consoleOutputSelector)) { |
| 21 | // init ansi colors |
| 22 | addScript("/userContent/theme/js/ansi_up.js", function() { |
| 23 | var ansiUp = new AnsiUp, |
| 24 | $console = document.querySelector(consoleOutputSelector), |
| 25 | entities = { |
| 26 | 'amp': '&', |
| 27 | 'apos': '\'', |
| 28 | '#x27': '\'', |
| 29 | '#x2F': '/', |
| 30 | '#39': '\'', |
| 31 | '#47': '/', |
| 32 | 'lt': '<', |
| 33 | 'gt': '>', |
| 34 | 'nbsp': ' ', |
| 35 | 'quot': '"' |
| 36 | }, |
| 37 | decodeHTMLEntities = function(text) { |
| 38 | return text.replace(/&([^;]+);/gm, function(match, entity) { |
| 39 | return entities[entity] || match |
| 40 | }) |
| 41 | }, |
| 42 | colorizeFn = function() { |
| 43 | $console.innerHTML = decodeHTMLEntities(ansiUp.ansi_to_html($console.innerHTML)); |
| 44 | }; |
Jakub Josef | 52c3f12 | 2017-08-09 13:14:07 +0200 | [diff] [blame] | 45 | |
Jakub Josef | 1aa4788 | 2017-08-09 20:21:57 +0200 | [diff] [blame] | 46 | colorizeFn(); |
| 47 | // create prototype.js global ajax handle responder |
| 48 | Ajax.Responders.register({ |
| 49 | onComplete: function(req, res) { |
| 50 | if (req.body.indexOf("start=") != -1 && res.status==200 && res.responseText != "") { |
| 51 | colorizeFn(); |
| 52 | } |
| 53 | } |
| 54 | }); |
| 55 | }, true); |
| 56 | } |
Jakub Josef | 52c3f12 | 2017-08-09 13:14:07 +0200 | [diff] [blame] | 57 | }); |