Diferencia entre revisiones de «MediaWiki:Common.js»

De Vanaheim Wiki
Ir a la navegación Ir a la búsqueda
Sin resumen de edición
Add wiki language switcher
Línea 1: Línea 1:
$(function() {
$( function () {
    // Busca todos los elementos con la clase 'copiar-portapapeles'
$( '.copiar-portapapeles' ).on( 'click', function () {
    $('.copiar-portapapeles').on('click', function() {
var textoACopiar = $( this ).data( 'texto-a-copiar' );
        var textoACopiar = $(this).data('texto-a-copiar');
var $temp = $( '<textarea>' );
       
 
        // Crea un elemento de texto temporal para copiar su contenido
$( 'body' ).append( $temp );
        var $temp = $("<textarea>");
$temp.val( textoACopiar ).select();
        $("body").append($temp);
 
        $temp.val(textoACopiar).select();
try {
       
document.execCommand( 'copy' );
        try {
mw.notify( '¡Texto copiado al portapapeles!', { type: 'success' } );
            document.execCommand("copy");
} catch ( e ) {
            // Muestra una notificación de éxito
mw.notify( 'Error al copiar el texto.', { type: 'error' } );
            mw.notify('¡Texto copiado al portapapeles!', { type: 'success' });
}
        } catch (e) {
 
            mw.notify('Error al copiar el texto.', { type: 'error' });
$temp.remove();
        }
} );
       
 
        $temp.remove();
if ( window.vanaheimLanguageSwitcherLoaded ) {
    });
return;
});
}
 
window.vanaheimLanguageSwitcherLoaded = true;
 
var heading = document.getElementById( 'firstHeading' ) ||
document.querySelector( '.mw-first-heading' );
 
if ( !heading ) {
return;
}
 
function originalPageUrl() {
var url = new URL( window.location.href );
url.searchParams.delete( 'googtrans' );
return url.href;
}
 
function googleTranslateUrl( targetLanguage ) {
return 'https://translate.google.com/translate?sl=es&tl=' +
encodeURIComponent( targetLanguage ) +
'&u=' + encodeURIComponent( originalPageUrl() );
}
 
function makeButton( label, title, href ) {
var link = document.createElement( 'a' );
link.href = href;
link.textContent = label;
link.title = title;
link.setAttribute( 'aria-label', title );
link.style.display = 'inline-flex';
link.style.alignItems = 'center';
link.style.justifyContent = 'center';
link.style.width = '1.9em';
link.style.height = '1.9em';
link.style.marginLeft = '0.35em';
link.style.border = '1px solid #a2a9b1';
link.style.borderRadius = '999px';
link.style.background = '#fff';
link.style.fontSize = '0.55em';
link.style.lineHeight = '1';
link.style.textDecoration = 'none';
link.style.verticalAlign = 'middle';
link.style.boxShadow = '0 1px 2px rgba(0, 0, 0, 0.12)';
return link;
}
 
var wrapper = document.createElement( 'span' );
wrapper.className = 'vanaheim-language-switcher';
wrapper.style.whiteSpace = 'nowrap';
wrapper.style.marginLeft = '0.35em';
wrapper.style.fontFamily = 'system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif';
 
wrapper.appendChild( makeButton( '🇪🇸', 'Ver página original en español', originalPageUrl() ) );
wrapper.appendChild( makeButton( '🇺🇸', 'Traducir esta página al inglés con Google Translate', googleTranslateUrl( 'en' ) ) );
 
heading.appendChild( wrapper );
} );

Revisión del 15:17 19 may 2026

$( function () {
	$( '.copiar-portapapeles' ).on( 'click', function () {
		var textoACopiar = $( this ).data( 'texto-a-copiar' );
		var $temp = $( '<textarea>' );

		$( 'body' ).append( $temp );
		$temp.val( textoACopiar ).select();

		try {
			document.execCommand( 'copy' );
			mw.notify( '¡Texto copiado al portapapeles!', { type: 'success' } );
		} catch ( e ) {
			mw.notify( 'Error al copiar el texto.', { type: 'error' } );
		}

		$temp.remove();
	} );

	if ( window.vanaheimLanguageSwitcherLoaded ) {
		return;
	}

	window.vanaheimLanguageSwitcherLoaded = true;

	var heading = document.getElementById( 'firstHeading' ) ||
		document.querySelector( '.mw-first-heading' );

	if ( !heading ) {
		return;
	}

	function originalPageUrl() {
		var url = new URL( window.location.href );
		url.searchParams.delete( 'googtrans' );
		return url.href;
	}

	function googleTranslateUrl( targetLanguage ) {
		return 'https://translate.google.com/translate?sl=es&tl=' +
			encodeURIComponent( targetLanguage ) +
			'&u=' + encodeURIComponent( originalPageUrl() );
	}

	function makeButton( label, title, href ) {
		var link = document.createElement( 'a' );
		link.href = href;
		link.textContent = label;
		link.title = title;
		link.setAttribute( 'aria-label', title );
		link.style.display = 'inline-flex';
		link.style.alignItems = 'center';
		link.style.justifyContent = 'center';
		link.style.width = '1.9em';
		link.style.height = '1.9em';
		link.style.marginLeft = '0.35em';
		link.style.border = '1px solid #a2a9b1';
		link.style.borderRadius = '999px';
		link.style.background = '#fff';
		link.style.fontSize = '0.55em';
		link.style.lineHeight = '1';
		link.style.textDecoration = 'none';
		link.style.verticalAlign = 'middle';
		link.style.boxShadow = '0 1px 2px rgba(0, 0, 0, 0.12)';
		return link;
	}

	var wrapper = document.createElement( 'span' );
	wrapper.className = 'vanaheim-language-switcher';
	wrapper.style.whiteSpace = 'nowrap';
	wrapper.style.marginLeft = '0.35em';
	wrapper.style.fontFamily = 'system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif';

	wrapper.appendChild( makeButton( '🇪🇸', 'Ver página original en español', originalPageUrl() ) );
	wrapper.appendChild( makeButton( '🇺🇸', 'Traducir esta página al inglés con Google Translate', googleTranslateUrl( 'en' ) ) );

	heading.appendChild( wrapper );
} );