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.
$(document).ready(function(){// Remove redlinks if they exist upon pageloadredlinks_removeall();});$(function($){mw.loader.using(['mediawiki.util']).then(function(){varportletlink=mw.util.addPortletLink('p-tb','#','Remove redlinks');$(portletlink).click(function(e){e.preventDefault();// Default parameters, and begin script on regular view of articlevarloc=window.location.href;varredlinks;vari;// Gather all redlinks with class "new"redlinks=[];vara=document.getElementsByTagName('a');for(i=0;i<a.length;i++){if(a[i].getAttribute('class')=="new"){redlinks[redlinks.length]=a[i].href.replace('https://en.wikipedia.org/w/index.php?title=','').replace('&action=edit&redlink=1','');redlinks[redlinks.length-1]=redlinks[redlinks.length-1].replace(/_/g,' ');redlinks[redlinks.length-1]=decodeURIComponent(redlinks[redlinks.length-1]);}}// Save all redlinksif(redlinks.length>0){localStorage.redlinks=JSON.stringify(redlinks);// If we are in the edit page, then remove redlinks automatically; if we are on the reading page, then go to the edit pageif(window.location.href.indexOf('action')>=0)redlinks_removeall();else{varpageTitle;if(window.location.href.indexOf('?title=')>=0){varregex=/\?title=(.+?)&/;varregexMatch=window.location.href.match(regex);pageTitle=regexMatch[1];}else{pageTitle=window.location.href;if(window.location.href.indexOf('#')>=0)pageTitle=pageTitle.substr(0,pageTitle.indexOf('#'));pageTitle=pageTitle.substr(window.location.href.indexOf('/wiki/')+6);}window.location.href="https://en.wikipedia.org/wiki/"+pageTitle+"?action=edit";}}else{alert('No redlinks!');}});});});functionredlinks_removeall(){varwpTextbox1=document.getElementById('wpTextbox1');if(!wpTextbox1)return;// Automatic removal of redlinks when stored.if(localStorage.redlinks!==""&&localStorage.redlinks!==undefined){// Gather saved redlinkstry{varredlinks=JSON.parse(localStorage.redlinks);}catch(e){return;}// Regular expression to escape special charactersvartotalredlinks=0;RegExp.quote=function(str){returnstr.replace(/[.?*+^$[\]\\(){}|-]/g,"\\$&");};for(i=0;i<redlinks.length;i++){// Regular expression for piped links and direct linksvarreglink1=newRegExp('\\[\\[\\s*('+RegExp.quote(redlinks[i]).replace(/\s/g,"[\\s\\_]*")+')\\s*\\|\\s*([^\\]]*)\\s*\\]\\]','gi');varreglink2=newRegExp('\\[\\[\\s*('+RegExp.quote(redlinks[i]).replace(/\s/g,"[\\s\\_]*")+')\\s*\\]\\]','gi');// Add total number of matches for bothif(wpTextbox1.value.match(reglink1)!==null)totalredlinks+=wpTextbox1.value.match(reglink1).length;if(wpTextbox1.value.match(reglink2)!==null)totalredlinks+=wpTextbox1.value.match(reglink2).length;// Includes categories// Remove category rather than simply convert it to unlinked textif(redlinks[i].substr(0,9)=="Category:"){varreglink3=newRegExp('\\[\\[\\s*('+RegExp.quote(redlinks[i])+')\\s*\\]\\]\\n','gi');wpTextbox1.value=wpTextbox1.value.replace(reglink3,"");}// Remove redlinks and convert to unlinked textwpTextbox1.value=wpTextbox1.value.replace(reglink1,"$2");wpTextbox1.value=wpTextbox1.value.replace(reglink2,"$1");}// Alert for summary of removed redlinks; total in edit summaryif(totalredlinks>0){document.getElementById('wpSummary').value+="Removed "+totalredlinks+" redlink"+(totalredlinks==1?"":"s")+" via [[User:Alex 21/script-redlinks|script]].";alert("Automatically removed "+totalredlinks+" redlink"+(totalredlinks==1?"":"s")+"!");}else{// Redlinks were seen in the article but none were found in the wikicode - check the nevigational boxes for these redlinksalert('No redlinks in the article! (Check transcluded templates and the navigational boxes.)');}// Remove the template(s)wpTextbox1.value=wpTextbox1.value.replace(/\{\{[Cc]leanup-?\s*[Rr]ed\s*[Ll]inks?[^\}]*\}\}/g,"");wpTextbox1.value=wpTextbox1.value.replace(/\{\{[Rr]ed\s*links?[^\}]*\}\}/g,"");wpTextbox1.value=wpTextbox1.value.replace(/\{\{[Tt]oo many red links[^\}]*\}\}/g,"");// Clear all saved redlinkslocalStorage.redlinks='';}}