Last active
September 18, 2017 22:13
-
-
Save ejdyksen/6615470 to your computer and use it in GitHub Desktop.
A tool for getting at a JS console when there is none available.
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
<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> |
@danharper I would have loved to use something more sophisticated, but I didn't have network access to the browser session at all from my development machine (odd as that seems).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Had you considered using Weinre for remote debugging? I've been using this for debugging PhoneGap applications whilst they're running natively on a device.