MediaWiki:Gadget-NullEditButton.js

Jump to navigation Jump to search

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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/**
 * Null edit button gadget.
 * Adds a button to on the "More" menu to quickly null edit a page.
 * Adapted from https://yugipedia.com/wiki/MediaWiki:Gadget-NullEditButton.js
 */
( function _gadgetNullEditButton( window, $, mw, console ) {
	"use strict";

	var LAST_LOG = '11:54, 7 January 2023 (UTC)';

	var config = mw.config.get( [ 'wgPageName' ] );

	var disabledClass = 'is-disabled';

	var $content = $( '#bodyContent' );

	var $nullEditbutton = $( '<li>', {
		id: 'ca-null',
		'class': 'mw-list-item',
		html: $( '<a>', {
			href: '#',
			title: 'Null edit this page',
			html: $( '<span>', {
				text: 'Null edit'
			} )
		} ),
		click: function( e ) {
			e.preventDefault();

			notify( 'Waiting for mediawiki.api module to load.', 'fail' );
		}
	} );

	var $loadingSpinner = $( '<span>', {
		id: 'null-edit-spinner',
		'class': 'smw-overlay-spinner large inline'
	} );

	function notify( message, type ) {
		return mw.notify( message, {
			title: 'Null edit',
			tag: 'null_edit',
			type: type,
			autoHide: !mw.config.get( 'debug' )
		} );
	}

	function loadContent() {
		$content.load( window.location.href + ' #bodyContent > *', function( response, status, jqXHR ) {
			if ( jqXHR.status !== 200 ) {
				throw new Error( 'Error loading content after successful null edit!' );
			}

			// Cleanup page appearance:
			$content.removeClass( disabledClass );

			$loadingSpinner.remove();
			
			mw.hook( 'wikipage.content' ).fire( $content );
			
			notify( 'Null edit success!', 'success' );
		} );
	}

	function handleFail() {
		// Cleanup page appearance:
		$content.removeClass( disabledClass );

		$loadingSpinner.remove();

		notify( 'Null edit fail!', 'fail' );

		mw.log( '[Gadget] [NullEditButton] Null edit fail.', arguments );
	}

	function handleNullEditClick( e ) {
		e.preventDefault();

		notify( 'Null editing...', 'progress' );

		$content
			.addClass( disabledClass )
			.parent()
				.prepend( $loadingSpinner );

		new mw.Api()
			.postWithEditToken( {
				action: 'edit',
				title: config.wgPageName,
				summary: 'Something bad happened! This was supposed to be a null edit!',
				prependtext: ''
			} )
			.done( loadContent )
			.fail( handleFail );
	}

	function init() {
		if ( $( '#ca-view' ).hasClass( 'selected' ) ) {
			$( '#p-cactions' )
				.removeClass( 'emptyPortlet' )
				.find( 'ul' )
					.append( $nullEditbutton );

			// Wait for the mw API module to load, just to be sure nothing breaks
			mw.loader.using( 'mediawiki.api' ).done( function() {
				mw.log( '[Gadget] [NullEditButton] - mediawiki.api loaded.' );

				$nullEditbutton
					.off( 'click' ) // Remove fallback click.
					.click( handleNullEditClick );
			} );
		}
	}

	$( init );

	console.log( '[Gadget] [NullEditButton] last updated at', LAST_LOG );

} )( window, window.jQuery, window.mediaWiki, window.console );