MediaWiki:Gadget-Checkbox.js

De Hispanopedia
Revisión del 03:19 23 ago 2025 de Supremo (discusión | contribs.) (Página creada con «var TemplateCheckbox = { init: function () { $( '.template-checkbox' ).click( TemplateCheckbox.toggle ); }, toggle: function () { var checkbox = $( this ); // Only autoconfirmed users to prevent vandalism var groups = mw.config.get( 'wgUserGroups' ); if ( !groups.includes( 'autoconfirmed' ) ) { return; } // Toggle the checkbox in the frontend var check = checkbox.text(); if ( check ) { checkbox.empty(); } else { checkbox.text( '✓'…»)
(difs.) ← Revisión anterior | Revisión actual (difs.) | Revisión siguiente → (difs.)

Nota: Después de publicar, quizás necesite actualizar la caché de su navegador para ver los cambios.

  • Firefox/Safari: Mantenga presionada la tecla Shift mientras pulsa el botón Actualizar, o presiona Ctrl+F5 o Ctrl+R (⌘+R en Mac)
  • Google Chrome: presione Ctrl+Shift+R (⌘+Shift+R en Mac)
  • Edge: mantenga presionada Ctrl mientras pulsa Actualizar, o presione Ctrl+F5
var TemplateCheckbox = {

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

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

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

		// 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 );