Topic: [Snippet] EmailCloak - Cloaks email addresses using Javascript.  (Read 10947 times)

Pages: [1]   Go Down

#1: 23-Jan-2006, 09:10 AM

Kunal Kapoor
Posts: 145

Effective, Functional, Standards Compliant

WWW
Hello fellow MODx-ers. I've been using MODx for a while now and have finally got something to contribute. I must admit, this is the first time I've ever contributed to an Open Source project, so please be gentle. Roll Eyes It's a snippet which allows email addresses to be cloaked or hidden from SPAM bots. Of course, this method is not foolproof, in fact:

Quote

I cannot see any other alternatives to this yet, and the code is very raw. If anyone can think of any improvements, I'm all open to suggestions. One feature I quite like is that you can set a page ID using $spage to make the link direct to a contact form in case Javascript is disabled.

Anyways, here is the code. This is still a work in progress though. Remember, be gentle... Wink

{EDITED: To include the <script> block of Javascript using $modx->regClientStartup() - Thanks Raymond!}

Code:
/**
 * EmailCloak
 * Cloaks any given email address.
 *
 * Code reworked by Kunal Kapoor.
 * Main credits go to Shawn K. Hall from this website address: http://tinyurl.com/9zouq
 */

///////////////////////////////////
//  <-----  USAGE  ----->
///////////////////////////////////
// To use this snippet, type this in your document content:
// [[EmailCloak? &saddress=`youremail@address.com` &scaption=`Email Me`]]
//
// OPTIONAL: You can include &stitle if you want to include a title attribute in your <a> tag.
// OPTIONAL: You can include $spage to the id of the page of your contact
//           form in case Javascript is not enabled.
// You MUST include both a &saddress and &scaption to include the link on your page.
//
// The script is provided without any warranty and is not a failsafe method of SPAM proofing your
// email address. For more information, visit http://tinyurl.com/9zouq
///////////////////////////////////
///////////////////////////////////
 
//variables
  $eaddress= "";  $sdomain= "";  $aextra = "";

//begin parsing
  list($eaddress, $sdomain)= split('@', $saddress);
  list($sdomain, $aextra) = split('\?', $sdomain);
  $sdomain = ereg_replace('\.', '#', $sdomain);

//create the js address
  $smailme = "mailMe('".urlencode( $sdomain );
  if($aextra != "" ){
    $smailme .= "?" . urlencode( $aextra );
  }
  $smailme .= "','" . urlencode( $eaddress ) . "')";

//build the js events
  $sbuild =" onmouseover=\"javascript: this.href=$smailme;\"";
  $sbuild.=" onfocus=\"javascript: this.href=$smailme;\"";

//if there is not $spage defined, use the current page id in case javascript is not enabled
  if ($spage == "") { $spage = "[~[*id*]~]"; }

//build up $thejavascript by including the javascript first, then stick it in the <head>
  $thejavascript  = "<script type=\"text/javascript\">";
  $thejavascript .= "  function mailMe(sDom, sUser){";
  $thejavascript .= "    return(\"mail\"+\"to:\"+sUser+\"@\"+sDom.replace(/%23/g,\".\"));";
  $thejavascript .= "  }";
  $thejavascript .= "</script>";
  $modx->regClientScript($thejavascript);

//return
  $theresult = "<a href=\"$spage\"$sbuild title=\"$stitle\">$scaption</a>";
  return $theresult;
« Last Edit: 23-Jan-2006, 09:32 AM by Kunal Kapoor »
Kunal Kapoor
Technical Director
Limesharp Internet Limited - Effective, Functional, Standards Compliant

IRC Nick: KingKoopa
Skype ID: KingKoopa16

#2: 23-Jan-2006, 09:17 AM

Emeritus
xwisdom
Posts: 1,732

Nice snippet.

You might want to make use of the $modx->regClientStartup() function so you can register the javascript function inside the head tag
xWisdom
www.xwisdomhtml.com
The fear of the Lord is the beginning of wisdom:
MODx Co-Founder - Create and do more with less.

#3: 23-Jan-2006, 09:20 AM

Kunal Kapoor
Posts: 145

Effective, Functional, Standards Compliant

WWW
Nice snippet.

You might want to make use of the $modx->regClientStartup() function so you can register the javascript function inside the head tag

*Hops off to Documentation to learn how to do that!*

{EDITED: This has now been done. - Thanks Raymond!}
« Last Edit: 23-Jan-2006, 09:33 AM by Kunal Kapoor »
Kunal Kapoor
Technical Director
Limesharp Internet Limited - Effective, Functional, Standards Compliant

IRC Nick: KingKoopa
Skype ID: KingKoopa16

