Created
November 18, 2009 16:57
-
-
Save cheeaun/238043 to your computer and use it in GitHub Desktop.
Better Emoticons Formatter plugin for Talkerapp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://cheeaun.talkerapp.com/plugins/10 | |
var path = '/images/icons/'; | |
// 1. For humans | |
var emoticons = { | |
evil: { | |
image: 'smiley-evil.png', | |
text: '>:-) >:)' | |
}, | |
laughing: { | |
image: 'smiley-lol.png', | |
text: ':-)) :))' | |
}, | |
smiling: { | |
image: 'smiley.png', | |
text: ':-) :)' | |
}, | |
grin: { | |
image: 'smiley-grin.png', | |
text: ':-D :D' | |
}, | |
angry: { | |
image: 'smiley-mad.png', | |
text: 'X( X-(' | |
}, | |
sad: { | |
image: 'smiley-sad.png', | |
text: ':( :-(' | |
}, | |
cry: { | |
image: 'smiley-cry.png', | |
text: ';( ;-(' | |
}, | |
cool: { | |
image: 'smiley-cool.png', | |
text: 'B) B-) 8) 8-)' | |
}, | |
confused: { | |
image: 'smiley-confuse.png', | |
text: ':S :-S' | |
}, | |
shocked: { | |
image: 'smiley-eek.png', | |
text: ':O :-O' | |
}, | |
razz: { | |
image: 'smiley-razz.png', | |
text: ':P :-P' | |
} | |
}; | |
// 2. For regex | |
var emoticonsText = $.trim(_.map(emoticons, function(val){ | |
return val.text + ' '; | |
}).join('')).split(' ').join('|').replace(/([\(\)])/g, '\\$1'); | |
var emoticonsRegex1 = new RegExp('(^|\\B)(' + emoticonsText + ')(\\s|$)', 'g'); | |
var emoticonsRegex2 = new RegExp('(^|\\B)\\u2009\\u2009(' + emoticonsText + ')\\u2009\\u2009(\\s|$)', 'g'); | |
// 3. For replacing convenience | |
var emoticonsImage = {}; | |
_.each(emoticons, function(val){ | |
var image = val.image; | |
_.each(val.text.split(' '), function(t){ | |
emoticonsImage[t] = image; | |
}); | |
}); | |
var emoticonify = function(el){ | |
var childNodes = el.childNodes; | |
var l = childNodes.length; | |
if (!l) return; | |
for (var i=0; i<l; i++){ | |
var node = childNodes[i]; | |
var type = node.nodeType; | |
if (type == 3){ | |
var text = node.data; | |
text = text.replace(emoticonsRegex1, '$1\u2009\u2009$2\u2009\u2009$3'); | |
node.data = text; | |
} else if (type == 1 && !/^pre|a|script|embed|object|img$/gi.test(node.tagName)){ | |
emoticonify(node); | |
} | |
} | |
el = $(el); | |
var html = el.html(); | |
if (!emoticonsRegex2.test(html)) return; | |
html = html.replace(emoticonsRegex2, function(a, b, c, d){ | |
return b + '<img src="' + path + emoticonsImage[c] + '" alt="' + c + '">' + d; | |
}); | |
el.html(html); | |
}; | |
plugin.onMessageInsertion = function(event){ | |
var lastIns = Talker.getLastInsertion(); | |
lastIns.each(function(i, el){ | |
emoticonify(el); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment