console.log Hijack
const log = console.log;
const consoleUI = document.body.appendChild(
document.createElement('div')
);
document.body.style.margin = 0;
Object.assign(consoleUI.style, {
position: 'fixed',
bottom: 0,
width: '100%',
height: '30%',
maxHeight: '450px',
minHeight: '200px',
background: 'rgb(68, 68, 81)',
overflow: 'scroll'
});
function consoleRow(str) {
const row = document.createElement('div');
consoleUI.prepend(row);
row.innerText = str;
Object.assign(row.style, {
padding: '.5em',
fontFamily: 'sans-serif',
color: 'white',
borderBottom: '1px solid rgba(255, 255, 255, .1)',
whiteSpace: 'pre-wrap'
});
}
console.log = (...args) => {
const formatted = args.map(val => {
return typeof val === 'object' ?
JSON.stringify(val, null, 2) : val;
});
consoleRow(formatted.join(' '));
log.apply(console, args);
};
// test it out
console.log(1, 2, 3, 4);
console.log(new Date());
const someObject = {
test: 123,
testing: { x: 1, y: 2, z: 3 }
};
console.log(someObject);
console.log(3, 2, 1, 0);
I’m thinking about adding a little fake console to the Snippet Zone Quick Editor – just whipped this up as a proof of concept – something like this should work…