Code Compactor

[Total: 0    Average: 0/5]

This will compact your source code into as few characters as possible.

Head Code:

<STYLE type=text/css>
A {	TEXT-DECORATION: none}
BODY {	FONT-SIZE: 10pt; MARGIN: 24px; 	font-family: Verdana,Narkisim,Peterburg; }
H2 {	FONT-SIZE: 140%}
H3 {	FONT-SIZE: 120%}
H4 {	FONT-SIZE: 100%}
PRE {	FONT-SIZE: 80%; FONT-FAMILY:  monospace}
TD {	FONT-SIZE: 10pt; VERTICAL-ALIGN: top; FONT-FAMILY: Verdana,Arial, Helvetica, sans-serif; TEXT-ALIGN: left}
TH {	FONT-WEIGHT: bold; FONT-SIZE: 10pt; VERTICAL-ALIGN: bottom;  TEXT-ALIGN: left}
UL {	LIST-STYLE-TYPE: square}
#demoBox {	BORDER-RIGHT: #000000 1px solid; PADDING-RIGHT: 8px; BORDER-TOP: #000000 1px solid; PADDING-LEFT: 8px; PADDING-BOTTOM: 8px; BORDER-LEFT: #000000 1px solid; WIDTH: 24em; PADDING-TOP: 8px; BORDER-BOTTOM: #000000 1px solid}
.footer {	MARGIN-BOTTOM: 0px; TEXT-ALIGN: center}
TABLE.boxed {	border-spacing: 2px}
TD.boxed {	BORDER-RIGHT: #000000 1px solid; PADDING-RIGHT: 8px; BORDER-TOP: #000000 1px solid; PADDING-LEFT: 8px; PADDING-BOTTOM: 2px; BORDER-LEFT: #000000 1px solid; COLOR: #000000; PADDING-TOP: 2px; BORDER-BOTTOM: #000000 1px solid; BACKGROUND-COLOR: #ffffff}
TH.boxed {	BORDER-RIGHT: #000000 1px solid; PADDING-RIGHT: 8px; BORDER-TOP: #000000 1px solid; PADDING-LEFT: 8px; PADDING-BOTTOM: 2px; BORDER-LEFT: #000000 1px solid; COLOR: #000000; PADDING-TOP: 2px; BORDER-BOTTOM: #000000 1px solid; BACKGROUND-COLOR: #c0c0c0}
TH.boxedHeader {	BORDER-RIGHT: #000000 1px solid; PADDING-RIGHT: 8px; BORDER-TOP: #000000 1px solid; PADDING-LEFT: 8px; PADDING-BOTTOM: 2px; BORDER-LEFT: #000000 1px solid; COLOR: #ffffff; PADDING-TOP: 2px; BORDER-BOTTOM: #000000 1px solid; BACKGROUND-COLOR: #808080}
INPUT.text {	BORDER-TOP-WIDTH: 2px; PADDING-RIGHT: 2px; PADDING-LEFT: 2px; BORDER-LEFT-WIDTH: 2px; BORDER-LEFT-COLOR: #00d0d0; BORDER-BOTTOM-WIDTH: 2px; BORDER-BOTTOM-COLOR: #00d0d0; PADDING-BOTTOM: 2px; BORDER-TOP-COLOR: #00d0d0; PADDING-TOP: 2px; SCROLLBAR-BASE-COLOR: #00d0d0; BACKGROUND-COLOR: red; BORDER-RIGHT-WIDTH: 2px; BORDER-RIGHT-COLOR: #aad0d0}
TEXTAREA {	BORDER-TOP-WIDTH: 2px; PADDING-RIGHT: 2px; PADDING-LEFT: 2px; BORDER-LEFT-WIDTH: 2px; BORDER-LEFT-COLOR: #00d0d0; BORDER-BOTTOM-WIDTH: 2px; BORDER-BOTTOM-COLOR: #00d0d0; PADDING-BOTTOM: 2px; BORDER-TOP-COLOR: #00d0d0; PADDING-TOP: 2px; SCROLLBAR-BASE-COLOR: #00d0d0; BACKGROUND-COLOR: green; BORDER-RIGHT-WIDTH: 2px; BORDER-RIGHT-COLOR: #aad0d0}
INPUT.button {	PADDING-RIGHT: 0px; PADDING-LEFT: 0px; FONT-WEIGHT: bold; FONT-SIZE: 10pt; BORDER-LEFT-COLOR: #00d0d0; BORDER-BOTTOM-COLOR: #00d0d0; PADDING-BOTTOM: 0px; WIDTH: 5em; BORDER-TOP-COLOR: #00d0d0; PADDING-TOP: 0px;  BACKGROUND-COLOR: aqua; BORDER-RIGHT-COLOR: blue}
TABLE {	BORDER-RIGHT: 2px outset; PADDING-RIGHT: 4px; BORDER-TOP: 2px outset; PADDING-LEFT: 4px; PADDING-BOTTOM: 4px; BORDER-LEFT: 2px outset; PADDING-TOP: 4px; BORDER-BOTTOM: 2px outset; BACKGROUND-COLOR: yellow}
TD {	FONT-SIZE: 10pt; }
TH {	FONT-SIZE: 10pt; }
TH {	BORDER-RIGHT: #000000 1px solid; PADDING-RIGHT: 8px; BORDER-TOP: #000000 1px solid; PADDING-LEFT: 8px; BORDER-LEFT: #000000 1px solid; WIDTH: auto; COLOR: #ffffff; BORDER-BOTTOM: #000000 1px solid; BACKGROUND-COLOR: #008080; TEXT-ALIGN: left}
</STYLE>
<SCRIPT type=text/javascript>

var literalStrings;  // For temporary storage of literal strings.

function crunch(f) {

  var input, output;
  var i;

  // Get input script code, process it and display output.

  f.elements.codeOut.value = "";
  f.elements.statusMsg.value = "Working...";
  input = f.elements.codeIn.value;
  output = input;
  f.elements.statusMsg.value = "Replacing literal strings...";
  output = replaceLiteralStrings(output);
  f.elements.statusMsg.value = "Removing comments...";
  output = removeComments(output);
  f.elements.statusMsg.value = "Compressing white space...";
  output = compressWhiteSpace(output);
  f.elements.statusMsg.value = "Restoring literal strings...";
  output = restoreLiteralStrings(output);
  f.elements.statusMsg.value = "Combining literal strings...";
  output = combineLiteralStrings(output);
  f.elements.statusMsg.value = "Done.";
  f.elements.codeOut.value = output;

  // Compute sizes.

  f.elements.sizeIn.value   = input.length;
  f.elements.sizeOut.value  = output.length;
  f.elements.sizeDiff.value = input.length - output.length;
  if (input.length > 0)
    f.elements.pctOut.value = 100 -
      Math.round(output.length / input.length * 10000) / 100;

  return false;
}

function replaceLiteralStrings(s) {

  var i, c, t, lines, escaped, quoteChar, inQuote, literal;

//  literalStrings = new Array(s.length);    // Preallocate some memory.
  literalStrings = new Array();
  t = "";

  // Split script into individual lines.

  lines = s.split("n");
  for (i = 0; i < lines.length; i++) {

    j = 0;
    inQuote = false;
    while (j <= lines[i].length) {
      c = lines[i].charAt(j);

      // If not already in a string, look for the start of one.

      if (!inQuote) {
        if (c == '"' || c == "'") {
          inQuote = true;
          escaped = false;
          quoteChar = c;
          literal = c;
        }
       else
         t += c;
      }

      // Already in a string, look for end and copy characters.

      else {
        if (c == quoteChar && !escaped) {
          inQuote = false;
          literal += quoteChar;
          t += "__" + literalStrings.length + "__";
          literalStrings[literalStrings.length] = literal;
        }
        else if (c == "\")
          escaped = true;
        else
          escaped = false;
        literal += c;
      }
      j++;
    }
    t += "n";
  }

  return t;
}

function removeComments(s) {

  var lines, i, t;

  // Remove '//' comments from each line.

  lines = s.split("n");
  t = "";
  for (i = 0; i < lines.length; i++)
    t += lines[i].replace(/(.*)//.*$/g, "$1");

  // Replace newline characters with spaces.

  t = t.replace(/(.*)n(.*)/g, "$1 $2");

  // Remove '/* ... */' comments.

  t = t.replace(/(.*)x2fx2a(.*|n)x2ax2f(.*)/g, "$1 $3");

  return t;
}

function compressWhiteSpace(s) {

  // Condense white space.

  s = s.replace(/s+/g, " ");
  s = s.replace(/^s(.*)/, "$1");
  s = s.replace(/(.*)s$/, "$1");

  // Remove uneccessary white space around operators, braces and parentices.

  s = s.replace(/s([x21x25x26x28x29x2ax2bx2cx2dx2fx3ax3bx3cx3dx3ex3fx5bx5dx5cx7bx7cx7dx7e])/g, "$1");
  s = s.replace(/([x21x25x26x28x29x2ax2bx2cx2dx2fx3ax3bx3cx3dx3ex3fx5bx5dx5cx7bx7cx7dx7e])s/g, "$1");
  return s;
}

function restoreLiteralStrings(s) {

  var i;

  for (i = 0; i < literalStrings.length; i++)
    s = s.replace(new RegExp("__" + i + "__"), literalStrings[i]);

  return s;
}

function combineLiteralStrings(s) {

  var i;

  s = s.replace(/"+"/g, "");
  s = s.replace(/'+'/g, "");

  return s;
}

</SCRIPT>

Body Code:

<CENTER>
<DIV id=demoBox>
<H3>JavaScript Crunchinator Demo</H3>
<P>You can condense JavaScript code using the form below. Just cut and paste any 
script code into the text area marked 'Input', press the 'Crunch' button and the 
condensed version will appear under 'Output.'</P>
<P>You can then use the 'Select' button to quickly highlight the output for easy 
cut and paste into a new source file.</P>
</DIV>
</CENTER>
<FORM onsubmit="return crunch(this)">
<P>
<TABLE border=0>
  <TBODY>
  <TR>
    <TH colSpan=3>Input</TH></TR>
  <TR>
    <TD colSpan=3><TEXTAREA name=codeIn rows=10 cols=80></TEXTAREA></TD></TR>
  <TR>
    <TD><B>Bytes:</B> <INPUT class=text disabled size=10 name=sizeIn></TD>
    <TD><B>Status:</B> <INPUT class=text disabled size=30 name=statusMsg></TD>
    <TD style="TEXT-ALIGN: right"><INPUT class=button type=submit value=Crunch> <INPUT class=button type=reset value=Clear></TD></TR></TBODY></TABLE></P>
<P>
<TABLE border=0>
  <TBODY>
  <TR>
    <TH colSpan=4>Output</TH></TR>
  <TR>
    <TD colSpan=4><TEXTAREA name=codeOut rows=10 cols=80></TEXTAREA></TD></TR>
  <TR>
    <TD><B>Bytes:</B> <INPUT class=text disabled size=10 name=sizeOut></TD>
    <TD><B>Saved:</B> <INPUT class=text disabled size=10 name=sizeDiff></TD>
    <TD><B>% Saved:</B> <INPUT class=text disabled size=10 name=pctOut></TD>
    <TD style="TEXT-ALIGN: right"><INPUT class=button onclick=this.form.codeOut.select();this.form.codeOut.focus(); type=button value=Select></TD></TR></TBODY></TABLE></P></FORM></BODY></HTML>



</CENTER>

Times Viewed: 0

Leave a Reply

Your email address will not be published. Required fields are marked *