jQuery email obfuscation

Was looking for a way to hide or obfuscate email addresses from spam bots and found this Google groups page which was pretty much what I was after for a solution, however this plugin isn’t quite complete. Javascripts .replace only works once, normally, so an email address with multiple ‘ DOT ‘ s will not deobfuscate correctly.

A quick Google later… here’s my solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
jQuery.fn.deobfuscate = function()
{
return this.each(function()
{
var content = $(this).text();</code>
 
/* grab the part inside the braces, swap out placeholders, and trim
*/
var obfuscated = content.match(/\[(.*)\]/);
/*
for(var i=0;i<obfuscated.length();i++){
alert(obfuscated[i]);
}
*/
var address = obfuscated[1]
.replace(' AT ', '@')
.replace(new RegExp(' DOT ', 'g'), '.')
.replace(/^\s+|\s+$/g, '');
 
/* get everything before the braces and trim
*/
var text = content.match(/.?[^[]+/);
 
text = (text[0] != content)
? text[0].replace(/^\s+|\s+$/g, '')
: address;      // if there's no text part, use the address
 
var title = $(this).attr('title') || '';
 
$(this).replaceWith($('<a href="mailto:' + address + '" title="' +
title + '">' + text + '</a>'));
});
 
};

note the second .replace now contains a new Regex() command which looks for all occurrances of ‘ DOT ‘. Works a treat.