Usuario:Cethos/MediaWiki:Gadget-FastButtons.js

De Wikiversidad

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)
  • Internet Explorer/Edge: mantenga presionada Ctrl mientras pulsa Actualizar, o presione Ctrl+F5
  • Opera: Presiona Ctrl+F5.
/**
 * FastButtons de Racso
 * <nowiki>
 */
// Protege contra doble inclusión
if ( window.fastButtons === undefined ) {

    importScript( 'Special:Mypage/FB.js' );

   //Checker
    var fastButtons = {
        FBList: '',
        ufb: '',
        wikiURL: mw.util.wikiScript( 'index' ) + '?title=',
        isSysop: false
    };

    //Enlaza con window
    window.fastButtons = fastButtons;

    //Scope local para no contaminar espacio global
    (function ( $, mw ) {
        'use strict';
        //*********************************************************************
        //Funciones generales
        //*********************************************************************
        //readInputButton: ejecuta el código de un botón de acción que lee datos. Lee un dato del usuario.
        fastButtons.readInputButton = function ( mensaje, buttonType, wikiAction ) {
            wikiAction = wikiAction || 'edit';
            var input = window.prompt( decodeURI( mensaje ) );
            if ( input != null ) {
                location.href = fastButtons.wikiURL + wikiAction + '&' + buttonType + '=' + encodeURI( input );
            }
            return false;
        };

        //addTemplate: añade una plantilla al inicio de la página, y guarda.
        function addTemplate( plantilla, resumen ) {
            $( '#wpTextbox1' ).val( plantilla + '\n' + $( '#wpTextbox1' ).val() );
            $( '#wpSummary' ).val( resumen + fastButtons.ufb );
            $( '#editform' ).submit();
        }

        //addTemplateEnd: añade una plantilla al final de la página, y guarda.
        function addTemplateEnd( plantilla, resumen ) {
            $( '#wpTextbox1' ).val( $( '#wpTextbox1' ).val() + '\n' + plantilla );
            $( '#wpSummary' ).val( resumen + fastButtons.ufb );
            $( '#editform' ).submit();
        }

        //addFastButton: genera la cadena para añadir un botón, tomando ciertos parámetros.
        function addFastButton( buttonLabel, buttonType, typeParam, wikiAction ) {
            typeParam = typeParam || buttonLabel;
            wikiAction = wikiAction || 'edit';
            return '[<a href="' + fastButtons.wikiURL + wikiAction + '&' + buttonType + '=' +
                encodeURI( typeParam ) + '">' + buttonLabel + '<\/a>] ';
        }

        //addActionFastButton: genera la cadena para añadir un botón de acción.
        function addActionFastButton( buttonLabel, accion ) {
            return '[<a href="#" onclick="return ' + accion + ';">' + buttonLabel + '<\/a>] ';
        }

        //addBlockFastButton: genera la cadena para añadir un botón de bloqueo rápido. Es un botón con código más específico,
        //por lo que se hizo por aparte en vez de complicar las demás funciones generalizándolas tanto.
        function addBlockFastButton() {
            var targetName = '',
                urlHref = location.href;
            if ( urlHref.match( "Especial:Bloquear/" ) ) {
                targetName = decodeURI( urlHref.substring( urlHref.indexOf( "Especial:Bloquear/" ) + 18 ) );
            } else if ( urlHref.match( "Especial:Contribuciones/" ) ) {
                targetName = decodeURI( urlHref.substring( urlHref.indexOf( "Especial:Contribuciones/" ) + 24 ) );
            }

            if ( !targetName ) {
                return '';
            }

            return '[<a href="' + mw.util.wikiScript( 'index' ) + '?title=Especial:Bloquear/' +
                targetName + '&FBblock=31">31h, vandalismo<\/a>] ';
        }

        //*********************************************************************
        //Funciones de carga [por defecto] de los botones.
        //*********************************************************************
        //BOTONES PARA NO-SYSOP
        function loadNoSysop() {
            var tmp = '';

            tmp += addFastButton( 'Vandalismo', "FBdest" );
            tmp += addFastButton( 'Pruebas de edición', "FBdest" );
            tmp += addFastButton( 'Promocional', "FBdest" );
            if( $.inArray( mw.config.get( 'wgNamespaceNumber' ), [ 0, 104 ] ) > -1 ) {
                tmp += addFastButton( 'Fuera del alcance del proyecto', "FBdest" );
                tmp += addFastButton( 'Infraesbozo', "FBdest" );
            }
            tmp += addActionFastButton( 'Plagio', "fastButtons.readInputButton( 'URL', 'FBplagio' );" );
            tmp += addActionFastButton( 'Otro', "fastButtons.readInputButton( 'Motivo', 'FBdest' );" );

            return tmp;
        }

        //BOTONES PARA SYSOP
        function loadSysop() {
            var tmp = '';

            tmp += addFastButton( 'Borrado rápido', 'FBdelete', 'default', 'delete' );
            tmp += addActionFastButton( 'Borrar', "fastButtons.readInputButton( 'Motivo', 'FBdelete', 'delete' );" );

            return tmp;
        }

        //PLANTILLAS (BOTONES GENERALES)
        function loadTemplates() {
            var tmp = '';

            if( $.inArray( mw.config.get( 'wgNamespaceNumber' ), [ 0, 104 ] ) > -1 ) {
                tmp += addFastButton( 'SRA', 'FBplant', '{{sinrelevancia|{{subst:CURRENTDAY}}|{{subst:CURRENTMONTHNAME}}}}' );
                tmp += addFastButton( 'Wikificar', 'FBplant', '{{wikificar|t={{subst:CURRENTTIMESTAMP}}}}' );
                tmp += addFastButton( 'Promocional', 'FBplant', '{{promocional|{{subst:CURRENTDAY}}|{{subst:CURRENTMONTHNAME}}}}' );
                tmp += addFastButton( 'Fuente primaria', 'FBplant', '{{fuente primaria|{{subst:CURRENTDAY}}|{{subst:CURRENTMONTHNAME}}}}' );
                tmp += addFastButton( 'Referencias', 'FBplant', '{{referencias}}' );
                tmp += addFastButton( 'Wikcionario', 'FBplant', '{{wikci}}' );
                tmp += addFastButton( 'Wikilibros', 'FBplant', '{{a wikilibros}}' );
                tmp += addFastButton( 'Wikiversidad', 'FBplant', '{{a wikiversidad}}' );
            }

            return tmp;
        }

        //BOTONES DE VARIAS FUNCIONALIDADES (BOTONES GENERALES)
        function loadMisc() {
            var tmp = '',
                warnMenu = encodeURI( "Aviso:\n(nada): Prueba\n0a: Etiqueta\n" +
                    "0b: Estilo\n0c: Spam\n2: Ediciones sin sentido\n2a: Blanqueo\n" +
                    "3: Detente\n4: Última advertencia\n5: Bloqueo" );

            if( mw.config.get( 'wgNamespaceNumber' ) != 3 ) {
                return tmp;
            }

            tmp += addActionFastButton( "Advertencia", "fastButtons.readInputButton(\'" + warnMenu + "\', 'FBwarn' );" );
            return tmp;
        }

        //*********************************************************************
        //Añadir los botones
        //*********************************************************************
        function cargar() {
            //Extraer datos de la página
            var tmp;
            if ( $.inArray( 'sysop', mw.config.get( 'wgUserGroups' ) ) > -1 ) {
                fastButtons.isSysop = true;
            }

            fastButtons.wikiURL = fastButtons.wikiURL + mw.util.wikiUrlencode( mw.config.get( 'wgPageName' ) ) + "&action=";

            //Tiene que ser editable
            if ( mw.config.get( 'wgNamespaceNumber' ) >= 0 ) {
                //Botones para bibliotecarios
                if ( fastButtons.isSysop ) {
                    if ( typeof botonesSysop == 'function' ) {
                        tmp = botonesSysop();
                    } else {
                        tmp = loadSysop();
                    }
                    if ( tmp ) {
                        fastButtons.FBList = fastButtons.FBList + 'Sysop: ' + tmp + '<br>';
                    }
                } else { //Botones de destruir. Sólo aparecen a no-bibliotecarios.
                    if ( typeof botonesNoSysop == 'function' ) {
                        tmp = botonesNoSysop();
                    } else {
                        tmp = loadNoSysop();
                    }
                    if ( tmp ) {
                        fastButtons.FBList = fastButtons.FBList + 'Destruir: ' + tmp + '<br>';
                    }
                }

                //Plantillas. Aparecen a todos los usuarios.
                if ( typeof botonesPlantillas == 'function' ) {
                    tmp = botonesPlantillas();
                } else {
                    tmp = loadTemplates();
                }
                if ( tmp != '' ) {
                    fastButtons.FBList = fastButtons.FBList + 'Plantillas: ' + tmp + '<br>';
                }

                //Otros. Aparecen a todos los usuarios.
                if ( typeof botonesOtros == 'function' ) {
                    tmp = botonesOtros();
                } else {
                    tmp = loadMisc();
                }
                if ( tmp ) {
                    fastButtons.FBList = fastButtons.FBList + 'Otros: ' + tmp;
                }
            } else {
                if ( fastButtons.isSysop ) {
                    if ( typeof botonesBloqueo == 'function' ) {
                        tmp = botonesBloqueo();
                    } else {
                        tmp = addBlockFastButton();
                    }
                    if ( tmp ) { //El <br> de más es porque el botón es peligroso y aparece muy pegado a las demás opciones
                        fastButtons.FBList = fastButtons.FBList + '<br>Bloqueo: ' + tmp;
                    }
                }
            }
        }
        
        /* Mostrar los botones en la página */
        function instalar() {
            var temp;
            cargar();

            if ( $( '#contentSub' ).html() ) {
                $( '#contentSub' ).html( $( '#contentSub' ).html() + '<br>' + fastButtons.FBList );
            } else {
                $( '#contentSub' ).html( fastButtons.FBList );
            }

            if ( mw.util.getParamValue( 'FBdest' ) != null ) {
                temp = mw.util.getParamValue( 'FBdest' );
                addTemplate( "{{destruir|1=" + temp + "}}", "Destruir: " + temp );
            } else if ( mw.util.getParamValue( 'FBplant' ) != null ) {
                temp = mw.util.getParamValue( 'FBplant' );
                addTemplate( temp, "Plantilla: " + temp);
            } else if ( mw.util.getParamValue( 'FBplagio' ) != null ) {
                temp = mw.util.getParamValue( 'FBplagio' );
                addTemplate( '{{plagio|1=' + temp + '|2=--~~~~}}', "Plagio desde " + temp );
            } else if ( mw.util.getParamValue( 'FBdelete' ) != null ) {
                temp = mw.util.getParamValue( 'FBdelete' );
                if ( temp != 'default' ) {
                    $( '#wpReason' ).val( temp );
                }
                $( '#deleteconfirm' ).submit();
            } else if ( mw.util.getParamValue( 'FBwarn' ) != null ) {
                temp = mw.util.getParamValue( 'FBwarn' );
                addTemplateEnd( '{{subst:prueba' + decodeURI(temp) + '}} --~~~~', 'Aviso al usuario' );
            } else if ( mw.util.getParamValue( 'FBblock' ) != null ) {
                $( '#mw-input-wpExpiry' ).val( '31 hours' );
                $( '#mw-input-wpReason' ).val( '[[WP:VN|Vandalismo de páginas]]' );
                $( '#mw-input-wpDisableUTEdit' ).prop( 'checked', true );
                $( 'form.visualClear' ).submit();
            }
        }

        $( document ).ready( instalar );

    })( jQuery, mediaWiki ); // Fin de función anónima
} //fin de chequeo
//</nowiki>