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.
TemplateCheckbox = {

	init: function () {
		$( '.template-checkbox' ).click( TemplateCheckbox.toggle );
	},

	toggle: function () {
		var checkbox = $( this );

		var groups = mw.config.get( 'wgUserGroups' );
		if ( !groups.includes( 'autoconfirmed' ) ) {
			return; // Only autoconfirmed users to prevent vandalism
		}

		// Toggle the checkbox in the frontend
		var check = checkbox.text();
		if ( check ) {
			checkbox.empty();
		} else {
			checkbox.text( '✓' );
		}

		// Figure out the index of the current checkbox
		var index = 0;
		var current = this;
		$( '.template-checkbox' ).each( function () {
			index++;
			if ( current === this ) {
				return false;
			}
		} );

		// Toggle the checkbox in the backend
		var page = mw.config.get( 'wgPageName' );
		new mw.Api().edit( page, function ( revision ) {

			// Replace the nth occurrence of '{{Checkbox'
			var search = '\\{\\{Checkbox\\|?[^}]*';
			var regexp = RegExp( '^(?:[\\s\\S]*?' + search +  '){' + index + '}', 'gi' );
			var text = revision.content.replace( regexp, function ( match ) {
				var replacement = '|✓';
				if ( check ) {
					replacement = '';
				}
				return match.replace( RegExp( search + '$' ), '{{Checkbox' + replacement );
			} );

			// Return the edit params
			return {
				'text': text,
				'summary': ( check ? 'Uncheck' : 'Check' ),
				'minor': true
			};
		} );
	}
};

$( TemplateCheckbox.init );
Cookies help us deliver our services. By using our services, you agree to our use of cookies.