Any Javascript Obfuscator?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    Any Javascript Obfuscator?

    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
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    You may find JSMin useful (at least for the first objective).

    Comment

    • hsriat
      Recognized Expert Top Contributor
      • Jan 2008
      • 1653

      #3
      Originally posted by acoder
      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.p hp method=post enctype="multip art/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::__constr uct()

      What to do...

      PS: I don't understand OOP Concepts :(

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        I suggest you ask in the PHP forum.

        Comment

        • hsriat
          Recognized Expert Top Contributor
          • Jan 2008
          • 1653

          #5
          Originally posted by acoder
          I suggest you ask in the PHP forum.
          Yeah, you are right!!
          My fault!!

          Comment

          • rnd me
            Recognized Expert Contributor
            • Jun 2007
            • 427

            #6
            I use firefox to compress it.

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


            Example:
            Code:
            function BS(){
            
            
            function el2(tid) {
                return document.getElementById(tid);
            }
            
            function tags(elm, tid) {
                if (tid) {
                    if (elm.isString) {
                        elm = el(elm);
                    }
                    return obValsl(elm.getElementsByTagName(tid));
                }
                return obValsl(document.getElementsByTagName(elm));
            }
            
            }
            
            
            alert(BS.toSource())
            /* returns:
            function BS() {
            function el2(tid) {return document.getElementById(tid);}
            
            function tags(elm, tid) {if (tid) {if (elm.isString) {elm = el(elm);}return obValsl(elm.getElementsByTagName(tid));}return obValsl(document.getElementsByTagName(elm));}
            }   */

            remember to remove the first and last line of the resulting text, the function wrapper.

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              Originally posted by rnd me
              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)!

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Originally posted by hsriat
                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.

                Comment

                • hsriat
                  Recognized Expert Top Contributor
                  • Jan 2008
                  • 1653

                  #9
                  @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... :)

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    Originally posted by hsriat
                    @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?

                    Comment

                    • hsriat
                      Recognized Expert Top Contributor
                      • Jan 2008
                      • 1653

                      #11
                      Originally posted by acoder
                      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...

                      Comment

                      • rnd me
                        Recognized Expert Contributor
                        • Jun 2007
                        • 427

                        #12
                        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,
                        Code:
                        String.prototype.substitute = function (r) {
                            O = this;
                            for (z = 0; z < r.length; z++) {
                                tre = new RegExp(r[z][0], "gm");
                                O = O.replace(tre, r[z][1]);
                            }
                            return O;
                        }
                        
                        
                        function serial(dig) {
                            var bb = "";
                          if(!dig){ dig=5; } 
                            for (var q = 0; q < dig; q = q + 1) {
                                bb = bb + String.fromCharCode(Math.round(Math.random() * 25 + 65));
                            }
                            return bb;
                        }
                        
                        var subs=[
                          ["el2",serial()],
                          [ "tags", serial()]
                           ] //end sub array
                        alert(BS.toSource().substitute(subs) )

                        Comment

                        • hsriat
                          Recognized Expert Top Contributor
                          • Jan 2008
                          • 1653

                          #13
                          Originally posted by acoder
                          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)

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #14
                            Originally posted by hsriat
                            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.

                            Comment

                            • hsriat
                              Recognized Expert Top Contributor
                              • Jan 2008
                              • 1653

                              #15
                              Originally posted by acoder
                              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.

                              Comment

                              Working...