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.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/* * wiksort.js * Version 1.1 * Sort out unique words in Wikipedia articles to separate page * pop-ups must be enabled for the wikipedia ___domain * works best with Firefox due to its built-in spelling checker for textareas * * Written by Michael Devore * Comments to: http://en.wikipedia.org/wiki/User_talk:Michael_Devore * Released to the public ___domain*/varwiksGlobals={regularNounCutoff:6,properNounCutoff:6,baseApiString:mw.config.get('wgServer')+mw.config.get('wgScriptPath')+"/api.php?action=parse&format=xml&page=",wiksText:"Sort words",wiksId:"t-sort-words",wiksToolTip:"Sort unique article words"};functionwiksMain(){varwikiPage=mw.config.get('wgPageName').replace('_',' ');// wiksLoadPageXML(wikiPage);wiksLoadPageXML(encodeURIComponent(wikiPage));}functionwiksLoad(){if(mw.config.get('wgCanonicalNamespace')=="Special"){return;}// set up the tool linkif(mw.config.get('skin')!="cologneblue"){mw.util.addPortletLink('p-tb','javascript: wiksMain();',wiksGlobals.wiksText,wiksGlobals.wiksId,wiksGlobals.wiksToolTip);}else{// my favorite cologne blue skin doesn't support portlet links// place sort word link right before 'This page' quickbar section,// hopefully as last entry in previous 'Edit' sectionvarquickEl=document.getElementById("quickbar");varchildNode=quickEl?quickEl.firstChild:null;while(childNode){vartNode=childNode.firstChild;if(tNode&&tNode.nodeName=="#text"&&tNode.nodeValue=="This page"){break;}childNode=childNode.nextSibling;}if(childNode){varaBR=document.createElement("BR");varaEl=document.createElement("A");aEl.setAttribute("href","javascript: wiksMain();");aEl.appendChild(document.createTextNode(wiksGlobals.wiksText));quickEl.insertBefore(aEl,childNode);quickEl.insertBefore(aBR,childNode);}}}functionwiksLoadPageXML(wikiPage){varwikiApiString=wiksGlobals.baseApiString+wikiPage;varrequest=newXMLHttpRequest();request.open("GET",wikiApiString,true);request.setRequestHeader("User-Agent","Mozilla/5.0");request.setRequestHeader("Accept","text/xml");request.onreadystatechange=function(){if(request.readyState==4&&request.status==200){if(request.responseText){wiksProcessPage(request.responseText);}}};request.send(null);}functionwiksProcessPage(pText){// var pDoc = (new DOMParser()).parseFromString(pText, "application/xml");varpDoc;varisIE=false;try//Internet Explorer{pDoc=newActiveXObject("Microsoft.XMLDOM");pDoc.async="false";pDoc.loadXML(pText);isIE=true;}catch(e){try//Firefox, Mozilla, Opera, etc.{vardParser=newDOMParser();pDoc=dParser.parseFromString(pText,"application/xml");}catch(e){alert("wiksort could not process this page.");returnfalse;}}vartElements=pDoc.getElementsByTagName("text");vartElem=tElements[0];vard=document.createElement("div");varhtmlContent="";varcNode=tElem.firstChild;while(cNode){htmlContent+=cNode.nodeValue;cNode=cNode.nextSibling;}d.innerHTML=htmlContent;vartheText;if(isIE){theText=d.innerText;}else{theText=d.textContent;}wiksFilterAndShow(theText);returntrue;}functionwiksFilterAndShow(articleText){varregNoun=newRegExp("\\b[a-z][a-zA-Zé]{"+(wiksGlobals.regularNounCutoff-1)+",}","g");propNoun=newRegExp("\\b[A-Z][a-zA-Zé]{"+(wiksGlobals.properNounCutoff-1)+",}","g");varresultLC=newArray();// initial capped/proper nounsvarresult1=articleText.match(propNoun);// any nounvarresult2=articleText.match(regNoun);varresult;if(result1!=null){result=result1;if(result2!=null){result=result1.concat(result2);}}else{result=result2;}if(result){result.sort(function(x,y){vara=String(x).toUpperCase();varb=String(y).toUpperCase();if(a>b){return1;}if(a<b){return-1;}return0;});varwordCount=newArray();for(vari=0;i<result.length;i++){varlcWord=result[i].toLowerCase();if(lcWord=="reverse"||lcWord=="constructor"||lcWord=="every"||lcWord=="reduce"||lcWord=="splice"||lcWord=="filter"){lcWord+="_x_wiks";}if(wordCount[lcWord]!==undefined){wordCount[lcWord]++;}else{wordCount[lcWord]=1;}}wiksShowResults(result,wordCount);}}functionwiksShowResults(wordList,wordCount){varfeedback="";varfeedFormat="<html><head><title>";varfeedTitle="Sorted words in "+window.___location.href.match(/[^\/]+$/);feedTitle=feedTitle.replace(/_/g," ");feedFormat+=feedTitle;feedFormat+='</title><body>';feedFormat+="=="+feedTitle+'==<br><span id="wiksort">';varpreviousWord="";varrowCount=1;varlastLetter="";for(vari=0;i<wordList.length;i++){varlcWord=wordList[i].toLowerCase();varadjustWord=lcWord;if(lcWord=="reverse"||lcWord=="constructor"||lcWord=="every"||lcWord=="reduce"||lcWord=="splice"||lcWord=="filter"){adjustWord+="_x_wiks";}varcount=wordCount[adjustWord];if(lcWord!==previousWord+"s"){varstartLetter=lcWord.slice(0,1);if(lastLetter!=startLetter){lastLetter=startLetter;feedFormat+="\n<br> == "+startLetter.toUpperCase()+" ==\n<br>";rowCount+=2;}feedback+=wordList[i]+"("+count+") - "+(((i+1)%10==0)?"\n":"");feedFormat+=wordList[i]+" ("+count+") \n<br>";rowCount++;}i+=count-1;previousWord=lcWord;}feedFormat+="</span>\n";feedFormat+='<textarea id="sorttext" wrap="hard" rows="';feedFormat+=rowCount+'"></textarea>\n';feedFormat+='<script language="JavaScript">\n';feedFormat+='var st=document.getElementById("sorttext");\n';feedFormat+='var sortSpan=document.getElementById("wiksort");\n';feedFormat+='st.style.width="340px";\n';feedFormat+='st.style.fontSize="large";\n';feedFormat+='st.value=(sortSpan.textContent === undefined ? sortSpan.innerText : sortSpan.textContent);\n';feedFormat+='sortSpan.style.display="none";\n';feedFormat+='</script>';feedFormat+="</body></html>";if(feedback.length){varfeedWin=null;variCounter=0;while((feedWin=window.open())===null&&iCounter<10){iCounter++;}if(iCounter>=10){alert("You need to enable pop-ups for the Wikipedia site\n"+" (or the browser doesn't work with wiksort)\n");}varfeedDoc=feedWin.document;feedDoc.open();feedDoc.write(feedFormat);feedDoc.close();}else{alert("wiksort found no sortable words.");}}addOnloadHook(wiksLoad);