Online Shopping : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

10 of 17 people (59%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

How do I pack, compress and obfuscate html, js and css files with Javascript?

Apr 7th, 2008 23:02
ha mo, Mark Szlazak, Mark Szlazak, Jean-Bernard Valentaten, Colin Fraser,


I do not believe it is currently possible to do this in Javascript, you
may look for programs that will prevent users from downloading your page
in a useable format. Microsoft has such a program, they use it on their
site, and I am sure there is one around to suit your need.
---
Addendum:
Well, basically it can be done, at least the decryption is doable using 
JavaScript, but you will have to encrypt the files serverside or at 
least save them in an encrypted format on your web server. Compression 
could be done too but I really wouldn't want to do it since it involves 
a lot of coding, thus not really giving you a benefit on the traffic.
All you'll need is an algorithm that is not a one-way encryption 
(invent one *g*), a blank page only containig the header and the 
decrypt function. It might look like this:
<html>
<head>
  <script language="JavaScript">
  <!--
    function decrypt(sBody)
    {
      var sDecryptedBody = '';
      //decrypt the body string here, e.g.:
      for (var i=0; i<sBody.length; i++)
      {
        var iChar = sBody.charCodeAt(i);
        if (iChar > 0)
          sDecryptedBody += String.fromCharCode(iChar - 1);
        else
          sDecryptedBody += String.fromCharCode(255);
      }
      return(sDecryptedBody);
    }
    function decode()
    {
      var sCoded = document.getElementsByTagName('body')
[0].firstChild.data;
      document.open();
      document.write(decrypt(sCoded));
      document.close();
    }
  //-->
  </script>
</head>
<body onload="decode();">
  <!-- Place the encrypted document here -->
</body>
</html>
That should do the trick. All the user will see when she views the 
sourcecode is your blank document, containing the decode and decrypt 
function in the header and some garbage in the body. But be aware that 
any skilled person will be able to reproduce the decryption algorithm 
and thus view the real sourcecode!!!
HTH,
Jean
--------
Thanks for responding. The sites I found have very limited and/or slow 
JavaScript source compressors/crunchers/obfuscators in JavaScript. I 
have a different approach using a very long regular expression which is 
more extensive, orders of magnitude faster, and done with little 
JavaScript coding if you don't include the obfuscation/file 
reading/file writing/interface source.
See this pages commented source: 
http://members.aol.com/myscript/jscompress.html
Note that obfuscation in this context does not mean encoding and 
decoding. Just removing comments, whitespace, etc. obfuscates the 
source. But what I mean is replacing identifier/variable names with 
shorter versions to further compression. No decoding is required but 
only properly running script after the replacement. This code is a bit 
longer and slower but not as long as many may think.
I may post the shorter version but to understand its "trick" requires 
some explanation which maybe too long to post here. 
Mark.
http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com