469,271 Members | 1,554 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

Any Javascript Obfuscator?

hsriat
1,654 Expert 1GB
Is there any free Javascript Obsfucator available?

Actually I just want the obsfucator to do two things...
1. Remove Comments
2. Rename Functions and Variables
Feb 17 '08 #1
15 3997
acoder
16,027 Expert Mod 8TB
You may find JSMin useful (at least for the first objective).
Feb 18 '08 #2
hsriat
1,654 Expert 1GB
You may find JSMin useful (at least for the first objective).
I downloaded the PHP code and made a php file to upload the required js file.
Like this:
[PHP]<?php
//file name: minify.php
if (!isset($_POST['Submit']))
{
?>
<html>
<form action=minify.php method=post enctype="multipart/form-data">
<input type=file name=js></input>
<input type=submit name=Submit value=Minify>
</form>
</html>
<?php
}
else
{
include('jsmin-1.1.0.php');

if ($_FILES['js']['error']>0)
die ('Error Uploading File');

$file_js = fopen($_FILES['js']['tmp_name'],"r");
$js = fread($file_js,$_FILES['js']['size']);
//echo $js;

$min = new JSMin;

$minified = $min->minify($js);

//echo $minified;
}
?>[/PHP]
But it gives this warning...

Missing argument 1 for JSMin::__construct()

What to do...

PS: I don't understand OOP Concepts :(
Feb 18 '08 #3
acoder
16,027 Expert Mod 8TB
I suggest you ask in the PHP forum.
Feb 18 '08 #4
hsriat
1,654 Expert 1GB
I suggest you ask in the PHP forum.
Yeah, you are right!!
My fault!!
Feb 18 '08 #5
rnd me
427 Expert 256MB
I use firefox to compress it.

wrap your code in a function, then compress by using the .toSource() method on the function.


Example:
Expand|Select|Wrap|Line Numbers
  1. function BS(){
  2.  
  3.  
  4. function el2(tid) {
  5.     return document.getElementById(tid);
  6. }
  7.  
  8. function tags(elm, tid) {
  9.     if (tid) {
  10.         if (elm.isString) {
  11.             elm = el(elm);
  12.         }
  13.         return obValsl(elm.getElementsByTagName(tid));
  14.     }
  15.     return obValsl(document.getElementsByTagName(elm));
  16. }
  17.  
  18. }
  19.  
  20.  
  21. alert(BS.toSource())
  22. /* returns:
  23. function BS() {
  24. function el2(tid) {return document.getElementById(tid);}
  25.  
  26. function tags(elm, tid) {if (tid) {if (elm.isString) {elm = el(elm);}return obValsl(elm.getElementsByTagName(tid));}return obValsl(document.getElementsByTagName(elm));}
  27. }   */
  28.  

remember to remove the first and last line of the resulting text, the function wrapper.
Feb 18 '08 #6
acoder
16,027 Expert Mod 8TB
I use firefox to compress it.

wrap your code in a function, then compress by using the .toSource() method on the function.
Nice tip (for compression, at least)!
Feb 18 '08 #7
acoder
16,027 Expert Mod 8TB
Is there any free Javascript Obsfucator available?

Actually I just want the obsfucator to do two things...
1. Remove Comments
2. Rename Functions and Variables
By the way, is this just to cut down on the size of the script? This is what minification is for.
Feb 18 '08 #8
hsriat
1,654 Expert 1GB
@acoder
Yeah, coz I ended up making at least 10 js files of all approx 25 kb. So I wanted to cut down the size. And I know the actual code size (w/o comments and spaces) is approx 15 kb, may be less than that.

@rnd me
Cool!! Thanks... :)
Feb 18 '08 #9
acoder
16,027 Expert Mod 8TB
@acoder
Yeah, coz I ended up making at least 10 js files of all approx 25 kb. So I wanted to cut down the size. And I know the actual code size (w/o comments and spaces) is approx 15 kb, may be less than that.
So, did you manage to cut it down in size?
Feb 19 '08 #10
hsriat
1,654 Expert 1GB
So, did you manage to cut it down in size?
I didn't do it yet...
Will do after testing.

Will tell you how much file size reduces...
Feb 19 '08 #11
rnd me
427 Expert 256MB
remember that in javascript, variable names are case-sensitive, so performing global replaces on compressed code could be an easy way to obfuscate names.

if you have a funciton or prototype for doing mulitple replaces, you could feed it a an array of function names, and have it substitute a serial number for each.



continuing above,
Expand|Select|Wrap|Line Numbers
  1.  
  2. String.prototype.substitute = function (r) {
  3.     O = this;
  4.     for (z = 0; z < r.length; z++) {
  5.         tre = new RegExp(r[z][0], "gm");
  6.         O = O.replace(tre, r[z][1]);
  7.     }
  8.     return O;
  9. }
  10.  
  11.  
  12. function serial(dig) {
  13.     var bb = "";
  14.   if(!dig){ dig=5; } 
  15.     for (var q = 0; q < dig; q = q + 1) {
  16.         bb = bb + String.fromCharCode(Math.round(Math.random() * 25 + 65));
  17.     }
  18.     return bb;
  19. }
  20.  
  21. var subs=[
  22.   ["el2",serial()],
  23.   [ "tags", serial()]
  24.    ] //end sub array
  25. alert(BS.toSource().substitute(subs) )
  26.  
  27.  
Feb 19 '08 #12
hsriat
1,654 Expert 1GB
So, did you manage to cut it down in size?
Yeah, quite a lot.

A 32kb well commented JS was reduced to 19 kb.
And a 15 kb, non-commented JS reduced to 14 kb (not too much tho :p)
Feb 25 '08 #13
acoder
16,027 Expert Mod 8TB
Yeah, quite a lot.

A 32kb well commented JS was reduced to 19 kb.
And a 15 kb, non-commented JS reduced to 14 kb (not too much tho :p)
Every little helps, eh?

If you're using long names for variables, functions, etc. you may want to replace those too.
Feb 25 '08 #14
hsriat
1,654 Expert 1GB
Every little helps, eh?

If you're using long names for variables, functions, etc. you may want to replace those too.
Yeah, I wanted to, but the thing you referred to didn't do that :p

And I don't want to do the find replace thing. B'coz with that, when I would do any change, after doing that with the original (commented) file, I would need to do the whole find-replace thing again.

So I wanted a one-click-solution, so that after making any change, I can get the minified* version quickly and upload it to server again.

* term used by that guy who made that program.
Feb 25 '08 #15
acoder
16,027 Expert Mod 8TB
Yeah, I wanted to, but the thing you referred to didn't do that :p

And I don't want to do the find replace thing. B'coz with that, when I would do any change, after doing that with the original (commented) file, I would need to do the whole find-replace thing again.

So I wanted a one-click-solution, so that after making any change, I can get the minified* version quickly and upload it to server again.

* term used by that guy who made that program.
Try YUI Compressor, Packer or ShrinkSafe (Dojo).
Feb 25 '08 #16

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Smugsboy | last post: by
2 posts views Thread by Luke Matuszewski | last post: by
20 posts views Thread by korund | last post: by
20 posts views Thread by twigster | last post: by
4 posts views Thread by FAQ server | last post: by
2 posts views Thread by Laurent Bugnion [MVP] | last post: by
4 posts views Thread by Peter Michaux | last post: by
11 posts views Thread by =?iso-8859-1?q?Martin_M=FCcke?= | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.