Jump to content

User:Gracenotes/console.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
$(function() {
	mw.util.addPortletLink('p-tb', 'javascript:setUpConsole()', 'Use console');
});

function setUpConsole() {
var div = document.createElement('div')
div.innerHTML = '<textarea id="console-code" rows="15" style="font-size: 8pt;"></textarea><input type="button" id="console-eval" value="Run"/><div id="console-log" style="border:2px solid black; vertical-align: top; width: 575px; height: 200px; overflow-y: scroll; background-color: #FDC; font-family: monospace; font-size: 8pt;"></div>';

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);
}
}