#4: 23-Jan-2006, 09:43 AM

Marketing & Design Team

davidm
MODx evangelist
Posts: 7,073

The best way to predict the future is to invent it

WWW
I know Aour had another solution, I'll notify him maybe he can add to this very useful feature !

Thanks Smiley
.: nodeo.net : Pour un web libre, moderne et ouvert ! :: david-molliere.net : Suivez en "live" mes expérimentations et billets sur les CMS et autres applications web :.

*** Forums modxcms.fr Participez à l'élaboration du site MODx francophone ! ***

! Nouveau !  En live, ne manquez pas les news de modxcms.fr sur Twitter   ! Nouveau !

MODx est l'outil idéal pour les developpeurs et webdesigners qui cherchent un framework de gestion de contenu hautement flexible et performant, tout en étant simple d'accès pour les utilisateurs finaux.

Config : Apache 2.2.8 - MySQL 5.0.67 - PHP 5.2.8 | Debian 4.0 (Etch)

Réalisations sous MODx : | pargade-notaires.fr | soleil.info | gican.asso.fr | michelez-notaires.com | amadom.gerondicap.com | jocelyne-violet.net

#5: 23-Jan-2006, 09:50 AM

Kunal Kapoor
Posts: 145

Effective, Functional, Standards Compliant

WWW
I know Aour had another solution, I'll notify him maybe he can add to this very useful feature !

Thanks Smiley

Thanks David.

This snippet needs to display the email address in an encrypted way (in the source that is) e.g.:

Code:
<a href="email@address.com">email@address.com</a>

We need to encrypt what is between the <a..> </a> tags if $scaption isn't given. $scaption needs to default to the email address if a caption is not supplied. But whatever it is replaced with, must be invisible (although, we cannot really say this will always work) to SPAMbots.

*Phew* I hope that made sense.

Ah well, will plod on.
Kunal Kapoor
Technical Director
Limesharp Internet Limited - Effective, Functional, Standards Compliant

IRC Nick: KingKoopa
Skype ID: KingKoopa16

#6: 23-Jan-2006, 11:04 AM

Emeritus
Djamoer
Posts: 1,495

No one can limit a man other than the man himself.

WWW
I just add this to the repository listing, hope this snippet can be maintained to ensure stability and for future update as well.

Thanks for sharing this great snippet with us.

Sincerely,

#7: 13-Apr-2006, 05:01 PM

bugsmi0
Posts: 356

Hello fellow MODx-ers. I've been using MODx for a while now and have finally got something to contribute. I must admit, this is the first time I've ever contributed to an Open Source project, so please be gentle. Roll Eyes It's a snippet which allows email addresses to be cloaked or hidden from SPAM bots. Of course, this method is not foolproof, in fact:

Quote

I cannot see any other alternatives to this yet, and the code is very raw. If anyone can think of any improvements, I'm all open to suggestions. One feature I quite like is that you can set a page ID using $spage to make the link direct to a contact form in case Javascript is disabled.

Anyways, here is the code. This is still a work in progress though. Remember, be gentle... Wink

{EDITED: To include the <script> block of Javascript using $modx->regClientStartup() - Thanks Raymond!}

Code:
/**
 * EmailCloak
 * Cloaks any given email address.
 *
 * Code reworked by Kunal Kapoor.
 * Main credits go to Shawn K. Hall from this website address: http://tinyurl.com/9zouq
 */

///////////////////////////////////
//  <-----  USAGE  ----->
///////////////////////////////////
// To use this snippet, type this in your document content:
// [[EmailCloak? &saddress=`youremail@address.com` &scaption=`Email Me`]]
//
// OPTIONAL: You can include &stitle if you want to include a title attribute in your <a> tag.
// OPTIONAL: You can include $spage to the id of the page of your contact
//           form in case Javascript is not enabled.
// You MUST include both a &saddress and &scaption to include the link on your page.
//
// The script is provided without any warranty and is not a failsafe method of SPAM proofing your
// email address. For more information, visit http://tinyurl.com/9zouq
///////////////////////////////////
///////////////////////////////////
 
//variables
  $eaddress= "";  $sdomain= "";  $aextra = "";

//begin parsing
  list($eaddress, $sdomain)= split('@', $saddress);
  list($sdomain, $aextra) = split('\?', $sdomain);
  $sdomain = ereg_replace('\.', '#', $sdomain);

//create the js address
  $smailme = "mailMe('".urlencode( $sdomain );
  if($aextra != "" ){
    $smailme .= "?" . urlencode( $aextra );
  }
  $smailme .= "','" . urlencode( $eaddress ) . "')";

