Jump to content

User:Edokter/FontSizer.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/**
 * Add an experimental fontsizer applet to the Tools menu.
 *
 * @dependencies: mediawiki.cookie
 * @source: [[User:Edokter/FontSizer.js]] / [[User:Edokter/FontSizer.css]]
 * @revision 3.1
 * @author: Edokter ([[User:Edokter]])
 **/

/* Load dependencies (remove when loading through ResourceLoader) */
mw.loader.load( '//wiki.riteme.site/w/index.php?title=User:Edokter/FontSizer.css&action=raw&ctype=text/css', 'text/css' );
mw.loader.using( 'mediawiki.cookie', function() {
/* Begin of mw.loader.using callback */

$( document ).ready( function() {

	var fontsizerButtons =
		'<li id="t-fontsizer">' +
			'<input type="submit" id="t-fontsizer-minus" name="minus" title="Decrease fontsize [alt--]" accesskey="-" value="−">' +
			'<input type="submit" id="t-fontsizer-reset" name="reset" title="Reset fontsize [alt-0]" accesskey="0" value="100%">' +
			'<input type="submit" id="t-fontsizer-plus" name="plus" title="Increase fontsize [alt-+]" accesskey="+" value="+">' +
		'</li>';

	var bodyStyle = document.getElementsByTagName( 'body' )[0].style;

	function getSize() {
		return parseInt( bodyStyle.fontSize.replace( '%', '' ) );
	}

	function setSize( size ) {
		bodyStyle.fontSize = size == 100 ? '' : size + '%';
		mw.cookie.set( 'fontSizer.size', size == 100 ? null : size, { prefix: '' } );
		$( '#t-fontsizer-reset' ).attr( 'value', size + '%' );
	}

	/* Initialize */
	$( '#p-tb' ).find( 'ul' ).prepend( fontsizerButtons );

	var cookie = mw.cookie.get( 'fontSizer.size', '' );
	if ( cookie ) {
		setSize( cookie );
	}

	$( '#t-fontsizer-minus' ).click( function() {
		var newSize = getSize();
		if ( !newSize ) {
			newSize = 100;
		}
		if ( newSize > 50 ) {
			newSize <= 100 ? newSize -= 5 : newSize -= 10;
		}
		setSize( newSize );
	});

	$( '#t-fontsizer-plus' ).click( function() {
		var newSize = getSize();
		if ( !newSize ) {
			newSize = 100;
		}
		if ( newSize < 200 ) {
			newSize < 100 ? newSize += 5 : newSize += 10;
		}
		setSize(newSize);
	});

	$( '#t-fontsizer-reset' ).click( function() {
		setSize( 100 );
	});
});

/* End of mw.loader.using callback */
} );
/* DO NOT ADD CODE BELOW THIS LINE */