MediaWiki:Common.js

De Vanaheim Wiki
Revisión del 15:17 19 may 2026 de Maintenance script (discusión | contribs.) (Add wiki language switcher)
Ir a la navegación Ir a la búsqueda

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
$( 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 );
} );