j-l-n / change avatars in vBulletin-based forums

// ==UserScript==
// @name            change avatars in vBulletin-based forums
// @namespace       j-l-n
// @copyright       2014, j-l-n
// @homepageURL     https://github.com/j-l-n
// @oujs:author     j-l-n
// @license         GNU GPL v2; http://www.gnu.org/licenses/gpl-2.0.html
// @description     allows you to replace avatars of defined users by freely selectable images. should work with many vBulletin-based forums.
// @include         http://forum.jswelt.de/*
// @version         1.4
// @lastModified    30/10/2014 (DD/MM/YYYY)
// @grant           none
// ==/UserScript==

Object.defineProperty(String.prototype, 'escapeRegExp', {
  enumerable: false,
  value: function () {
    return this.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
  }
}
);

(function () {
  var forum = window.location.protocol + '//' + window.location.hostname;
  if (window.localStorage && localStorage.setItem && localStorage.getItem && localStorage.removeItem) {
    if (localStorage.length > 0) {
      changeAvatars();
    }
  } 
  else {
    alert('Your browser does not support local storage. Can\'t use this extension.');
  }
  var memberPageRegExp = '^' + forum.escapeRegExp() + '/members/'.escapeRegExp();
  var memberPage = new RegExp(memberPageRegExp, 'i');
  if (window.location.href.search(memberPage) === 0) {
    var username = document.getElementsByClassName('member_username') [0].textContent;
    var usermenu = document.getElementById('usermenu');
    var newItem = document.createElement('li');
    usermenu.appendChild(newItem);
    var avatarChangeLink = document.createElement('a');
    avatarChangeLink.innerHTML = '\u270F change avatar';
    avatarChangeLink.href = '#changeAvatar';
    avatarChangeLink.onclick = function (call) {
      addNew(username);
      call.preventDefault();
    }
    usermenu.appendChild(avatarChangeLink);
    var currentAvatar = document.getElementById('user_avatar');
    var avatarChangeText = 'click to change avatar';
    currentAvatar.style.cursor = 'pointer';
    currentAvatar.alt = avatarChangeText;
    currentAvatar.title = avatarChangeText;
    currentAvatar.onclick = function () {
      addNew(username);
    }
  }
  function addNew(user) {
    var input = prompt('Note: to reset to default avatar, let field empty and click "OK"!\n\n\nURI of avatar which should be used instead of default:\n', '');
    if (input === null) {
      console.warn('dialog aborted by user.');
      return ;
    } 
    else if (input === '') {
      localStorage.removeItem(user);
      console.info('avatar of ' + user + ' will not be changed longer.');
    } 
    else {
      localStorage.setItem(user, input);
      init();
    }
  }
  function changeAvatars() {
    var imgs, avatarUrlRegExp, avatarUrl, matches, user, avatar;
    var i = 0;
    imgs = document.getElementsByTagName('img');
    avatarUrlRegExp = '^' + forum.escapeRegExp() + '/avatars/'.escapeRegExp() + '(.+)' + '.gif?dateline='.escapeRegExp();
    avatarUrl = new RegExp(avatarUrlRegExp, 'i');
    for (var x = 0; x < imgs.length; x++) {
      matches = imgs[x].src.match(avatarUrl);
      if (matches !== null) {
        user = matches[1];
        avatar = localStorage.getItem(user);
        if (avatar !== null) {
          imgs[x].src = avatar;
          i++;
        }
      }
    }
    console.info('changed ' + i + ' avatars.')
  }
}) ();