Monday, March 10, 2003

New Promise Online Javascript Beautifier - Unreadable Beginning Code Demystified

Bookmarklets (like GMailThis, Tumblr, BlogThis!) are beautiful even then mysterious as well as real confusing.

Their beginning code is e'er scrambled (or obfuscated) - so people who fifty-fifty accept unopen to background inwards HTML as well as Javascript programming discovery it difficult to analyze as well as sympathize the underlying logic or workflow.

For instance, the cryptic code that follows is a Tumblr Bookmarklet that volition post service a text snippet, icon or video from your electrical flow webpage to Tumblr.

javascript:var d=document,w=window,e=w.getSelection,k=d.getSelection, x=d.selection, s=(e?e():(k)?k():(x?x.createRange().text:0)), f='http://www.tumblr.com/share',l=d.location, e=encodeURIComponent,p='?v=3&u='+e(l.href) +'&t='+e(d.title) +'&s='+e(s), u=f+p; try{if(!/^(.*\.)?tumblr[^.]*$/.test(l.host))throw(0); tstbklt();}catch(z) {a =function(){if(!w.open(u,'t','toolbar=0, resizable=0,status=1, width=450,height=430'))l.href=u;}; if(/Firefox/.test(navigator.userAgent)) setTimeout(a,0);else a();}void(0)

Now if nosotros format or beautify the same code amongst proper indents, linebreaks as well as whitespaces, it would hold upward to a greater extent than readable as well as brand feel to virtually developers. See the same Tumble Bookmarklet code only reformatted:

javascript: var d = document,
w = window,
e = w.getSelection,
k = d.getSelection,
x = d.selection,
s = (e ? e(): (k) ? k(): (x ? x.createRange().text: 0)),
f = 'http://www.tumblr.com/share',
l = d.location,
e = encodeURIComponent,
p = '?v=3&u=' + e(l.href) + '&t=' + e(d.title) + '&s=' + e(s),
u = f + p;
try {
    if ( ! /^(.*\.)?tumblr[^.]*$/.test(l.host))
        throw(0);
    tstbklt();
}
..[snip]..
void(0)

This was done using the gratuitous online beautifier tool for javascript available at http://elfz.laacz.lv/beautify/? - it's an opened upward beginning PHP script that you lot are gratuitous to work on your ain server.

For formatting code written inwards PHP, CSS, etc, you lot may endeavour the prettyprinter.de/ - similar to Javascript beautifier only gives to a greater extent than options for formatting the output.

No comments:

Post a Comment