MediaWiki:Gadget-FlowAvatars.js

Från Wikipedia

OBS: Efter du har publicerat sidan kan du behöva tömma din webbläsares cache för att se ändringarna.

  • Firefox / Safari: Håll ned Skift och klicka på Uppdatera sidan eller tryck Ctrl-F5 eller Ctrl-R (⌘-R på Mac)
  • Google Chrome: Tryck Ctrl-Skift-R (⌘-Skift-R på Mac)
  • Internet Explorer / Edge: Håll ned Ctrl och klicka på Uppdatera eller tryck Ctrl-F5
  • Opera: Tryck Ctrl-F5.
$( function () {
	'use strict';
	var content,
		captions,
		names,
		links,
		sources;
	function addAvatars () {
		$( '.mw-userlink' ).not( '.flow-avatar-kollad' ).each( function ( idx, elem ) {
			var name = $( elem ).text().trimLeft(),
				index = names.indexOf( name );
			if ( index !== -1 ) {
				$( elem ).closest( '.flow-post-main' ).before( '<div class="flow-avatar"><a href="' + links[ index ] + '"><img src="' + sources[ index ] + '" width="50"></a></div>' );
			}
			$( this ).addClass( 'flow-avatar-kollad' );
		} );
	}
	if ( mw.config.get( 'wgPageContentModel' ) === 'flow-board' ) {
		$.getJSON(
			mw.config.get( 'wgServer' ) + '/w/api.php?action=parse&page=Wikipedia:Flow/Visningsbilder&prop=text&format=json',
			function ( data ) {
				if ( data.parse ) {
					content = $( data.parse.text[ '*' ] );
					captions = content.find( '.thumbcaption' );
					names = captions.map( function () {
						return $( this ).text().trimLeft();
					} ).get();
					links = content.find( '.image' ).map( function () {
						return 'https://commons.wikimedia.org' + $( this ).attr( 'href' ).replace( '/Fil:', '/File:' );
					} ).get();
					sources = content.find( '.thumbimage' ).map( function () {
						return $( this ).attr( 'src' );
					} ).get();
					addAvatars();
					$( document ).scroll( addAvatars );
				}
			}
		);
	}
} );