User:Cryptic/smiley.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Cryptic/smiley. |
//<pre><nowiki>
var smiley = new Array
(
// regexp image name location width height
new Array("[:=]\\)|:-\\)", "Smile.png", "2/26", 20, 20),
new Array("[:=]D|:-D", "Teeth.png", "7/72", 22, 22),
new Array("[:=]\\(|:-\\(", "Sad.png", "d/d8", 20, 20),
new Array(";-?[)D]", "Smile_eye.png","9/94", 20, 20),
new Array("[:=][pP]|:-[pP]", "Tongue.png", "c/c4", 22, 22),
new Array("[:=]'\\(|:'-\\(", "Cry.png", "d/d8", 22, 22),
new Array("8-?[)D]", "Shade.png", "d/dc", 22, 22)
);
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 = "http://upload.wikimedia.org/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);
}
addOnloadHook(smileys);
//</nowiki></pre>