User:Writ Keeper/Scripts/elRemover.js

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.
function removeELsWK()
{
	var textToReplace = $("#wpTextbox1").text();
	var elRegex = /([^>] ?)\[http[^ \]]+ ?([^\]]*)\]/g
	var numberOfMatches = textToReplace.match(elRegex).length;
	$("#wpTextbox1").text(textToReplace.replaceAll(elRegex, "$1$2"));
	alert("Detected and replaced " + numberOfMatches + " external links; please preview changes to validate the replacements.");
}

$(document).ready(function ()
{
	if ((mw.config.get("wgAction") === "edit" || mw.config.get("wgAction") === "submit") && mw.config.get("wgNamespaceNumber") === 0)
	{

		mw.loader.using("mediawiki.util").done( function ()
		{
			mw.util.addPortletLink('p-cactions', '#', "remove external links", "ca-remove-els", "remove all external links in this section");
			$("#ca-remove-els").click( function(event)
			{
				event.preventDefault();
				return removeELsWK();
			});
		});
	}
});