User:Alex 21/script-redlinks.js
Appearance
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 user script seems to have a documentation page at User:Alex 21/script-redlinks. |
$(function($) {
var portletlink = mw.util.addPortletLink('p-tb', '#', 'Remove redlinks');
$(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 [[User:AlexTheWhovian/script-redlinks|script]].";
} else {
alert('No redlinks in the article! (Check the navigational boxes.)');
}
localStorage.redlinks = '';
}
});
});