User:Gracenotes/console.js

$(function {	mw.util.addPortletLink('p-tb', 'javascript:setUpConsole', 'Use console'); });

function setUpConsole { var div = document.createElement('div') div.innerHTML = '  ';

document.getElementById('siteSub').appendChild(div);

document.getElementById('console-eval').onclick = function { var text = document.getElementById('console-code').value; try { var result = eval(text); if (typeof result != 'undefined') pp(result); } catch (e) { pp(e); } };

window.itr = function(obj) { for (var prop in obj) pp(prop + ": " + obj[prop]); }

window.pp = function(obj) { var box = document.createElement("div"); var disp; var type = typeof obj; if (!obj == null) disp = 'null'; else if (type == 'string' || type == 'number') disp = obj; else if (type == 'boolean' || type == 'function') disp = obj.toString; else if (type == 'object') { if (obj instanceof Error) { disp = obj.name + ': ' + obj.message + '\nLine: ' + obj.lineNumber; } else { disp = obj.toString; }	} else { disp = obj; }

box.appendChild(document.createTextNode(disp)); box.setAttribute("style", "margin-top: .2em; padding-left:.2em; border: 0px solid black; background-color: lightgray; color: #006"); document.getElementById('console-log').appendChild(box); } }