Obfuscate

 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
from django import template
from django.utils.encoding import force_unicode
import random

register = template.Library()

class ObfuscateNode(template.Node):
    def __init__(self, nodelist):
        self.nodelist = nodelist

    def render(self, context):
        s = force_unicode(self.nodelist.render(context))
        code_list = []
        for c in s:
            d = ord(c)
            x = random.randint(0, d)
            code_list.append("%d+%d" % (x, d-x))
        return '<script type="text/javascript">document.write(String.fromCharCode(%s))</script>' % ",".join(code_list)


@register.tag
def obfuscate(parser, token):
    """
    Example:
    {% obfuscate %}
        <form>
            ...
            <input type="submit">
        </form>
    {% endobfuscate %}
    """
    nodelist = parser.parse(('endobfuscate',))
    parser.delete_first_token()
    return ObfuscateNode(nodelist)

Comments

ericflo (on March 12, 2008):

Wow! Doesn't the HTML produced get extremely large?

#

mikko (on March 12, 2008):

Size is ok I think, see it live here: http://www.adasweden.se/postit/

#

mikko (on March 12, 2008):

(But please don't submit anything in that form) ^^

#

(Forgotten your password?)

You may use Markdown syntax here, but raw HTML will be removed.