Przejdź do zawartości

Wikipedysta:Peter Bowman/missing-person-infoboxes.js: Różnice pomiędzy wersjami

Z Wikipedii, wolnej encyklopedii
Usunięta treść Dodana treść
m dr.
mw.hook( 'pb-mpi.results' ).fire( $results );
 
Linia 81: Linia 81:
fetchStuff( evt.target.value )
fetchStuff( evt.target.value )
.then( function ( titles ) {
.then( function ( titles ) {
var $ul = $( '<ol>' );
var $ol = $( '<ol>' );
titles.forEach( function ( title ) {
titles.forEach( function ( title ) {
$ul.append( makeListItem( title ) );
$ol.append( makeListItem( title ) );
} );
} );
$results.html( $ul );
$results.html( $ol );
mw.hook( 'pb-mpi.results' ).fire( $results );
} )
} )
.catch ( function ( e ) {
.catch ( function ( e ) {

Aktualna wersja na dzień 00:02, 9 lis 2022

// https://stuk.github.io/jszip/documentation/examples/get-binary-files-ajax.html

( function () {
	var jsZipUrl = 'https://unpkg.com/jszip@3.7.1/dist/jszip.min.js',
		staticServerUrl = 'https://tools-static.wmflabs.org/pbbot/plwiki/missing-person-infoboxes',
		categoriesDescriptorFile = 'categories.json',
		mappings = null,
		$content = null,
		$selector = null,
		$link = null,
		$spinner = null,
		$results = null;
	
	function init() {
		$content = $( '#pb-mpi-content' );
		
		if ( !$content.length ) {
			alert( 'Błąd skryptu: zmieniono układ strony.' );
			return;
		}
		
		$content.append(
			$( '<label>' )
				.attr( 'for', 'pb-mpi-selector' )
				.text( 'Wybierz kategorię: ' ),
			$selector = $( '<select>' )
				.attr( 'id', 'pb-mpi-selector' ),
			' ',
			$link = $( '<a>' )
				.text( '(pobierz plik)' )
				.hide(),
			' ',
			$spinner = $( '<span>' )
				.append( $.createSpinner( { size: 'small', type: 'inline' } ) ),
			$results = $( '<div>' )
		);
		
		$selector.append(
			$( '<option>' )
				.attr( {
					disabled: true,
					selected: true
				} ),
			$( '<option>' )
				.attr( 'value', 'all' )
				.text( 'Pokaż wszystko' )
				.data( 'pb-mpi-entry', { filename: 'all' } )
		);
		
		$selector
			.prop( 'disabled', true )
			.on( 'change', onCategoryChange );
		
		$.when(
			$.getJSON( mw.format( '$1/$2', staticServerUrl, categoriesDescriptorFile ) ),
			$.getScript( jsZipUrl )
		).then( function ( jsonRes, scriptRes ) {
			mappings = jsonRes[ 0 ];
			
			mappings.forEach( function ( entry ) {
				$( '<option>' )
					.attr( 'value', entry.filename )
					.text( entry.category )
					.data( 'pb-mpi-entry', entry )
					.appendTo( $selector );
			} );
			
			$selector.prop( 'disabled', false );
			$spinner.hide();
		}, function ( err ) {
			alert( 'Błąd skryptu: brak odpowiedzi serwera.' );
		} );
	}
	
	function onCategoryChange( evt ) {
		$selector.prop( 'disabled', true );
		$link.attr( 'href', mw.format( '$1/$2.zip', staticServerUrl, evt.target.value ) ).show();
		$spinner.show();
		$results.css( 'opacity', 0.5 );
		
		fetchStuff( evt.target.value )
			.then( function ( titles ) {
				var $ol = $( '<ol>' );
				
				titles.forEach( function ( title ) {
					$ol.append( makeListItem( title ) );
				} );
				
				$results.html( $ol );
				mw.hook( 'pb-mpi.results' ).fire( $results );
			} )
			.catch ( function ( e ) {
				$results.html( $( '<p>' ).text( e ) );
			} )
			.finally( function () {
				$selector.prop( 'disabled', false );
				$spinner.hide();
				$results.css( 'opacity', 'initial' );
			} );
		
		return false;
	}
	
	function fetchStuff( filename ) {
		return fetch( mw.format( '$1/$2.zip', staticServerUrl, filename ) )
			.then( function ( response ) {
				if ( response.status === 200 || response.status === 0 ) {
					return Promise.resolve( response.blob() );
				} else {
					return Promise.reject( new Error( response.statusText ) );
				}
			} )
			.then( JSZip.loadAsync )
			.then( function ( zip ) {
				return zip.file( mw.format( '$1.txt', filename ) ).async( 'string' );
			} )
			.then( function ( content ) {
				return content.split( '\n' );
			} );
	}
	
	function makeListItem( title ) {
		return $( '<li>' ).append(
			$( '<a>' )
				.attr( 'href', mw.util.getUrl( title ) )
				.text( title ),
			' (',
			$( '<a>' )
				.attr( 'href', mw.util.getUrl( title, { action: 'edit' } ) )
				.text( 'edytuj' ),
			', ',
			$( '<a>' )
				.attr( 'href', mw.util.getUrl( mw.format( 'Dyskusja:$1', title ) ) )
				.text( 'dyskusja' ),
			')'
		);
	}
	
	if ( mw.config.get( 'wgPageName' ) === 'Wikipedysta:PBbot/biogramy_bez_infoboksów' ) {
		$.when(
			mw.loader.using( [ 'jquery.spinner', 'mediawiki.util' ] ),
			$.ready
		).done( init );
	}
} )();