Created
June 19, 2017 14:15
-
-
Save greg-nagy/d68b8f22e12e15d9ec1749096b1a30d7 to your computer and use it in GitHub Desktop.
helper for displaying console.log
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare function require(name:string); | |
var striptags = require('striptags'); | |
// export var console2:Console2Interface = null; | |
let originalConsoleLog:Function = console.log; | |
interface Console2Interface { | |
extendedLog: Function | |
} | |
let createLoggerHTMLElement = (logger: HTMLDivElement) => { | |
logger = document.createElement("div"); | |
logger.id = "log"; | |
document.body.appendChild(logger); | |
return logger; | |
}; | |
let registerConsole2 = (logger: HTMLDivElement) => { | |
let _console:Console2Interface = { | |
extendedLog: (log: string, color: string) => { | |
logger.innerHTML += `<span style="color:${color}">${log}</span><br/>`; | |
originalConsoleLog(striptags(log)); | |
} | |
}; | |
window['console2'] = _console; | |
// console2 = _console; | |
}; | |
export function replaceConsoleLog() { | |
let logger: HTMLDivElement = document.getElementById('log') as HTMLDivElement; | |
if (logger == null) { | |
logger = createLoggerHTMLElement(logger); | |
} | |
console.log = (...args) => { | |
for (let i = 0; i < args.length; i++) { | |
if (typeof args[i] == 'object') { | |
logger.innerHTML += (JSON && JSON.stringify ? JSON.stringify(args[i], undefined, 2) : args[i]) + '<br />'; | |
} else { | |
logger.innerHTML += args[i] + '<br />'; | |
args[i] = striptags(args[i]); | |
} | |
} | |
originalConsoleLog.apply(this, args); | |
}; | |
registerConsole2(logger); | |
} | |
if(!window.hasOwnProperty('NO_REPLACE_CONSOLE_LOG')) | |
{ | |
replaceConsoleLog(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment