Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: Re: Compressing/Obfuscating a Javascript file

by Incognito (Pilgrim)
on Oct 10, 2001 at 05:34 UTC ( #117911=note: print w/ replies, xml ) Need Help??

in reply to Re: Compressing/Obfuscating a Javascript file
in thread Compressing/Obfuscating a Javascript file

The 'magical' function (a beautiful one-liner I never even though of) is great for most JavaScript code, but there is an exception I noticed going through my 400K of code... If the Javascript contains an anonymous function, the regex will fail... Here's a function that contains multiple anonymous (non-named) functions:

function InitializeTree (objTree, strRootID, strRootLabel, strRootURL, + strRootImage) { var objNewNode; objTree.Target = null; objTree.className = "TreeView"; objNewNode = AddTreeItem (objTree, "", strRootID, strRootLabel, st +rRootURL, strRootImage, true, ""); // Set up the event handling. with (objTree) { onmouseup = function () { mouseupTreeItem(this); }; onmousedown = function () { mousedownTreeItem(this); }; onmouseover = function () { mouseoverTreeItem(this); }; onmouseout = function () { mouseoutTreeItem(this);}; onclick = function () { onclickTreeItem(this); }; ondblclick = function () { dblclickTreeItem(this); }; onresize = function () { onresizeTree(this); }; onselectstart = function () { window.event.returnValue=false; +}; } return (0); }
For all other cases, I think it works fine... Again, I don't know where to start when a regex (or split, or whatever) that will handle this, but I feel we're almost there...

Comment on Re: Re: Compressing/Obfuscating a Javascript file
Download Code
Re: Re: Re: Compressing/Obfuscating a Javascript file
by tachyon (Chancellor) on Oct 10, 2001 at 06:21 UTC

    I presume the problem is that it is splitting on the anon functions which you do not want. Either of these two should work better:

    # alternative one - insist on "function name (" syntax or no split my @functions = split /(?=\bfunction\s+\w+\s*\()/, $data; # alternative two - functions always on line by themselves my @functions = split /^(?=function\s)/, $data;

    Alternative 1 is the better option. cheers



      Yes, Alternative 1 is the better option, but the only problem I can see now is if we have named functions in the function itself... And (to be a devil's advocate) what if those functions have functions?
      function InitializeTree (objBlah) { // Set up the event handling. with (objBlah) { test = function test2() { doSomethingHere(); }; } return (0); }

        Herein you strike the basic problem. To do this correctly you need a complete tokenisation/lexical analysis. As even Netscape and M$ do not agree on what constitutes valid code this is an interesting task. However I am not interested :-)

        If you are happy that a function definition will generally start on a line by itself then option 2 is the go. This will leave named nested functions alone which would not seem to be a major deal. You either make a compromise and be happy that you can parse a say 99% subset of all legal code with a simple rules based system or you get the sourcecode of a javascript interpretter, and hack into the guts of it.

        I once wrote some code to strip the comments out of Perl code and condense it down. It ended up about 70K and 1000 lines long, and will still fail in some specific *rare* cases (but it will strip the entire standard distro :-) It would have been quicker to hack to perl source I think. You can have a look to see how over the top it can get here




Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://117911]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2014-09-17 02:26 GMT
Find Nodes?
    Voting Booth?

    How do you remember the number of days in each month?

    Results (56 votes), past polls