User:Mooeypoo/User script demos/ReplaceArticleImagesWithImageFromArrayAndSwitchOnMouseover.js

/** * User script demo: Replace all images in the article with random images from an array * and switch between old and new image on mouseover/out * * This is an expansion of the simpler script: https://www.mediawiki.org/wiki/User:Mooeypoo/User_script_demos/ReplaceArticleImagesWithImageFromArray.js */ $( document ).ready( function {	var randomIndex,		imageArray = [			// Array of image src from commons			'https://upload.wikimedia.org/wikipedia/commons/thumb/8/82/P_S_Kr%C3%B8yer_1899_-_Sommeraften_ved_Skagens_strand._Kunstneren_og_hans_hustru.jpg/320px-P_S_Kr%C3%B8yer_1899_-_Sommeraften_ved_Skagens_strand._Kunstneren_og_hans_hustru.jpg',			'https://upload.wikimedia.org/wikipedia/commons/c/cf/Heinrich_Hirschsprung_%28Peder_Severin_Kr%C3%B8yer%29.jpg',			'https://upload.wikimedia.org/wikipedia/commons/e/ee/%22Birdcatcher%22_with_jockey_up.jpg',			'https://upload.wikimedia.org/wikipedia/commons/4/44/%27Amitabha_in_Sukhavati_Paradise%27%2C_Tibetan%2C_circa_1700%2C_San_Antonio_Museum_of_Art.jpg'		],		// Get a random integer between min and max		getRandomInt = function ( min, max ) {			return Math.floor( Math.random * ( max - min + 1 ) + min );		};

// Find all images in the body $( '.mw-body img' ).each( function {			var randomIndex = getRandomInt( 0, imageArray.length - 1 ),				originalImageSrc = $( this ).prop( 'src' ),				newImageSrc = imageArray[ randomIndex ];

// Save both original src and replaced src so we can toggle between them $( this ) .data( 'src-original', originalImageSrc ) .data( 'src-new', newImageSrc ) // Replace the src property .prop( 'src', newImageSrc ) // Listen to events so we can switch between the two .on( 'mouseover', function {					// Show original src					$( this ).prop( 'src', $( this ).data( 'src-original' ) );				} ) .on( 'mouseout', function {					// Show new src					$( this ).prop( 'src', $( this ).data( 'src-new' ) );				} ); } ); } );