User:Writ Keeper/Scripts/massProtect.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 protectAllTheThings(protectLevel, editSummary)
{
	mw.loader.using('mediawiki.api').done(function ()
	{
		if (editSummary === null)
		{
			return false;
		}
		if (editSummary === '')
		{
			if(protectLevel == "all")
			{
				editSummary = "protection no longer necessary";
			}
			else
			{
				editSummary = "Mass protecting pages due to abuse";
			}
		}
		var api = new mw.Api();
		$(".mw-prefixindex-list a").each(function (ind, el)
		{
			api.postWithEditToken(
			{
				"action": "protect",
				"title": el.title,
				"protections": "edit=" + protectLevel,
				"expiry": "never",
				"reason": editSummary
			}
			).done(function ()
			{
				$(el).after("*");
			}
			);
		}
		);
	}
	);
	return false;
}
mw.hook('wikipage.content').add(function ()
{

	if (mw.config.get("wgCanonicalSpecialPageName") == "Prefixindex")
	{
		mw.loader.using(['mediawiki.util', 'oojs-ui']).done(function ()
		{
			mw.util.addPortletLink('p-cactions', '#', "semiprotect all", "ca-semiprotecteverything", "semiprotect all pages displayed here");
			$("#ca-semiprotecteverything").click(function (event)
			{
				event.preventDefault();
				mw.loader.load('mediawiki.api');
				return protectAllTheThings("autoconfirmed", prompt("Enter an edit summary, or leave blank to use the default (or hit Cancel to cancel the semiprotection entirely)"));
			}
			);
			mw.util.addPortletLink('p-cactions', '#', "te-protect all", "ca-teprotecteverything", "te-protect all pages displayed here");
			$("#ca-teprotecteverything").click(function (event)
			{
				event.preventDefault();
				mw.loader.load('mediawiki.api');
				return protectAllTheThings("templateeditor", prompt("Enter an edit summary, or leave blank to use the default (or hit Cancel to cancel the te-protection entirely)"));
			}
			);
			mw.util.addPortletLink('p-cactions', '#', "unprotect all", "ca-unprotecteverything", "unprotect all pages displayed here");
			$("#ca-unprotecteverything").click(function (event)
			{
				event.preventDefault();
				mw.loader.load('mediawiki.api');
				return protectAllTheThings("all", prompt("Enter an edit summary, or leave blank to use the default (or hit Cancel to cancel the unprotection entirely)"));
			}
			);
		}
		);
	}
}
);