Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
/*<nowiki>
 * a favicon setter by [[User:Ricordisamoa]]
 *
*/
/* global $, mw */
$( document ).ready( function () {
	'use strict';

	var pref, thumbBaseUrl, $iconNormal, $iconLarge, pngIze, src, canvas, context, imageObj;

	if ( typeof window.FaviconSettings === 'undefined' ) {
		return;
	}
	pref = window.FaviconSettings;
	thumbBaseUrl = '//upload.wikimedia.org/wikipedia/commons/thumb/';
	$iconNormal = $( 'link[rel="shortcut icon"]' ); // link for normal icon
	$iconLarge = $( 'link[rel="apple-touch-icon"]' ); // link for hi-res icon
	pngIze = function ( str ) {
		return str.replace( /(\.svg)$/i, '$1.png' );
	};
	if ( mw.config.get( 'wgNamespaceNumber' ) === 6 && pref.IsImageAsIcon === true && $( 'div#file a>img' ).length === 1 ) {
		src = $( 'div#file a>img' ).parent().attr( 'href' )// parse the image url
			.replace( /(upload\.wikimedia\.org\/wikipedia\/[a-z]+\/)([a-z0-9])/, '$1thumb/$2' )// make it a thumb
			.split( '/' );

		if ( pref.IsSiteLogoInIcon === false ) {
			$iconNormal.attr( 'href', pngIze( src.join( '/' ) + '/32px-' + src[ src.length - 1 ] ) );
			$iconLarge.attr( 'href', pngIze( src.join( '/' ) + '/57px-' + src[ src.length - 1 ] ) );
		} else {
			canvas = document.createElement( 'canvas' );
			canvas.width = 32;
			canvas.height = 32;
			context = canvas.getContext( '2d' );
			imageObj = new Image();
			imageObj.crossOrigin = 'Anonymous';// allow CORS images
			imageObj.onload = function () {
				var imageObj2;
				context.drawImage( imageObj, 0, 0 );
				imageObj2 = new Image();
				imageObj2.crossOrigin = 'Anonymous';
				imageObj2.onload = function () {
					var imageObj3;
					context.drawImage( imageObj2, canvas.width - imageObj2.width, canvas.height - imageObj2.height );
					$iconNormal.attr( 'href', canvas.toDataURL() );
					canvas.width = canvas.height = 57;// resize the canvas for the hi-res icon
					imageObj3 = new Image();
					imageObj3.crossOrigin = 'Anonymous';
					imageObj3.onload = function () {
						var imageObj4;
						context.drawImage( imageObj3, 0, 0 );
						imageObj4 = new Image();
						imageObj4.crossOrigin = 'Anonymous';
						imageObj4.onload = function () {
							context.drawImage( imageObj4, canvas.width - imageObj4.width, canvas.height - imageObj4.height );
							$iconLarge.attr( 'href', canvas.toDataURL() );
						};
						imageObj4.src = thumbBaseUrl + '4/4a/Commons-logo.svg/21px-Commons-logo.svg.png';
					};
					imageObj3.src = pngIze( src.join( '/' ) + '/57px-' + src[ src.length - 1 ] );
				};
				imageObj2.src = thumbBaseUrl + '4/4a/Commons-logo.svg/12px-Commons-logo.svg.png';
			};
			imageObj.src = pngIze( src.join( '/' ) + '/32px-' + src[ src.length - 1 ] );
		}
	} else if ( pref.IsIconTransparent === true ) {
		switch ( mw.config.get( 'wgServer' ).split( '.' ).slice( -2 )[ 0 ] ) {
			case 'wikipedia': {
				$iconNormal.attr( 'href', thumbBaseUrl + '5/5a/Wikipedia%27s_W.svg/16px-Wikipedia%27s_W.svg.png' );
				$iconLarge.attr( 'href', thumbBaseUrl + '7/77/Wikipedia_svg_logo.svg/57px-Wikipedia_svg_logo.svg.png' );
				break;
			}
			case 'wikisource': {
				$iconNormal.attr( 'href', thumbBaseUrl + '4/4c/Wikisource-logo.svg/16px-Wikisource-logo.svg.png' );
				$iconLarge.attr( 'href', thumbBaseUrl + '4/4c/Wikisource-logo.svg/57px-Wikisource-logo.svg.png' );
				break;
			}
			case 'wikiquote': {
				$iconNormal.attr( 'href', thumbBaseUrl + '3/32/Wikiquote-logo.png/16px-Wikiquote-logo.png' );
				$iconLarge.attr( 'href', thumbBaseUrl + '3/32/Wikiquote-logo.png/57px-Wikiquote-logo.png' );
				break;
			}
		}
	}
} );