Jump to content

User:Sverdrup/monobook.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Sverdrup (talk | contribs) at 15:39, 3 September 2005. 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.
// crazy code

// Include smiley code
// Credit: [[User:Cryptic/smiley.js]]

var smiley = new Array
(
 //         regexp               image name   location width height
 new Array("[:=]\\)|:-\\)",     "Smile.png",    "2/26", 20, 20),
 new Array("[:=]\\(|:-\\(",     "Sad.png",      "d/d8", 20, 20),
 new Array(";-?[)D]",           "Smile_eye.png","9/94", 20, 20)
);

function smileys()
{
  var i;
  for (i = 0; i < smiley.length; ++i)
    {
      smiley[i].push(new RegExp("^(" + smiley[i][0] + ")$"));
      smiley[i].push(new RegExp("\\W(" + smiley[i][0] + ")$"));
      smiley[i].push(new RegExp("^(" + smiley[i][0] + ")\\W"));
      smiley[i].push(new RegExp("\\W(" + smiley[i][0] + ")\\W"));
    }
  add_smileys(document);
}

function smiley_image(i)
{
  var n = document.createElement("img");
  n.src = "/media/wikipedia/commons/" + smiley[i][2] + "/" + smiley[i][1];
  n.width = smiley[i][3];
  n.height = smiley[i][4];
  return n;
}

function add_smileys(n)
{
  if (!n)
    return;
  add_smileys(n.firstChild);
  var i, j, k, a, next = n.nextSibling;
  if (n.nodeType == Node.TEXT_NODE)
    {
      for (i = 0; i < smiley.length; ++i)
        {
          for (j = 5; j <= 8; ++j)
            if (a = smiley[i][j].exec(n.data))
              {
                switch (j)
                  {
                    case 5:
                      // just replace
                      n.parentNode.replaceChild(smiley_image(i), n);
                      break;
                    case 6:
                      // stuff before, but not after
                      k = n.splitText(a.index + 1);
                      n.parentNode.replaceChild(smiley_image(i), k);
                      break;
                    case 7:
                      // stuff after, but not before
                      k = n.splitText(a[1].length);
                      next = smiley_image(i);
                      n.parentNode.replaceChild(next, n);
                      next = next.nextSibling;
                      break;
                    case 8:
                      // stuff before and after
                      k = n.splitText(a.index + 1);
                      j = k.splitText(a[1].length);
                      next = smiley_image(i);
                      n.parentNode.replaceChild(next, k);
                      next = next.nextSibling;
                      break;
                  }
                i = smiley.length;
                break;
              }
        }
    }
  add_smileys(next);
}

if (window.addEventListener)
  window.addEventListener("load", smileys, false);
else if (window.attachEvent)
  window.attachEvent("onload", smileys);