Wednesday, September 2, 2009

[jQuery] Re: Email obfuscator/defuscator plugin

I wrote one but have never gotten around to submitting it. Feedback welcome.

/**
* De-obfuscate printed email addresses which are of the type:
*
* <span class="Obfuscated" title="some title">
* some link text [ someone AT gmail DOT com ]
* <span>
*
* The braces around the address part are hard-wired here. That should
* probably be set by a param. Ditto for the "AT" & "DOT".
*
* @author brian ally, zijn digital
**/
jQuery.fn.deobfuscate = function()
{
return this.each(function()
{
var content = $(this).text();

/* grab the part inside the braces, swap out placeholders, and trim
*/
var obfuscated = content.match(/\[(.*)\]/);
var address = obfuscated[1]
.replace(' AT ', '@')
.replace(' DOT ', '.')
.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>'));
});
};


$(function()
{
$('.Obfuscated').deobfuscate();
});

The title attribute of the span will be given to the link. If you
leave out the "some link text" part, the email address itself will be
used.

Here's a PHP function to create the span from an email address.

/**
* Wrap an email address in a formatted span tag in such a way
* as to allow for easy javascript translation to a normal mailto: link.
*
* this:
* foo@bar.net, 'email foo!', 'some title text'
* becomes:
* <span class="Obfuscated" title="some title text">email foo! [ foo
AT bar DOT net ]</span>
*
* @param string address email address
* @param string text link text
* @param string title link title
* @return string obfuscated address, wrapped in i span tag
*
* @author brian ally, zijn digital
**/
function obfuscateEmail($address, $text= null, $title = null)
{
if (empty($address)) return null;

$regexp = '^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$';
$search = Array('@', '.');
$replace = Array(' AT ', ' DOT ');
$class = 'Obfuscated';

if (is_string($address) && eregi($regexp, $address))
{
$obfuscated = str_replace($search, $replace, $address);

return "<span class=\"${class}\""
. (!is_null($title) ? " title=\"${title}\"" : null)
. '>'
. (!is_null($text) ? $text : null)
. "[ ${obfuscated} ]</span>";
}
return $address;
}


On Wed, Sep 2, 2009 at 12:35 PM, shapper<mdmoura@gmail.com> wrote:
>
> I found the following one:
>
> http://plugins.jquery.com/project/RotationalStringObfuscator
>
> It seems interesting.
>
> How can I apply it to all emails on my web page?
>
> Thank You,
> Miguel

No comments: