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
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 8 months ago
Comments
Please login first before commenting.