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 17:37, 1 July 2011 (importStylesheet). 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.
$(function() {

  var fontsizerButtons =
    '<li id="fontsizer">' +
      '<input type="submit" id="fontsizer-minus" name="minus" title="Decrease fontsize [alt--]" accesskey="-" value="−">' +
      '<input type="submit" id="fontsizer-reset" name="reset" title="Reset fontsize [alt-o]" accesskey="o" value="100%">' +
      '<input type="submit" id="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: '/' });
    $('#fontsizer-reset').attr('value', size + '%');
  }

  /* Initialize */
  importStylesheet('User:Edokter/FontSizer.css');
  $('#p-personal').find('ul').append(fontsizerButtons);
  var cookie = $.cookie('fontSizer.size');
  if (cookie) {
    setSize(cookie);
  }

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

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

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