Skip to content

Instantly share code, notes, and snippets.

@amodm
Last active May 17, 2017 09:48
Show Gist options
  • Save amodm/09a3350ec3162b8fe27d76bc24cca373 to your computer and use it in GitHub Desktop.
Save amodm/09a3350ec3162b8fe27d76bc24cca373 to your computer and use it in GitHub Desktop.
console.log to output window on jsFiddle
// Hotlink: https://rawgit.com/amodm/09a3350ec3162b8fe27d76bc24cca373/raw/6bce576080dba78ef49fddbbda28f2e421b2caea/jsfiddle-logger.js
//
// You may need to call initJsFiddleLogger() explicitly
//
const existingConsoleLog = console && console.log;
const jsFiddleLogger = function(message) {
if (existingConsoleLog) {
existingConsoleLog(message);
}
if (typeof message === 'object') {
message = JSON.stringify(message);
}
const newLine = document.createElement('p');
newLine.append(message);
jsFiddleLogger.jsFiddleLogContainer.appendChild(newLine);
};
function initJsFiddleLogger() {
if (!console) console = {};
if (!console.log || !console.log.jsFiddleLogContainer) {
console.log = jsFiddleLogger;
}
const logContainerName = 'logger';
let logContainer = document.getElementById(logContainerName);
if (!logContainer) {
logContainer = document.createElement('div');
logContainer.id = logContainerName;
document.body.appendChild(logContainer);
jsFiddleLogger.jsFiddleLogContainer = logContainer;
}
}
//onDomReady(initJsFiddleLogger);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment