User:Saintrain/J1/deSpamWiki.js

// //================ deSpamWiki  ============================================================ function deSpamWiki { var GBUG = false; var BUG = GBUG,  BUGT = GBUG; if(BUGT)console.time("dSW");

// each instance of SpamObj specifies what HTML node to look for and what to do with it. // id and class are mututally exclusive node atributes; // clsNdx is just silly (unless, maybe, zap every node with class?); // act(ion) must be one of the switch cases, below var SpamObj = { id:"", class:"", clsNdx:0,  act:"" };

// will be an array of SpamObj var spamList = new Array;

// start populating spamList var sndx = 0; spamList[sndx] = SpamObj = { id : "editpage-specialchars", act : "hide node" };     sndx++; //-???- spamList[sndx] = SpamObj = { id : "toolbar",               act : "hidable node" };  sndx++; /// these are better done in css //-css- spamList[sndx] = SpamObj = { id : "newarticletext",        act : "delete node" };  sndx++; //-css- spamList[sndx] = SpamObj = { id : "userinvalidcssjstitle", act : "delete node" };  sndx++; //-css- spamList[sndx] = SpamObj = { id : "editpage-copywarn",     act : "delete node" };  sndx++; //-css- spamList[sndx] = SpamObj = { id : "editpage-copywarn2",    act : "delete node" };  sndx++; //-css- spamList[sndx] = SpamObj = { id : "editpage-copywarn3",    act : "delete node" };  sndx++; //-css- spamList[sndx] = SpamObj = { id : "talkpagetext",          act : "delete node" };  sndx++;

// itterate through spamList for(var s=0; s<spamList.length; s++) { if(BUG)console.log('dSW: spamList[',s,'].id={',spamList[s].id,'} .class={',spamList[s].class,'}  .act={',spamList[s].act,'} '); var spam, spamStr; //spam is the node;  spamStr will be uset to label the collapsed block with( spamList[s] ) { // rest of the func if( "" != id ) { //find the node spamStr = id; spam = document.getElementById( id );   // the node }    else if( "" != class ) { spamStr = class; var hits = document.getElementsByClass( class, null, null ); if( hits ) spam = hits[clsNdx]; // this is silly! just use 1st (or all?). }    else { if(BUG)console.log('dSW: spamList[s][',s,'].id == .class == "" '); continue; }

// not every page will have the target nodes. Not an error. if ( !spam ) { if(BUG)console.log('dSW: no find spam={',spamStr,'}' ); continue; }

// found the node. Act on it. switch( act ) {

// hide the node by wrapping it in       // //          //      show/hide  //    navStr  <- where the spam goes //          // see: Wikipedia:NavFrame. "NavFrame", "NavHead", "NavToggle" etc are all expected by the NavFrame code case "hide node": //==========  "hide node"  ======================================= case "hidable node": //===== =  "hidable node"  ======================================= if( !GBUG ) BUG = false; var spamMom = spam.parentNode; if ( !spamMom ) { if(BUG)console.log('dSW: NO FIND spam={',spamStr,'}.parentNode' ); continue; } var hndx = 666 + s; // bump index so as not to interfere with "legit" navframe stuff // create the wrapper div var NavFrame = document.createElement("div"); NavFrame.setAttribute('id', 'NavFrame' + hndx ); // give it an unique id        NavFrame.className = 'NavFrame'; // this puts the new block just before the spam block in the tree. //  Then the spam is placed inside the block, so, when all is displayed //  the (hidden) spam shows up at the same place it was originally. spamMom.insertBefore( NavFrame, spam ); // create the header var NavHead = document.createElement( "div" ); NavHead.textContent = spamStr; NavHead.className = 'NavHead'; NavFrame.appendChild( NavHead ); // create the show/hide button. This will float on the right var NavToggle = document.createElement("a"); NavHead.appendChild( NavToggle ); NavToggle.className = 'NavToggle'; NavToggle.setAttribute('id', 'NavToggle' + hndx ); NavToggle.setAttribute('href', 'javascript:toggleNavigationBar(' + hndx + ');'); // Link it to the NavFrame toggler NavToggle.textContent = "[hide]"; // the NavFrame toggler expects this string spam.id = "NavFrame" + hndx; // (possibly re-)id the original spam. (This hasn't caused problems yet.) spam.className += " NavContent"; // n.b.  append class NavFrame.appendChild( spam ); // re-parents spam. Puts it in the wrapper block if( "hide node" == act ) // hide immediately toggleNavigationBar( hndx );

break;

// better done in css if possible case "delete node": //==========  "delete node"  ======================================= if( !GBUG ) BUG = false; var spamMom = spam.parentNode; if ( !spamMom ) { if(BUG)console.log('dSW: NO FIND spam={',spamStr,'}.parentNode' ); continue; } spamMom.removeChild( spam ); spam = null; break;

// never used the rest but they seemed a good idea at the time case "delete last kids": //==========  "delete last kids"  ======================================= if( !GBUG ) BUG = false; if( ! spam.hasChildNodes) { if(BUG)console.log('dSW: NO FIND spam={',spamStr,'}.hasChildNodes' ); continue; } var c0 = spam.firstChild; if(BUG)console.log('dSW: LEAVING: tagName={',c0.tagName,'}, name={',c0.name,'}, kid=',c0 ); if(BUG)console.log(' innerHTML={',c0.innerHTML,'}' ); while( 1 < spam.childNodes.length ) { var lc = spam.lastChild; if(BUG)console.log('dSW: removing: tagName={',lc.tagName,'}, name={',lc.name,'}, kid=',lc ); if(BUG)console.log(' innerHTML={',lc.innerHTML,'}' ); spam.removeChild( spam.lastChild ); if(BUG)console.log(' '); }       break; case "delete last tags": //==========  "delete last tags"  ======================================= if( !GBUG ) BUG = false; if( ! spam.hasChildNodes) { if(BUG)console.log('dSW: NO FIND spam={',spamStr,'}.hasChildNodes' ); continue; } var it0 = -1; for(k=0; k<spam.childNodes.length; k++) { if( spam.childNodes[k].tagName ) { // 1st tag it0 = k + 1; break; }         }        if( it0 < 0 )  // no tags break; var tag0 = spam.childNodes[it0]; if(BUG)console.log('dSW: LEAVING: tagName={',tag0.tagName,'}, name={',tag0.name,'}, kid=',tag0 ); if(BUG)console.log(' innerHTML={',tag0.innerHTML,'}' ); while( it0 < spam.childNodes.length ) { var lc = spam.lastChild; if(BUG)console.log('dSW: removing: tagName={',lc.tagName,'}, name={',lc.name,'}, kid=',lc ); if(BUG)console.log(' innerHTML={',lc.innerHTML,'}' ); spam.removeChild( spam.lastChild ); if(BUG)console.log(' '); }       break; default: if(BUG)console.log('dSW: UNKNOWN act[',s,'] = {',act,'}.' ); continue; } // switch } // with( spam ) BUG = GBUG; } // for(s) if(BUGT)console.timeEnd("dSW"); } //============================================================================ addOnloadHook( deSpamWiki);

//