Skip to content

Instantly share code, notes, and snippets.

@chrisjlee
Forked from ejdyksen/mini-console.html
Created February 25, 2014 22:48
Show Gist options
  • Save chrisjlee/9219652 to your computer and use it in GitHub Desktop.
Save chrisjlee/9219652 to your computer and use it in GitHub Desktop.
<div id="consolelog" style="font-family: 'Courier New', Courier, monospace; font-size: 12px; margin: 40px 30px 0px; background-color: white; border: 2px solid black; padding: 10px;"></div>
<input type="text" id="consoleinput" style="margin: 0px 30px; width: 400px;" onkeypress="return evalConsoleInput(event, this.value);" />
<script type="text/javascript">
var appendConsole = function(message, type) {
var color = "black";
if (type === "error") {
color = "red";
} else if (type === "debug") {
color = "blue";
}
var div = document.createElement('div');
div.style.color = color;
div.style.marginBottom = "10px";
div.innerHTML = message;
document.getElementById("consolelog").appendChild(div);
}
var originalConsole = null;
if (window.console != null) {
originalConsole = window.console;
}
window.console = {
log: function(message) {
appendConsole(message, "info");
originalConsole.log(message);
},
info: function(message) {
appendConsole(message, "info");
originalConsole.info(message);
},
debug: function(message) {
appendConsole(message, "debug");
originalConsole.debug(message);
},
error: function(message) {
appendConsole(message, "error");
originalConsole.error(message);
}
};
function evalConsoleInput(e, message)
{
if (e.keyCode == 13) { // 13 is the keycode for the enter key
var inputField = document.getElementById("consoleinput");
var evalString = inputField.value;
console.log("> " + evalString);
try {
var returnValue = eval(evalString);
console.log(returnValue);
} catch (e) {
console.error(e.message);
} finally {
inputField.value = "";
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment