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