User:Epicgenius/blank sandbox.js

From Wikipedia, the free encyclopedia
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.
// /* Code adapted from [[User:DreamRimmer/BlankUserPage.js]] */
// <nowiki>
var config = {};

$.when(
	// Resource loader module
	mw.loader.using( [ 'mediawiki.util', 'mediawiki.api' ] ),
	// Page ready
	$.ready
).then( function() {

	// MediaWiki configuration 
	config.mw = mw.config.get( [
		"wgPageName",
		"wgNamespaceNumber",
	]);

	var API = new mw.Api({
		ajax: {
			headers: {
				"Api-User-Agent": "userPageBlanking"
			}
		}
	});

	// Setup
	var isUserPage = config.mw.wgNamespaceNumber === 2 || config.mw.wgNamespaceNumber === 3;
	if (isUserPage) {
		mw.util.addPortletLink('p-cactions', "#", 'BlankSandbox', 'ca-blankSandbox', "Blank sandbox", null, "#ca-move");
		$('#ca-blankSandbox').on('click', function() {
			console.log("About to blank " + config.mw.wgPageName);
			blankSandbox(config.mw.wgPageName)
				.then(function() { window.location.reload(); });
		});
		mw.util.addPortletLink('p-cactions', "#", 'BlankPage', 'ca-blankUPage', "Blank User page", null, "#ca-move");
		$('#ca-blankUPage').on('click', function() {
			console.log("About to blank " + config.mw.wgPageName);
			blankUserPage(config.mw.wgPageName)
				.then(function() { window.location.reload(); });
		});
		return;
	}

	function blankSandbox(UserPage) {
		let editSummary = prompt("Reason for blanking page","Blanked sandbox");
		if (editSummary == null || editSummary == "") {
			console.log("Not blanking the page");
			return Promise.resolve();
		}
		var queryParams = {
			action: "edit",
			text: "{{user sandbox}}",
			summary: editSummary + " using [[User:Epicgenius/blank sandbox.js|blank sandbox]]",
			nocreate: true,
			title: UserPage
		};
		return API.postWithToken("csrf", queryParams);
	}
	
	function blankUserPage(userPage) {
		let editSummary = prompt("Reason for blanking page","Blanked per [[WP:UPNOT]]");
		if (editSummary == null || editSummary == "") {
			console.log("Not blanking the page");
			return Promise.resolve();
		}
		var queryParams = {
			action: "edit",
			text: "",
			summary: editSummary + " using [[User:Epicgenius/blank sandbox.js|blank sandbox]]",
			nocreate: true,
			title: userPage
		};
		return API.postWithToken("csrf", queryParams);
	}
});
// </nowiki>