jsfmt and jsobf available for download
By popular request I have made my JavaScript minimizer and obfuscator available for download. The jsobf program strips JavaScript source code down to the minimum whitespace required to separate the tokens, and performs automatic semicolon insertion to completely irradiate the need for line breaks. It also offers some obfuscation by renaming identifiers.
Also available here is a JavaScript formatter, jsfmt, which reverses the minimizing process. This is useful for inspecting untidy source code, or code that has been minimized for obfuscation purposes.
» Download jsobf-1-2-0
» Download jsfmt-1-0
Both these programs are PHP command line scripts and require at least version 5.2 of the PHP CLI binary. Windows is not supported, and probably never will be.
I hope you find them useful.
If you are obfuscating source code to use with the Prototype library, be careful. Prototype actually interrogates parts of your source code literally!!! The only example I am aware of so far is the use of $super as a function argument. This cannot be renamed because Prototype searches for it as a string literal.
To solve this problem, use the –protect option of jsobf, and don’t forget to escape your “$” symbols ;)
jsobf –protect=\$super
Further comments about the –protect switch.
I ended up adding the identifiers that I wish to protect in a “special” comment syntax. Then I used a script to scan the file, collect all the identifiers to protect and then invoke jsobf.
I used the following format:
//#: MyClassName
function MyClassName()
{
…
}
MyClassName.prototype = {
…
}
Note the line starting with the //#:
It would be great if you could implement a similar feature.
Thanks, keep up the great work !!!
This is a great tool.
But I can’t remove the ‘-i’ switch without problems:
Example:
function ModalDisplay()
{
…
}
ModalDisplay.prototype = {
…
clearFavourites : function()
{
var i;
for (i=0; i<this.thumbNails.length; i++)
{
this.thumbNails[i].setSelected(false);
}
},
…
}
Example after jsobf and then jsfmt on the above:
clearFavourites : function ( ) {
var $3a;
for ( $3a = 0; $3a < this.thumbNails.length; $3a ++ ) {
this.thumbNails[i].setSelected ( false );
}
}
NOTE: the code reads this.thumbNails[i] and not this.thumbNails[$3a]
There are several places in my code where this happens and I have simply had to manually weed them out using the –protect switch. But I am still puzzled. Am I doing something wrong?
Reg
SOLVED – with workaround
I think I have isolated the situation in which the above bug occurs. In any situation as follows:
> myClass.myMember[index]
‘index’ appears to get ignored. A quick work around is to –protect one or two variable names which you can then re-use in these situations.
eg:
–protect ix,…
> for(var ix; i myClass.myMember[ix] = whatever;
> }
So yea one little bug, but still a great tool!
Thanks for this Reg. I guess the part of the parse tree that deals with bracket access isn’t carrying the obfuscated variables into its scope. I’ll take a look when I get the chance.
I didn’t realise anyone was actually using it ;)