User:The Transhumanist/redlinks Feb2016.js

// Feb 28 2016 version of User:AlexTheWhovian/script-redlinks.js $(function($) {	var portletlink = mw.util.addPortletLink('p-tb', '#', 'Rmv rdlnks0');	$(portletlink).click( function(e) { e.preventDefault; var loc = window.location.href; var redlinks; var i;		if (loc.indexOf('action=edit') < 0 && loc.indexOf('action=submit') < 0) { redlinks = []; var a = 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]); }			}			if (redlinks.length > 0) { localStorage.redlinks = JSON.stringify(redlinks); alert('You will need to click this link again after the editing page loads to remove the redlinks.'); window.location = window.location.href.substr(0, window.location.href.indexOf('#'))+"?action=edit"; } else { alert('No redlinks!'); }		} else { var totalredlinks = 0; RegExp.quote = function(str) { return str.replace(/[.?*+^$[\]\\{}|-]/g, "\\$&"); }; redlinks = JSON.parse(localStorage.redlinks); var wpTextbox1 = document.getElementById('wpTextbox1'); for (i = 0; i < redlinks.length; i++) { var reglink1 = new RegExp('\\[\\[\\s*('+RegExp.quote(redlinks[i])+')\\s*\\|\\s*([^\\]]*)\\s*\\]\\]','gi'); var reglink2 = new RegExp('\\[\\[\\s*('+RegExp.quote(redlinks[i])+')\\s*\\]\\]','gi'); if (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 if (redlinks[i].substr(0,9) == "Category:") { var reglink3 = new RegExp('\\[\\[\\s*('+RegExp.quote(redlinks[i])+')\\s*\\]\\]\\n','gi'); wpTextbox1.value = wpTextbox1.value.replace(reglink3,""); }				wpTextbox1.value = wpTextbox1.value.replace(reglink1,"$2"); wpTextbox1.value = wpTextbox1.value.replace(reglink2,"$1"); }			if (totalredlinks > 0) { document.getElementById('wpSummary').value += "Removed "+totalredlinks+" redlink"+(totalredlinks==1?"":"s")+" via script."; alert('Redlinks removed.'); } else { alert('No redlinks in the article! (Check the navigational boxes.)'); }			localStorage.redlinks = ''; }	}); });