User:Eirikr/edittools.js

/* Basic flow

* Get existing `select` element, remove all children → All of its `option` children have numeric IDs corresponding to the position of the `p` element containing the special chars to insert. Since we're going to change that, it's safer to have the IDs correspond directly to the IDs of those `p` elements, and for that, it's easier to just create these afresh rather than try to rejigger the existing ones. * Get content of user's `edittools` page → This is retrieved as a text string, not as a DOM object. The content is wrapped in ` ` by the MW parser, so       we need to regex the string to remove this extraneous unwanted. * Prepend this user content to the existing ` ` content. * Create new `select` element, populate with `option` children for all of the `p` elements in ` `

/** * startAjaxGet starts an XmlHttpRequest call on mediawiki. * should be in a library somewhere function startAjaxGet(url,onsuccess,argument){ // See https://en.wikipedia.org/wiki/XMLHttpRequest var req = new XMLHttpRequest; if(!req) return false; // Set up `onreadystatechange` event listener req.onreadystatechange=function{ if( req.readyState == 4 ) // DONE state if( req.status == 200 ) // HTML request return code: 200 = normal success if(typeof(onsuccess)=='function')onsuccess(req.responseText,argument); // responseText: this returns text, NOT a DOM object. };   req.open("GET",url); req.send(); //the  is required for firefox 3b2 return true; } var wet = {}; //Wiktionary Edit Tools global object /** * wet_init starts the process of sorting the edittools, should be called every * page load. function wet_init{ //Check we are in edit mode wet.div=document.getElementById('editpage-specialchars'); if(!wet.div)return false;

// Get existing `select` element, remove all children wet.sel = document.getElementById('charSubsetControl'); wet.sel.innerHTML = "";

// TODO: what are these? Document! wet.ps={};wet.os={};wet.fp=0;

//Get any user edittools section startAjaxGet(       mw.config.get( 'wgScript' ) +'?title=User:'+encodeURIComponent(mw.config.get( 'wgUserName' ))+'/edittools&action=render',        wet_user_ajax    ); //Parse the stuff we already have for(var i=0;i<wet.div.childNodes.length;i++){ var p = wet.div.childNodes[i]; if(p.nodeName.toUpperCase=='P'){ if(p.getAttribute('id')){ wet_add_option(p); }       }    }    //Insert select. wet.div.insertBefore(wet.sel,wet.div.childNodes[0]); wet.sel.setAttribute('id','edittools-select'); try{ wet.sel.addEventListener('change',function{wet_select_set},false); }catch(e){ wet.sel.attachEvent('onchange',function{wet_select_set}); }   wet_select_first; } function wet_user_ajax(resText){ if(resText.indexOf('class="noarticletext"')>-1) return wet_select_first;

// Throwaway div to put the `resText` somewhere so we can use the DOM on it   var div=document.createElement('div'); div.innerHTML=resText;

/* Loop through all the `p` elements in `div` and add to `wet.div`, and to the `select` options. Note that there's `div`, containing then ` ` added by the parser, and the div wrapper from my edittools page to emulate MediaWiki:Edittools. So we dig down to get at the `p` elements. */   div = div.childNodes[0]; for(var i=0;i]*>/g,''); var cn = wet.ps[which].className; cn=cn.replace(/( +)?delayload( +)?/g,''); wet.ps[which].className=cn; }catch(e){ alert("Mediawiki:Edittools/"+which+" contains illegal elements ( s only!)"); } } function wet_add_option(p,before){ var nme=p.getAttribute('id'); p.style.cssText='display:none';

var opt=document.createElement('option'); opt.setAttribute('value',nme); opt.appendChild(document.createTextNode(decodeURIComponent(nme                       .replace(/^edittools-/, '')                        .replace(/\.([0-9A-F][0-9A-F])/g, '%$1')                        .replace(/_/g, '%20')                        )));

if(before){ wet.sel.insertBefore(opt,wet.sel.childNodes[wet.fp]); wet.fp+=1; }else{ wet.sel.appendChild(opt); }   wet.ps[nme]=p; } function wet_select_set(index){ if(typeof(index)=='undefined'&&wet.sel.selectedIndex>-1){ index=wet.sel.selectedIndex; }   if(index>-1&&wet.sel.options[index]){ if(wet.currentIndex>-1&&wet.sel.options[wet.currentIndex]){ wet.ps[ wet.sel.options[wet.currentIndex].getAttribute('value') ].style.cssText='display: none'; }       wet.currentIndex=index; wet.sel.options[index].selected=true; jQuery.cookie('edittoolscharsubset',wet.currentIndex); var which=wet.sel.options[wet.currentIndex].getAttribute('value'); wet.ps[which].style.cssText='display:inline'; if(wet.ps[which].className.indexOf('delayload')>=0){ startAjaxGet(               mw.config.get( 'wgScript' ) +'?title=Mediawiki:Edittools/'+which.replace(/.([0-9A-F]{2})/g,"%$1")+'&action=render',                wet_getset_ajax,which           	); }   } } wet.done=0; function wet_select_first{ wet.done+=1; if(wet.done<2)return false; var index=parseInt(jQuery.cookie('edittoolscharsubset')); if(isNaN(index))index=0; wet_select_set(index); } jQuery(wet_init);