MediaWiki:Gadget-autodel.js

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
// Outline for an automatic delete link generator.
// Not highly tested. Started by Splarka @ test.wikipedia
// Written for & maintained by Mike_lifeguard @ en.wikibooks
// Rewritten by [[m:User:Hoo man]]
// https://meta.wikimedia.org/wiki/MediaWiki:Gadget-autodel.js

mw.loader.using( ['mediawiki.util', 'mediawiki.cookie'], function() {
	function portletOnClick() {
		var reason = prompt( 'Enter default delete reason', '' );
		if( !reason ) {
			return false;
		}
		var links = document.getElementById( 'bodyContent' ).getElementsByTagName( 'a' );
		var arp = mw.config.get( 'wgServer' ) + mw.config.get( 'wgArticlePath' ).replace( '$1', '' );
		if( links.length === 0 ) {
			return false;
		}
		// random token, to prevent CSRF deletions
		// https://bugzilla.wikimedia.org/show_bug.cgi?id=40743
		var autoDeleteToken = ( Math.ceil( Math.random() * 20000000000 ) );
		// expire after 45 min
		var date = new Date();
		date.setTime( date.getTime() + ( 45 * 60 * 1000 ) );
		// save to cookie
		$.cookie( 'autoDeleteToken', autoDeleteToken, { expires: date, path: '/' } );
		for( var i=0; i<links.length; i++ ) {
			if( links[i].href.indexOf( arp ) === 0 && links[i].href.indexOf( 'action=delete' ) === -1 &&
				links[i].href.indexOf( '/' + mw.config.get( 'wgFormattedNamespaces' )[-1] + ':' ) === -1
			) {
				links[i].href += '?action=delete&submitdelete=' + autoDeleteToken + '&deletetalkalso=true&wpReason=' + encodeURIComponent( reason );
				links[i].innerHTML += ' (delete)';
			}
		}
		return false;
	}
	// Auto delete
	if( mw.util.getParamValue( 'submitdelete' ) ) {
		if( mw.util.getParamValue( 'submitdelete' ) === $.cookie( 'autoDeleteToken' ) && !mw.util.getParamValue( 'deletetalk' ) ) {
			$( document ).ready( function() {
				if( document.getElementById( 'ca-talk' ).className.indexOf( 'new' ) === -1 &&
					mw.util.getParamValue( 'deletetalkalso' ) === 'true' &&
					( mw.config.get( 'wgNamespaceNumber' ) % 2 === 0 )
				) {
					// if the talk tab isn't redlinked and this is the first time AND this is not a talk page then
					// add the parameters necessary to delete the talk page
					document.getElementById( 'deleteconfirm' ).action += '&deletetalk=true&submitdelete=' + $.cookie( 'autoDeleteToken' );
					document.getElementById( 'deleteconfirm' ).action += 'wpReason=' + encodeURIComponent( mw.util.getParamValue( 'wpReason' ) );
				}
				// set the delete reason and auto delete
				$( '#wpReason' ).attr( 'value', mw.util.getParamValue( 'wpReason' ) );
				var sub = ( document.getElementById( 'wpConfirmB' ) ) ? document.getElementById( 'wpConfirmB' ) : document.getElementById( 'mw-filedelete-submit' );
				sub.click();
			} );
		}else if( mw.util.getParamValue( 'submitdelete' ) && mw.util.getParamValue( 'submitdelete' ) !== $.cookie( 'autoDeleteToken' ) ) {
			// Invalid token
			alert( 'Invalid autoDeleteToken, please recreate the auto delete links!' );
			return;
		}

		// The "main" page has already been deleted, go on with the talk
		if(mw.config.get( 'wgAction' ) === 'delete' && mw.util.getParamValue( 'deletetalk' ) === 'true' && mw.util.getParamValue( 'submitdelete' ) === $.cookie( 'autoDeleteToken' ) ) {
			$( document ).ready( function() {
				var url = document.getElementById( 'ca-talk' ).getElementsByTagName( 'a' )[0].href;
				url += '?action=delete&submitdelete=' + $.cookie( 'autoDeleteToken' ) + '&wpReason=' + encodeURIComponent( mw.util.getParamValue( 'wpReason' ) );
				document.location.href = url;
			} );
		}else if( mw.util.getParamValue( 'submitdelete' ) && mw.util.getParamValue( 'submitdelete' ) !== $.cookie( 'autoDeleteToken' ) ) {
			// Invalid token
			alert( 'Invalid autoDeleteToken, please recreate the auto delete links!' );
			return;
		}
	}
	// Init portlet IF contribs or log or newpages or [article AND (submit|view|purge)]
	if( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Contributions' || mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Whatlinkshere' ||
		mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Log' || mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Prefixindex' ||
		mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Newpages' ||
		( mw.config.get( 'wgIsArticle' ) && ( mw.config.get( 'wgAction' ) === 'submit' || mw.config.get( 'wgAction' ) === 'view' || mw.config.get( 'wgAction' ) === 'purge' ) )
	) {
		$( document ).ready( function() {
			// portlet link with onClick
			$(
				mw.util.addPortletLink( 'p-cactions', '#', 'autodelete', 'ca-dellink', 'generate deletion links' )
			).on( 'click', portletOnClick );
		} );
	}
} );