User:Mdaniels5757/PurgeTab.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
/**
 * Add buttons (located in the "more" tab for vector users) to
 * purge, hard purge (forcerecursivelinkupdate=1), and null edit.
 * 
 * See documentation at [[User:Mdaniels5757/PurgeTab]]
 *
 * @source https://en.wikisource.org/wiki/MediaWiki:Gadget-PurgeTab.js
 * @revision revid 9616727 (03:55, September 25, 2019)
 */
 
 jQuery(document).ready(function () {

	if (!mw.config.get('wgArticleId'))
		return;
	var link;

	link = mw.util.addPortletLink(
		'p-cactions', mw.util.getUrl(mw.config.get('wgPageName'), { 'action': 'purge' }),
		(mw.user.options.get( 'skin' ) == 'vector' ) ? "Purge" : "*",
		'ca-purge', "Purge cache for this page", '*'
	);

	link = mw.util.addPortletLink(
	// not sure if raw link is valid for extra params  Using wikiScript instead of getUrl
		'p-cactions', mw.util.wikiScript('api') + '?action=purge&titles=' + mw.config.get('wgPageName') + '&forcerecursivelinkupdate=1&redirects=1',
		(mw.user.options.get( 'skin' ) == 'vector' ) ? "Hard purge" : "**",
		'ca-purge-forcerecursivelinkupdate', "Purge with forced recursive-link table update", ','
	);

	if ( link ) {
		link.addEventListener('click', function (ev) {
			mw.loader.using( 'mediawiki.api' ).done(function() {
				( new mw.Api() ).post({
					action: 'purge',
					pageids: mw.config.get('wgArticleId'),
					forcerecursivelinkupdate: 1,
					redirects: 1
				}).then(function () {
					location.reload();
				}, function (code, details) {
					var mesg;
					switch (code) {
					case 'http':
						mesg = 'HTTP error: ' + details.xhr.statusText;
						break;
					case 'ok-but-empty':
						mesg = 'Received empty response.';
						break;
					default:
						mesg = details.error.info;
					}
					mw.util.jsMessage('<b>Hard purge failed</b>: ' + mesg);
					console.error(arguments);
				} );
			} );
			ev.preventDefault();
		}, false);
	}
	link = mw.util.addPortletLink(
		'p-cactions', 'javascript:void window.warranty',
		(mw.user.options.get( 'skin' ) == 'vector' ) ? "Null edit" : "***",
		'ca-nulledit', "Null edit", '0'
	);

	if ( link ) {
		link.addEventListener('click', function (ev) {
			mw.loader.using( 'mediawiki.api' ).done( function() {
				( new mw.Api() ).post({
					action: 'edit',
					pageid: mw.config.get('wgArticleId'),
					appendtext: '',
					watchlist: 'nochange',
					nocreate: '1',
					token: mw.user.tokens.get('csrfToken')
				}).then(function () {
					location.reload();
				}, function (code, details) {
					var mesg;
					switch (code) {
					case 'http':
						mesg = 'HTTP error: ' + details.xhr.statusText;
						break;
					case 'ok-but-empty':
						mesg = 'Received empty response.';
						break;
					default:
						mesg = details.error.info;
					}
					mw.util.jsMessage('<b>Null edit failed</b>: ' + mesg);
					console.error(arguments);
				} );
			} );
			ev.preventDefault();
		}, false);
	}
} );

/**
* [[Category:User scripts]]
*/