User:Aseleste/Scripts/Purger.js

//

/* Purger */

(function {	/** Configuration */	var config = {		debug: false,		namespace: 'purger',		portletGroup: 'p-cactions',		portletId: 'ca-purger',		portletAccessKey: null,		i18n: {			portletText: 'Purger',			portletTooltip: 'Purge this page',			specialPage: {				url: 'Special:Purger',				documentTitle: 'Purger - Wikipedia',				title: 'Purger: ',				forcelinkupdate: 'forcelinkupdate',				forcerecursivelinkupdate: 'forcerecursivelinkupdate',				mathpurge: 'mathpurge',				purgeButton: 'Purge',				nullButton: 'Null edit', 			},			purgeSucceed: 'Purge succeed',			purgeFailed: 'Purge failed',			nullSucceed: 'Null edit succeed',			nullFailed: 'Null edit failed',			nullSummary: 'Purger: null edit',			nullBug: '(If you see this message, undo this edit, notify the editor, and then report to Aseleste.)',		},	}	function namespaced(string) { return config.namespace + '-' + string } function formatMessage(message) { return '[' + config.namespace + '] ' + message } /** Page source of special page. * Identifiers (without namespace): * - purge * - null * - forcelinkupdate * - forcerecursivelinkupdate * - mathpurge * Classes (without namespace): * - purge-options */	config.specialPageSource = (function {		function generatePurgeOption(name) {			return '' +			'' + config.i18n.specialPage[name] + ' '		}		return generatePurgeOption('forcelinkupdate')			+ generatePurgeOption('forcerecursivelinkupdate')			+ generatePurgeOption('mathpurge')			+ '' + config.i18n.specialPage.purgeButton + ' '			+ '' + config.i18n.specialPage.nullButton + ' '	}) /* Loading */ $.when(		mw.loader.using(['mediawiki.util']),		$.ready	).then(function {		if (mw.config.get('wgPageName').startsWith(config.i18n.specialPage.url)) {			// special page 			writeSpecialPage		} else { 			// add portlet link 			mw.util.addPortletLink(config.portletGroup, mw.util.getUrl(config.i18n.specialPage.url + '/' + mw.config.get('wgPageName')), config.i18n.portletText, config.portletId, config.i18n.portletTooltip, config.portletAccessKey, null);		}	}) function writeSpecialPage { var page = mw.config.get('wgPageName').split(config.i18n.specialPage.url + '/')[1] if (page === undefined) return // no page specified /* Initialization */ function initialize { document.title = config.i18n.specialPage.documentTitle $('h1').text(config.i18n.specialPage.title + page); $('#mw-content-text') .empty .append(config.specialPageSource) $('#' + namespaced('purge')).on('click', doPurge) $('#' + namespaced('null')).on('click', doNullEdit) }		/* Actions */ function doNullEdit(event) { var parameters = { action: 'edit', title: page, summary: (function{					if (config.debug) return config.i18n.nullSummary					else return config.i18n.nullSummary + ' ' + config.i18n.nullBug				}), minor: true, nocreate: true, watchlist: 'nochange', appendtext: '', }			console.log(formatMessage('POST with CSRF token')) console.log(parameters) new mw.Api .postWithToken('csrf', parameters) .then(function(result) {					console.log(formatMessage('Success result'))					console.log(result)					window.location.href = mw.util.getUrl(page)               }, function(result) {					console.log(formatMessage('Failure result'))					console.log(result)					mw.notify(config.i18n.nullFailed, {type: 'error'});				}); event.preventDefault }		function doPurge(event) { var parameters = { action: 'purge', titles: page, }			$('.' + namespaced('purge-options')) .each(function(index, element) { parameters[element.value] = element.checked }) console.log(formatMessage('POST')) console.log(parameters) new mw.Api .post(parameters) .then(function(result) {					console.log(formatMessage('Success result'))					console.log(result)					window.location.href = mw.util.getUrl(page)               }, function(result) {					console.log(formatMessage('Failure result'))					console.log(result)					mw.notify(config.i18n.purgeFailed, {type: 'error'});				}); event.preventDefault }		initialize } })

//