Skip to content

Instantly share code, notes, and snippets.

@fedek6
Last active December 13, 2021 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fedek6/c50336795ea3de851e252ec7551971bd to your computer and use it in GitHub Desktop.
Save fedek6/c50336795ea3de851e252ec7551971bd to your computer and use it in GitHub Desktop.
Virtual console for JSFiddle
// Init DOM
const consoleContainer = document.createElement("div");
consoleContainer.classList.add("console");
const legend = document.createElement("small");
legend.textContent = "Console";
consoleContainer.append(legend);
const vConsole = document.createElement("pre");
consoleContainer.append(vConsole);
window.addEventListener('DOMContentLoaded', (event) => {
document.body.append(consoleContainer);
});
// Style
const style = document.createElement("style");
style.textContent = `.console {
margin: 2em 0;
}
.console small {
text-transform: uppercase;
color: silver;
}
pre {
border: solid 1px Silver;
height: 30vh;
overflow-y: scroll;
margin: 0 0;
font-size: .85em;
resize:both;
}`;
document.head.append(style)
const originalConsole = console.log;
// Overwrite console.log
console.log = (...a) => {
a.map((l) => {
let t;
if (typeof l.toString == "function") {
l = l.toString();
} else {
l = JSON.stringify(l);
}
t = document.createTextNode(`${l} `);
vConsole.appendChild(t);
});
vConsole.appendChild(document.createTextNode("\n"));
vConsole.scrollTop = vConsole.scrollHeight;
originalConsole(...a);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment