Jump to content

User:Edokter/FontSizer.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Edokter (talk | contribs) at 16:19, 15 January 2012 (Header). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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: jquery.cookie
 * Revision: 1.0
 * Author: Edokter
 * Source: [[User:Edokter/FontSizer.js]] / [[User:Edokter/FontSizer.css]]
 **/

$( 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-o]" accesskey="o" 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 + '%';
    $.cookie( 'fontSizer.size', size == 100 ? null : size, { expires: 365, path: '/' } );
    $( '#t-fontsizer-reset' ).attr( 'value', size + '%' );
  }

  /* Initialize */
  importStylesheetURI( '//en.wikipedia.org/w/index.php?title=User:Edokter/FontSizer.css&action=raw&ctype=text/css' );
  $( '#p-tb' ).find( 'ul' ).prepend( fontsizerButtons );
  var cookie = $.cookie( '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 );
  });
});