Utente:Ricordisamoa/vandalo.js

Da Wikipedia, l'enciclopedia libera.
Vai alla navigazione Vai alla ricerca

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.

/* <nowiki>
 * vandalo.js by [[User:Ricordisamoa]]
 * uses jQuery & Ajax
 * helps reporting vandals on itwiki
*/
/* global mediaWiki, jQuery */
( function ( mw, $ ) {
	'use strict';
	$( function () {
		var username = mw.config.get( 'wgTitle' );
		if ( mw.config.get( 'wgNamespaceNumber' ) === 2 || mw.config.get( 'wgNamespaceNumber' ) === 3 ) {
			username = username.split( '/' )[ 0 ];
		} else if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Contributions' ) {
			username = ( username.split( '/' )[ 1 ] || mw.util.getParamValue( 'target' ) );
		} else {
			return;
		}
		if ( username === mw.config.get( 'wgUserName' ) ) {
			return; // no self-reporting!
		}
		mw.loader.using( [ 'mediawiki.api', 'jquery.ui'], function () {
			var VICpage = 'Wikipedia:Vandalismi in corso',
			headingLevel = 3,
			api = new mw.Api(),
			getTodayTitle = function () {
				var now = new Date();
				return now.getDate() + ' ' + mw.config.get( 'wgMonthNames' )[ now.getMonth() + 1 ];
			},
			getLastHeadingTitle = function () {
				return api.get( {
					action: 'query',
					prop: 'revisions',
					titles: VICpage,
					rvlimit: 1,
					rvprop: 'parsetree',
					formatversion: 2
				} )
				.then( function ( data ) {
					var hs, h;
					hs = new DOMParser()
						.parseFromString( data.query.pages[ 0 ].revisions[ 0 ].parsetree, 'text/xml' )
						.getElementsByTagName( 'h' );
					hs = Array.prototype.filter.call( hs, function ( hh ) {
						return parseInt( hh.getAttribute( 'level' ), 10 ) === headingLevel;
					} );
					if ( hs.length > 0 ) {
						h = hs[ hs.length - 1 ];
						if ( h.childNodes.length === 1 ) {
							return h.firstChild.nodeValue.replace( /^=+|=+$/g, '' ).trim();
						}
					}
					return null;
				} );
			},
			parseText = function () {
				// predefined function to provide live-preview of the typed wikitext
				var $input = $( this );
				api.get( {
					action: 'parse',
					text: $input.val(),
					prop: 'text'
				} )
				.done( function ( data ) {
					$input.parent( 'label' ).nextAll( 'div' ).html( data.parse.text[ '*' ] );
				} );
			};
			$( mw.util.addPortletLink( 'p-tb', '#', 'Vandalo', 'vandalo.js-report', 'Segnala i contributi di questo utente come vandalici' ) )
			.click( function ( event ) {
				event.preventDefault();
				$( this ).remove();
				$( '<div>' )
				.append(
					$( '<input>' )
					.on( 'input change', parseText )
					.wrap( '<label>Descrizione: </label>' )
					.parent()
				)
				.append( '<br><br>' )
				.append(
					$( '<div>' )
					.css( {
						background: 'lightgray',
						padding: '4px'
					} )
				)
				.dialog( {
					title: 'Segnala un vandalo',
					buttons: [
						{
							text: 'Annulla',
							specialButton: 'cancel',
							click: function () {
								$( this ).dialog( 'close' );
							}
						},
						{
							text: 'Segnala',
							specialButton: 'proceed',
							click: function () {
								var $dialog = $( this ),
									reason = $dialog.dialog( 'widget' ).find( 'input' ).val(),
									userlink;
								if ( mw.util.isIPAddress( username ) ) {
									userlink = 'Speciale:Contributi/' + username;
								} else {
									userlink = mw.config.get( 'wgFormattedNamespaces' )[ 2 ] + ':' + username;
								}
								userlink += '|' + username;
								getLastHeadingTitle().done( function ( lastHeadingTitle ) {
									var heading, equals,
										todayTitle = getTodayTitle();
									if ( lastHeadingTitle === null || lastHeadingTitle !== todayTitle ) {
										equals = Array( headingLevel + 1 ).join( '=' );
										heading = '\n\n' + equals + ' ' + todayTitle + ' ' + equals;
									} else {
										heading = '';
									}
									api.postWithToken( 'edit', {
										action: 'edit',
										title: VICpage,
										appendtext: heading + '\n\n{{Vandalo|' + username + '}} – ' + reason + ' --~~~~',
										summary: '[[Utente:Ricordisamoa/vandalo.js|segnalazione]] di [[' + userlink + ']]'
									} )
									.done( function ( data ) {
										if ( data && data.error && data.error.info ) {
											$( '<span>' )
											.addClass( 'error' )
											.text( 'Errore: ' + data.error.info )
											.appendTo( $dialog.dialog( 'widget' ).empty() );
										} else {
											document.location.href = mw.util.getUrl( VICpage ) + '#footer';
										}
									} )
									.fail( function () {
										$( '<span>' )
										.addClass( 'error' )
										.text( 'Errore: la richiesta AJAX è fallita.' )
										.appendTo( $dialog.dialog( 'widget' ).empty() );
									} );
								} );
							}
						}
					]
				} );
			} );
		} );
	} );
}( mediaWiki, jQuery ) );
// </nowiki>