User:Gracenotes/console.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Gracenotes/console. |
$(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);
}
}