MediaWiki:Gadget-tradLink.js

Versione del 10 mar 2021 alle 15:10 di Daimona Eaytoy (discussione | contributi) (Annullata la modifica 119185353 di Daimona Eaytoy (discussione) Era giusto così)

Questa pagina definisce alcuni parametri di aspetto e comportamento generale di tutte le pagine. Per personalizzarli vedi Aiuto:Stile utente.


Nota: dopo aver salvato è necessario pulire la cache del proprio browser per vedere i cambiamenti (per le pagine globali è comunque necessario attendere qualche minuto). Per Mozilla / Firefox / Safari: fare clic su Ricarica tenendo premuto il tasto delle maiuscole, oppure premere Ctrl-F5 o Ctrl-R (Command-R su Mac); per Chrome: premere Ctrl-Shift-R (Command-Shift-R su un Mac); per Konqueror: premere il pulsante Ricarica o il tasto F5; per Opera può essere necessario svuotare completamente la cache dal menù Strumenti → Preferenze; per Internet Explorer: mantenere premuto il tasto Ctrl mentre si preme il pulsante Aggiorna o premere Ctrl-F5.

/** 
 *
 *Considera il testo selezionato come il titolo di una voce straniera, e lo trasforma nel titolo dell'equivalente voce italiana
 *
 * Autore : [[:it:User:Jalo]]
 *
 * Lo script contiene funzioni (tradLink_showDialog e keypress) derivate da:
 * http://it.wikipedia.org/w/index.php?title=MediaWiki:Gadget-tb-base.js&oldid=66478020
 * scritto da [[:it:User:Rotpunkt]]
 */

/* global mediaWiki, jQuery, OO */
( function( mw, $ ) {
	'use strict';

	/**
	 * Polyfill per trimStart() (liste di browser non esaustive)
	 */
	function trimStringStart( str ) {
		if ( 'trimStart' in String.prototype ) {
			return str.trimStart();
		} else if ( 'trimLeft' in String.prototype ) {
			// Edge, Firefox 27-60, Chrome 31-65, Opera 18-52
			return str.trimLeft();
		} else {
			// IE 9-11, Safari 9-11
			return str.replace( /^\s+/, '' );
		}
	}

	// Show the dialog in order to ask for the template parameters.
	function tradLink_showDialog( data, page ) {
		var lingue = tradLink_getLingue(),
			$dialog = $( '#gtb-dialog' ).html( "Inserire l'elenco delle lingue da utilizzare per il tool TradLink" ),
			$fieldset = $( '<fieldset>' ).css( 'border-color', 'gray' ).appendTo( $dialog );

		$( '<legend>' ).text( "Elenco" ).appendTo( $fieldset );
		var count = 1;
		$.each( lingue, function( id, val ) {
			$( '<label>' )
				.attr( 'for', id )
				.text( "Alt+" + count + ': ' )
				.appendTo( $fieldset );
			$fieldset
				.append( $( '<input/>' )
				.attr( 'id', id )
				.attr( 'type', 'text' )
				.attr( 'size', 3 )
				.attr( 'value', ( val || '' ) ) )
				.append( '<br/>' );
			count++;
		} );
		// show the dialog
		var Buttons = {},
			OKText = 'OK',
			CancelText = 'Cancel';

		Buttons[ OKText ] = function() {
			var params = [];
			count = 0;
			$dialog.find( 'input:text' ).each( function() {
				params[ count++ ] = $( this ).val().trim();
			} );
			lingue = params.join();
			var d = new Date();
			d.setTime( d.getTime() + ( 20 * 365 * 24 * 60 * 60 * 1000 ) ); // 20 years
			var expires = "expires=" + d.toUTCString();
			document.cookie = "tradLinkLangs=" + lingue + "; " + expires;
			$( this ).dialog( 'close' );
		};
		Buttons[ CancelText ] = function() {
			$( this ).dialog( 'close' );
		};
		$dialog.dialog( {
			title: "Configurazione del tool TradLink",
			width: 300,
			resizable: false,
			modal: true,
			zIndex: 10000,
			buttons: Buttons
		} );
	}

	function tradLink_getLingue() {
		var value = "",
			name = "tradLinkLangs=",
			ca = document.cookie.split( ';' );
		for ( var i = 0; i < ca.length; i++ ) {
			var c = trimStringStart( ca[ i ] );
			if ( c.indexOf( name ) === 0 ) {
				value = c.substring( name.length, c.length );
				break;
			}
		}

		return value !== ""
			? value.split( ',' ).slice( 0, 9 )
			: [ 'en', 'nl', 'de', 'sv', 'fr', 'ru', 'es', 'war', 'vi' ];
	}

	function tradLink_tradLink( cod ) {
		lingue = tradLink_getLingue();

		var toTrad = mw.html.escape( $( '#wpTextbox1' ).textSelection( 'getSelection' ) );
		var trad = "";
		$.getJSON( "https://" + lingue[ cod ] + ".wikipedia.org/w/api.php?callback=?", {
				action: "query",
				prop: "langlinks",
				lllang: "it",
				titles: toTrad,
				format: "json",
				redirects: ""
			},
			function( data ) {
				var selStart = document.getElementById( 'wpTextbox1' ).selectionStart,
					selEnd = document.getElementById( 'wpTextbox1' ).selectionEnd;
				if ( typeof( data ) != "undefined" &&
					typeof( data.query ) != "undefined" &&
					typeof( data.query.pages ) != "undefined"
				) {
					for ( var key in data.query.pages ) {
						if ( typeof( data.query.pages[ key ] ) != "undefined" &&
							typeof( data.query.pages[ key ].langlinks ) != "undefined" &&
							typeof( data.query.pages[ key ].langlinks[ 0 ] ) != "undefined" &&
							typeof( data.query.pages[ key ].langlinks[ 0 ][ "*" ] ) != "undefined"
						) {
							var trad = data.query.pages[ key ].langlinks[ 0 ][ "*" ];
							try {
								mw.html.escape( $( '#wpTextbox1' ).textSelection( 'encapsulateSelection', {
									pre: trad + "[",
									post: "]"
								} ) );
							} catch ( err ) {
								// ...?
							}
							document.getElementById( 'wpTextbox1' ).selectionStart = selStart + trad.length;
							document.getElementById( 'wpTextbox1' ).selectionEnd = selEnd + trad.length + 2;

							return;
						}
					}
				}
				try {
					mw.html.escape( $( '#wpTextbox1' ).textSelection( 'encapsulateSelection', {
						pre: "",
						post: "[NO LINK]"
					} ) );
				} catch ( err ) {
					// ...?
				}
				document.getElementById( 'wpTextbox1' ).selectionStart = selEnd;
				document.getElementById( 'wpTextbox1' ).selectionEnd = selEnd + 9;
			} );
	}

	// setup hotkeys
	$( '#wpTextbox1' ).keydown( function( event ) {
		if ( event.altKey && !event.shiftKey && !event.ctrlKey && !event.metaKey ) {
			if ( event.which === 48 ) {
				// configure
				tradLink_showDialog();
				event.preventDefault();
			} else if ( event.which >= 49 && event.which < 57 ) {
				// execute
				tradLink_tradLink( event.which - 49 );
				event.preventDefault();
			}
		}
	} );
}( mediaWiki, jQuery ) );