//build the js events
  $sbuild =" onmouseover=\"javascript: this.href=$smailme;\"";
  $sbuild.=" onfocus=\"javascript: this.href=$smailme;\"";

//if there is not $spage defined, use the current page id in case javascript is not enabled
  if ($spage == "") { $spage = "[~[*id*]~]"; }

//build up $thejavascript by including the javascript first, then stick it in the <head>
  $thejavascript  = "<script type=\"text/javascript\">";
  $thejavascript .= "  function mailMe(sDom, sUser){";
  $thejavascript .= "    return(\"mail\"+\"to:\"+sUser+\"@\"+sDom.replace(/%23/g,\".\"));";
  $thejavascript .= "  }";
  $thejavascript .= "</script>";
  $modx->regClientScript($thejavascript);

//return
  $theresult = "<a href=\"$spage\"$sbuild title=\"$stitle\">$scaption</a>";
  return $theresult;

does anyone know that when these codes are put in the post, it's next to impossible to hightlight and copy ? is this suppose to be that way ?  Would be better if we could highlight it all within the post without copying the entire template  Grin

#8: 13-Apr-2006, 05:10 PM

bugsmi0
Posts: 356

is the email cloak snippet more secure than this one ?

<script type='text/javascript'>

   var tg='<';
   var name='myemail';
   var at='&#x040;';
   var host1='mydomai';
   var host2='n.com';
   var text='Contact Us';
   document.write(tg+'a hr'+'ef=mai'+'lto:'+name);
   document.write(at+host1+host2+'>'+text+tg+'/a>');

</script>

#9: 14-Apr-2006, 12:15 PM

abbeyroad
Posts: 52

@bugsmi0:
To highlight and copy code from forums:

Place the mouse pointer at the beginning of the code you would like to copy. Click once with left mouse button.
Now go to the end of the code, using the scroll-bar if neccessary, and click again, whilst holding down the shift key and Bob's yer uncle - all the code is selected.

#10: 19-May-2006, 11:46 PM

24jedi
Posts: 22

is the email cloak snippet more secure than this one ?

<script type='text/javascript'>

   var tg='<';
   var name='myemail';
   var at='&#x040;';
   var host1='mydomai';
   var host2='n.com';
   var text='Contact Us';
   document.write(tg+'a hr'+'ef=mai'+'lto:'+name);
   document.write(at+host1+host2+'>'+text+tg+'/a>');

</script>

Where would I use this code ?
I presume it would be a chunk and then add the chunck variable to the body section where and email adress would normally be written. Is this correct ??

Thanks
Don
I am but a dwarf in a land of giants... with propellars, calculators, bluetooth and beepers Smiley

#11: 23-May-2006, 06:36 PM

bugsmi0
Posts: 356

you could do that using a chunk, yep, also the email snippet can be used too but I was curious which is more secure

#12: 21-Jun-2006, 09:41 AM

Foundation

rthrash
Posts: 11,348

WWW
Any plans on making this work more than once per page?
MODx is a content managmeent framework that allows web professionals to turn over sites to end-users for daily maintenance without worrying. Please help us help you when asking for assistance and read the wiki. Searching the forums from the top level helps, too.
Ryan Thrash
MODx Co-Founder
Principal @ Collabpad
work productively.
work intelligently.
work together.

#13: 3-Jul-2006, 06:26 PM

persepolis
Posts: 62

That works really great but I have one question:
How to append a subject ?

///////////////////////////////////
//  <-----  USAGE  ----->
///////////////////////////////////
// To use this snippet, type this in your document content:
//

1. [[EmailCloak? &saddress=`youremail@address.com\?subject=Test` &scaption=`Email Me`]]

leads to the following result:

onmouseover=\"javascript: this.href=letter('address%23comsubjec','youremail')

2. [[EmailCloak? &saddress=`youremail@address.com\?subject` &scaption=`Email Me`]]

leads to the following result:

onmouseover=\"javascript: this.href=letter('address%23comsubject','youremail')

#14: 17-Dec-2008, 05:12 PM

chrison600
Posts: 43

I'm trying to use this snippet and it doesn't seem to be working. Is it something that got broken after a new version release? I prefer to use this snippet vs that developed by Cyberk (http://modxcms.com/forums/index.php/topic,2552.0.html) because it also obfuscates the content of the link text.

Help?

Chris

#15: 18-Dec-2008, 10:23 AM

chrison600
Posts: 43

Disregard my previous post. I implemented the Email Obfuscation Plug-In by floh.

Thanks for MODx!!

Chris
Pages: [1]   Go Up
0 Members and 1 Guest are viewing this topic.