Created
October 3, 2010 21:23
-
-
Save mwbrooks/608947 to your computer and use it in GitHub Desktop.
PhoneGap-Console
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | |
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no, width=device-width"> | |
<title>PhoneGap Console</title> | |
<link rel="stylesheet" type="text/css" href="style.css" /> | |
<script type="text/javascript"> | |
function onLoad() { | |
var counter = 0; | |
var id = setInterval(function() { | |
counter++; | |
console.log("Hello from console.log " + counter); | |
}, 350); | |
setTimeout(function() { | |
clearTimeout(id); | |
}, 5000); | |
} | |
</script> | |
</head> | |
<body onload="onLoad();"> | |
<ul id="console"></ul> | |
<script type="text/javascript" src="phonegap-console.js"></script> | |
</body> | |
</html> |
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
var PhoneGapConsole = function() { | |
var consoleLog = window.console.log; | |
var consoles = []; | |
// Override console.log | |
// | |
window.console.log = function(string) { | |
// Log to the browser console | |
consoleLog.call(window.console, string); | |
// Log the HTML console | |
for (var i = 0; i < consoles.length; i++) { | |
consoles[i](string); | |
} | |
}; | |
return { | |
addConsole: function(newConsole) { | |
if (!newConsole) { return; } | |
consoles.push(newConsole); | |
} | |
}; | |
}; | |
var phonegapConsole = new PhoneGapConsole(); | |
// Add an annoying alert console | |
// | |
phonegapConsole.addConsole(function(string) { | |
alert(string); | |
}); | |
// Add debug.log | |
// | |
phonegapConsole.addConsole(function(string) { | |
if (typeof(debug) === 'undefined' || !debug.log) { return; } | |
debug.log(string); | |
}); | |
// Add HTML Console | |
// Requirement: body needs <ul id="console"></ul> | |
// | |
phonegapConsole.addConsole(function(string) { | |
var console = document.getElementById('console'); | |
if (!console) { return; } | |
var element = document.createElement('li'); | |
element.innerHTML = string; | |
console.appendChild(element); | |
console.scrollTop = console.scrollHeight | |
}); |
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
ul#console { | |
bottom:0px; | |
border-top:1px solid #CCCCCC; | |
background-color:#EFEFEF; | |
font-family: "Deja Vu San Mono", "Courier", "Sans Mono"; | |
height:70%; | |
left:0px; | |
margin:0px; | |
overflow:auto; | |
padding:0px; | |
position:absolute; | |
width:100%; | |
} | |
ul#console li { | |
font-size:16px; | |
list-style:none; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment