Last active
December 13, 2021 08:36
-
-
Save fedek6/c50336795ea3de851e252ec7551971bd to your computer and use it in GitHub Desktop.
Virtual console for JSFiddle
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
// 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