User:Splarka/domdump.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:Splarka/domdump. |
/* DOM dumper [0.0.3],
Original from http://en.wikipedia.org/wiki/User:Splarka/domdump.js
Dumps the dom or object structure of the element specified by the URL parameters:
* domdump=object (also has parameter: dumpcrop=#, length to crop attributes/text nodes)
* objdump=object
and creates a textarea in the contentSub of the results, with space indented tree structure.
Object can be specified by: ID, name, tag name, or class name. The first result is always
taken. So for example, you can domdump=html to dump the whole object.
To do:
* Allow exclusion of text/comments?
* Allow specific faux tag formatting?
* Allow custom indent structure?
*/
if(queryString('domdump') || queryString('objdump')) addOnloadHook(domDump);
var dumpCrop = '20';
var findDepth = 0;
function domDump() {
var objid = (queryString('domdump')) ? queryString('domdump') : queryString('objdump')
var obj = getElementByWhatever(objid);
if(!obj) return;
if(queryString('dumpcrop')) dumpCrop = parseInt(queryString('dumpcrop'))
if(queryString('domdump')) {
//call function to recall children elements recursively
var text = '[DOM dump of "' + objid + '"]\n' + dumpChildren(obj);
} else {
//use javascript for-in
var text = centerbuffer(' Object dump of "' + objid + '" ','=',80) + '\n';
for(var i in obj) {
text += centerbuffer(' ' + i + ' ','-',80) + '\n' + obj[i] + '\n';
}
}
//show dump after, so we don't pollute the results
var show = (document.getElementById('contentSub')) ? document.getElementById('contentSub') : document.getElementById('topbar')
if(!show) return;
var out = document.createElement('textarea');
out.style.width = '100%';
out.style.height = '30em';
out.value = text;
show.appendChild(out);
}
function centerbuffer(txt,chr,width) {
var buf = '';
var num = parseInt((width - txt.length + 1) / 2);
for(var i=0;i<num;i++) {
buf += chr.substr(0,1);
}
var ctxt = buf + txt + buf;
ctxt = ctxt.substr(0,width);
return ctxt;
}
function dumpChildren(obj,func) {
var childs = 0; var txt = ''; var txtafter = ''; var indent = '';
for(var i=0;i<findDepth;i++) indent += ' '
switch (obj.nodeType) {
case(1):
var attr = '';
for(var i=0;i<obj.attributes.length;i++) {
var anam = obj.attributes[i].nodeName.toLowerCase();
var aval = obj.attributes[i].nodeValue;
if(aval.length > dumpCrop) aval = aval.substr(0,dumpCrop) + '...'
attr += anam + '="' + aval + '" ';
}
if(attr.length > 0) attr = ' ' + attr.replace(/ $/,'');
txt += indent + '<' + obj.tagName.toLowerCase() + attr + '>\n';
txtafter = indent + '</' + obj.tagName.toLowerCase() + '>\n';
break;
case(3):
var val = escapeSome(obj.nodeValue);
if(val.length > dumpCrop) val = val.substr(0,dumpCrop) + '...';
txt += indent + '[text length="' + obj.nodeValue.length + '"]' + val + '[/text]\n';
break;
case(8):
var val = escapeSome(obj.nodeValue);
if(val.length > dumpCrop) val = val.substr(0,dumpCrop) + '...';
txt += indent + '[comment length="' + obj.nodeValue.length + '"]' + val + '[/comment]\n';
break;
}
while(obj.childNodes[childs]) {
findDepth++;
txt += dumpChildren(obj.childNodes[childs]);
findDepth--;
childs++;
}
if(obj.nodeType == 1) txt += txtafter
return txt;
}
function escapeSome(str) {
//return, linefeed, tabs
str = str.replace(/\r/g,'\\r').replace(/\n/g,'\\n').replace(/\t/g,'\\t');
// invisible unicodes, convert to JS entities
var eucChars = [160,173,847,1536,1537,1538,1539,1757,1807,3852,4447,4448,5760,6155,6156,6157,6158,8192,8193,8194,8195,8196,8197,8198,8199,8200,8201,8202,8203,8204,8205,8206,8207,8209,8232,8233,8234,8235,8236,8237,8238,8239,8239,8260,8287,8288,8289,8290,8291,8292,8298,8299,8300,8301,8302,8303,12272,12273,12274,12275,12276,12277,12278,12279,12280,12281,12282,12283,12288,12350,65279,65408,65532]
for(var i=0;i<eucChars.length;i++) {
var pattern = new RegExp(String.fromCharCode(eucChars[i]),'g');
if(str.search(pattern)!=-1) str = str.replace(pattern,'\\u' + hx4(eucChars[i]) + '')
}
return str;
}
function hx4(dec) {
var h = dec.toString(16);
while(h.length < 4) h = '0' + h;
return h;
}
function queryString(p) {
var re = RegExp('[&?]' + p + '=([^&]*)');
var matches;
if (matches = re.exec(document.location)) {
try {
return decodeURI(matches[1]);
} catch (e) {
}
}
return null;
}
function getElementByWhatever(thing,par) {
var parent = (!par) ? document : par
if(parent.getElementById(thing)) {
return parent.getElementById(thing);
} else if(parent.getElementsByName(thing)[0]) {
return parent.getElementsByName(thing)[0];
} else if(parent.getElementsByTagName(thing)[0]) {
return parent.getElementsByTagName(thing)[0];
} else {
var c = getElementsByClassName(parent,'*',thing);
if(c.length > 0) {
return c[0];
} else {
return false;
}
}
}