User:Splarka/stripsource.js

//script for generating a stripped version of a wikipage for local testing/modification // var getReq;

function sourcebutton { //only activates in monoobook-based skins currently (not hard to put into other skins manually) if(!document.getElementById('p-tb')) return addPortletLink('p-tb','javascript:getXML("' + document.location.href + '",getsourceStateChange)','Bare source','t-source','generate a localized version of the source of this page'); } addOnloadHook(sourcebutton);

function parsesource(data) { var source = data;

//strip out stuff like google ads, useless debug comments, sitenotice, sendtoafriend, other slowdowns //possible future stripables: edittools var rxp = [ /\<\!\-\- attempt to fetch [^>]*\-\-\>\n/ig, /\<\!\-\- right column \(google ads\) BEGIN \-\-\>[^¿]*\<\!\-\- right column \(google ads\) END \-\-\>/ig, /\<\!\-\- Served by [^¿]*\<\!\-\- analytics \(end\) \-\-\>/ig, /\.*\<\/li\>\n/ig, /\.*\<\/li\>\n/ig, /\[^<]{0,3}var reqSendToAFriend[^<]*\< 50[^<]*\<\/script\>/ig, /\[^¿]*\<\/script\>\<\/div\>/ig ] for(var i=0;i < rxp.length;i++) { source = source.replace(rxp[i],''); } source = source.replace(' ',' ');

//lazy output generation method (rewrite me as a popup?) var bod = document.getElementsByTagName('body')[0]; var sourcediv = document.createElement('div'); sourcediv.setAttribute('style','position:absolute;top:0;left:0;width:80%;height:80%;border:2px solid black;z-index:10;background-color:#ffffff;padding:1em;margin:2em;display:block;'); sourcediv.setAttribute('id','annoyingsourcebox'); var sourcekill = document.createElement('a'); sourcekill.setAttribute('href','javascript:document.getElementById("annoyingsourcebox").setAttribute("style","display:none;");'); sourcekill.setAttribute('style','text-decoration:underline;'); sourcekill.appendChild(document.createTextNode('kill popup')); var sourceta = document.createElement('textarea'); sourceta.setAttribute('style','width:100%;height:98%'); sourceta.appendChild(document.createTextNode(source)); sourcediv.appendChild(sourcekill); sourcediv.appendChild(sourceta); bod.appendChild(sourcediv); }

function getXML(url,func) { if (window.XMLHttpRequest) { // Non-IE browsers getReq = new XMLHttpRequest; } else if (window.ActiveXObject) { // IE   getReq = new ActiveXObject('Microsoft.XMLHTTP'); } if (getReq) { getReq.onreadystatechange = func; try { getReq.open('GET', url, true); getReq.send(''); } catch (e) { alert(e); } } else { alert('XMLHTTPRequest not supported'); } }

function getsourceStateChange { switch (getReq.readyState) { case 1: break; case 2: break; case 3: break; case 4: if (getReq.status == 200) { // OK response parsesource(getReq.responseText); } else { alert('Problem: ' + getReq.statusText); }     break; } } //