Login

hide_email

Author:
rukeba
Posted:
May 6, 2008
Language:
Python
Version:
.96
Score:
2 (after 2 ratings)

This templatetag obsfucate mailto link and hide it while not happen onmouseover event.

Usage:

{% hide_email object.author object.author.email %}

Output:

<a href="mailto:[email protected]"
onmouseover="var a=String.fromCharCode(79+35,14+103,66+41,30+71,49+49,16+81);
var b=String.fromCharCode(14+50,9+105,61+56,81+26,92+9,2+96,20+77,13+33,75+24,32+79,31+78);
this.href=['mail','to:',a,b].join('');">rukeba</a>

Based on Email Obsfucator and forums at Yandex.Market.

Example at my guitar blog

 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
35
36
37
38
39
40
import random
from django import template

class HideEmailNode(template.Node):
	def __init__(self, name, email):
		self.name = template.Variable(name)
		self.email = template.Variable(email)
	
	def render(self, context):
		name = self.name.resolve(context)
		email = self.email.resolve(context)
		email_name, email_domain = str(email).split('@')
		email_domain = '@'+email_domain
		decoded_email_name = []
		decoded_email_domain = []
		for c in email_name:
			d = ord(c)
			x = random.randint(0, d)
			decoded_email_name.append("%d+%d" % (x, d-x))
		for c in email_domain:
			d = ord(c)
			x = random.randint(0, d)
			decoded_email_domain.append("%d+%d" % (x, d-x))
		return '<a href="mailto:[email protected]" onmouseover="var a=String.fromCharCode(%s);var b=String.fromCharCode(%s);this.href=[\'mail\',\'to:\',a,b].join(\'\');">%s</a>' %\
			   (",".join(decoded_email_name), ",".join(decoded_email_domain), name)

def do_hide_email(parser, token):
	"""Return mailto link with hidden href like this:
	<a href="mailto:[email protected]" onmouseover="var a='@';a+='dot.';a+='com';var b='user';this.href=['mail','to:',b,a].join('');">name</a>
	
	Usage example:
	{% hide_email user [email protected] %}
	"""
	bits = token.contents.split()
	if len(bits) != 3:
		raise template.TemplateSyntaxError, "'%s' tag takes three arguments" % bits[0]
	# todo: check bit[2] is valid email
	return HideEmailNode(bits[1], bits[2])

register.tag('hide_email', do_hide_email)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 11 months ago
  5. Help text hyperlinks by sa2812 11 months, 3 weeks ago

Comments

Please login first before commenting.