// ==UserScript==
// @name          JIRA Demoticon
// @description   Turns smilies back into their rightful character sequences.
// @include       http://jira*/*
// ==/UserScript==

// find all elements which are emoticons fields
var regex = /([^/]+)\.(gif|png|jpg)$/
var img2txt = {
    "smile" : ":)",
    "sad" : ":(",
    "tongue" : ":P",
    "biggrin" : ":D",
    "wink" : ";)",
    "thumbs_up" : "(y)",
    "thumbs_down" : "(n)",
    "information" : "(i)",
    "check" : "(/)",
    "error" : "(x)",
    "warning" : "(!)",
    "add" : "(+)",
    "forbidden" : "(-)",
    "help_16" : "(?)",
    "lightbulb_on" : "(on)",
    "lightbulb" : "(off)",
    "star_yellow" : "(*))",
    "star_red" : "(*r)",
    "star_green" : "(*g)",
    "star_blue" : "(*b)",
}
var allDivs = document.evaluate(
    "//img[@class='emoticon']",
    document,
    null,
    XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
    null)
for (var i = 0; i < allDivs.snapshotLength; i++) {
    var thisDiv = allDivs.snapshotItem(i)
    var matches = thisDiv.src.match(regex)
    var txt = document.createTextNode(img2txt[matches[1]])
    thisDiv.parentNode.replaceChild(txt, thisDiv)
}
