User:Dr pda/editrefs.js
Appearance
From Wikipedia, the free encyclopedia
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.
This code will be executed when previewing this page.
Documentation for this user script can be added at User:Dr pda/editrefs.
//This function adds a link to the toolbox which, when clicked, searches the article for <ref></ref> //tags and presents them in textboxes for ease of editing //To use this function add importScript('User:Dr pda/editrefs.js'); //[[User:Dr pda/editrefs.js]] to your monobook.js // functioneditRefs(){ //Hide edit window vareditform=document.getElementById('editform'); editform.style.cssText+='display:none'; text=document.getElementById('wpTextbox1').value; //Hack to cope with HTML comments text=text.replace(/-->/g,"--@#%"); // var refs = text.match(/<ref[^/]*\/ref>|<ref[^/]*\/>/g); refs=text.match(/<[Rr]ef(\s*name=[^\>]*)?>[^>]*<\/[Rr]ef>/g); text=text.replace(/--@#%/g,"-->"); varoutput=document.createElement("ol"); output.id='ref-edit-ol'; if(refs!=null){ for(x=0;x<refs.length;x++){ //Hack to cope with HTML comments refs[x]=refs[x].replace(/--@#%/g,"-->"); varref_textbox=document.createElement("textarea"); ref_textbox.id='ref-box-'+x; varref_li=document.createElement("li"); ref_li.id='ref-li-'+x; ref_textbox.value=refs[x]; varnewlines=refs[x].match(/\n/g); varlines=(newlines==null)?1:newlines.length+1; ref_textbox.rows=(lines>refs[x].length/70)?lines:refs[x].length/70; ref_textbox.cols=70; ref_textbox.style.cssText='display:block;'; ref_li.appendChild(ref_textbox); output.appendChild(ref_li); } } else{ varref_li=document.createElement("li"); ref_li.id='ref-li'; ref_li.innerHTML='This article contains no <ref></ref> tags'; output.appendChild(ref_li); } vardummy=document.getElementById("editform"); dummy.parentNode.insertBefore(output,dummy); //Add buttons varupdate=document.createElement("input"); update.id='ref-edit-update'; update.value='Apply changes and preview'; update.title='Apply changes and preview'; update.type='button'; update.onclick=updateRefs; varcancel=document.createElement("input"); cancel.id='ref-edit-cancel'; cancel.value='Cancel and return to edit'; cancel.title='Cancel and return to edit'; cancel.type='button'; cancel.onclick=returnToEdit; varreset=document.createElement("input"); reset.id='ref-edit-reset'; reset.value='Reset fields'; reset.title='Reset fields'; reset.type='button'; reset.onclick=resetRefs; if(refs!=null){ output.parentNode.insertBefore(update,output.nextSibling); update.parentNode.insertBefore(cancel,update.nextSibling); cancel.parentNode.insertBefore(reset,cancel.nextSibling); } else{ output.parentNode.insertBefore(cancel,output.nextSibling); } } functionupdateRefs(){ varstartIndex=-1; for(x=0;x<refs.length;x++){ varnewref=document.getElementById('ref-box-'+x).value; startIndex=text.indexOf(refs[x],startIndex); //Only update if changed if(refs[x]!=newref){ text=text.substring(-1,startIndex)+newref+text.substring(startIndex+refs[x].length); } startIndex=startIndex+newref.length; } vartextbox=document.getElementById('wpTextbox1'); textbox.value=text; //returnToEdit(); document.getElementById('wpPreview').click(); } functionreturnToEdit(){ //Hide textboxes and buttons varoutput=document.getElementById('ref-edit-ol'); output.parentNode.removeChild(output); varupdate=document.getElementById('ref-edit-update'); if(update)update.parentNode.removeChild(update); varcancel=document.getElementById('ref-edit-cancel'); if(cancel)cancel.parentNode.removeChild(cancel); varreset=document.getElementById('ref-edit-reset'); if(reset)reset.parentNode.removeChild(reset); //Show edit window vareditform=document.getElementById('editform'); editform.style.cssText=''; } functionresetRefs(){ for(x=0;x<refs.length;x++){ varref_textbox=document.getElementById('ref-box-'+x); ref_textbox.value=refs[x]; } } $(function(){ if(document.forms.editform){ mw.util.addPortletLink('p-tb','javascript:editRefs()','Edit references','t-edit-refs','Edit <ref></ref> tags','',''); } });