var tmpStr = "" >>
<< Dude
Hello Zip JavaS…

Author Zhou Renjian Create@ 2004-07-25 09:49
whizz Note icon

Hello Zip JavaScript

<html>
<head>
<title>Hello Zip JavaScript</title>
</head>
<body>

<script type="text/javascript" src="keywords.js"></script>
<script type="text/javascript">
var filePath = "G:/whizzoff.com/layout.bottom.js";
var FSO = new ActiveXObject("Scripting.FileSystemObject");
var fileName = FSO.GetFileName(filePath);
var fileFolderName = FSO.GetParentFolderName(filePath);
var outputFilePath = fileFolderName + "/zip." + fileName;

var txt = FSO.OpenTextFile(filePath, 1, false);
var textContent = txt.ReadAll();
txt.Close();

/* 2. remove all the / *...* / comments */
var index
var startIndex = 0;
var endIndex = 0;
var newText = "";
while (startIndex != -1 && endIndex != -1) {
 startIndex = textContent.indexOf("/*", endIndex);
 if (startIndex != -1) {
  newText += textContent.substring(endIndex, startIndex);
  endIndex = textContent.indexOf("*/", startIndex) + 2;
  if (endIndex == -1) {
   newText += textContent.substring(startIndex);
  }
 }
}
if (startIndex == -1) {
 newText += textContent.substring(endIndex);
}
if (endIndex == -1) {
 newText += textContent.substring(startIndex);
}
textContent = newText;

//textContent = "\"http://hello.com\";\r\n" + textContent;
/* 3. remove all the // comments */
textContent = textContent.replace(/\s*\/\/.*/ig, ""); // TODO: DONOT remove the "//" in a string!

/* 1. trim all the indents */
textContent = textContent.replace(/\s*(.*)/g, function($0, $1) {return $1;});

/* 4. " = " -> "=", " + " -> "+", " * " -> "*", " / " -> "/", " > " -> ">", ... */
textContent = textContent.replace(/\s*=\s*/g, "=");
textContent = textContent.replace(/\s*\+\s*/g, "+");
textContent = textContent.replace(/\s*-\s*/g, "-");
textContent = textContent.replace(/\s*\*\s*/g, "*");
textContent = textContent.replace(/\s*\/\s*/g, "/");
textContent = textContent.replace(/\s*\+=\s*/g, "+=");
textContent = textContent.replace(/\s*-=\s*/g, "-=");
textContent = textContent.replace(/\s*\*=\s*/g, "*=");
textContent = textContent.replace(/\s*\/=\s*/g, "/=");
textContent = textContent.replace(/\s*>\s*/g, ">");
textContent = textContent.replace(/\s*>=\s*/g, ">=");
textContent = textContent.replace(/\s*<\s*/g, "<");
textContent = textContent.replace(/\s*<=\s*/g, "<=");
textContent = textContent.replace(/\s*==\s*/g, "==");
textContent = textContent.replace(/\s*!=\s*/g, "!=");
textContent = textContent.replace(/\s*\|\|\s*/g, "||");
textContent = textContent.replace(/\s*&&\s*/g, "&&");
textContent = textContent.replace(/,\s*/g, ","); // !!!Be careful of this option
textContent = textContent.replace(/;[ \t]/g, ";"); // !!!Be careful of this option
textContent = textContent.replace(/\s*\?\s*/g, "?");
textContent = textContent.replace(/\s*:\s*/g, ":");
textContent = textContent.replace(/\s*&\s*/g, "&");
textContent = textContent.replace(/\s*\|\s*/g, "|");
textContent = textContent.replace(/\s*!\s*/g, "!");
textContent = textContent.replace(/\s*\^\s*/g, "^"); // !!!Be careful of this option
textContent = textContent.replace(/\s*&=\s*/g, "&=");
textContent = textContent.replace(/\s*\|=\s*/g, "|=");
textContent = textContent.replace(/\s*\^=\s*/g, "^="); // !!!Be careful of this option

/* 6. "} else " -> "}else " */
textContent = textContent.replace(/}\s*else/g, "}else");
textContent = textContent.replace(/else\s*{/g, "else{");

/* 5. "}\n}" -> "}}" */
textContent = textContent.replace(/}\s*}\s*}\s*}/g, "}}}}");
textContent = textContent.replace(/}\s*}\s*}/g, "}}}");
textContent = textContent.replace(/}\s*}/g, "}}");

/* 8. delete all the blank line */
//textContent = textContent.replace(/^\[ \t]*$/g, "");

textContent = textContent.replace(/;\r}/g, ";}");
textContent = textContent.replace(/\)\s*{/g, "){");
textContent = textContent.replace(/try\s*{/g, "try{");
textContent = textContent.replace(/}\s*catch\s*{/g, "}catch{");
textContent = textContent.replace(/for\s*\(/g, "for(");
textContent = textContent.replace(/if\s*\(/g, "if(");
textContent = textContent.replace(/while\s*\(/g, "while(");
textContent = textContent.replace(/swith\s*\(/g, "switch(");

var wordList = textContent.split(/\W/);
wordList.sort();

var newWordList = new Array();
var newWordCount = new Array();
var prevWord = "";
var wordCount = 0;
for (var i = 0; i < wordList.length; i++) {
 wordCount++;
 if (prevWord != wordList[i]) {
  if (prevWord != "") {
   newWordList[newWordList.length] = prevWord;
   newWordCount[newWordCount.length] = wordCount;
  }
  prevWord = wordList[i];
  wordCount = 0;
 }
}
for (var i = 0; i < newWordList.length; i++) {
 for (var j = i; j < newWordList.length; j++) {
  if (newWordCount[i] < newWordCount[j]) {
   var word = newWordList[i];
   newWordList[i] = newWordList[j];
   newWordList[j] = word;
   var count = newWordCount[i];
   newWordCount[i] = newWordCount[j];
   newWordCount[j] = count;
  }
 }
}


Array.prototype.contains = function(o) {
 for (var i = 0; i < this.length; i++) {
  if (this[i] == o) {
   return true;
  }
 }
 return false;
}
var renameTips = new Array(newWordList.length);
var simpleNames = "abcdefghijklmnopqrstuvwxyz".split("");
var simpleNameIndex = -1;
var prefixNames = "joztabcdefghiklmnpqrsuvwxy".split("");
function getNextSimpleName() {
 simpleNameIndex++;
 if (simpleNameIndex < simpleNames.length) {
  return simpleNames[simpleNameIndex];
 } else {
  var prefixIndex = Math.floor(simpleNameIndex / 26) - 1;
  var postfixIndex = simpleNameIndex - (prefixIndex + 1) * 26;
  return prefixNames[prefixIndex] + simpleNames[postfixIndex];
 }
}

document.write("<table width=\"100%\" height=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"1\">\r\n");
document.write("    <tr>\r\n");
document.write("        <td width=\"200\">\r\n");
document.write("<table>");
for (var i = 0; i < newWordList.length; i++) {
 if ((newWordList[i].charAt(0) < '0' || newWordList[i].charAt(0) > '9')
   && newWordList[i].length > 1
   //&& newWordCount[i] > 1
   && !keywords.contains(newWordList[i])) {
  /*
  var regExp = new RegExp();
  regExp.Pattern = newWordList[i];
  regExp.Global = true;
  */
  document.write("<td>" + i + "</td>");
  document.write("<td><input id=\"rename" + i + "\" name=\"rename" + i + "\" type=\"checkbox\"/></td>");
  document.write("<td>" + newWordList[i] + "</td>");
  document.write("<td>" + newWordCount[i] + "</td>");
  document.write("<td><span id=\"nameTip" + i + "\"></span></td>");
  document.write("</tr>");
 }
}
document.write("</table>");
document.write("<input type=\"button\" onclick=\"renameAndPreview();\" value=\"Preview\"/>");
document.write("<input type=\"button\" onclick=\"writeOutZipJavaScript();\" value=\"Write\"/>");
document.write("        </td>\t\n");
document.write("        <td width=\"*%\">\r\n");
document.write("<div id=\"preview\"><pre>" + textContent + "</pre></div>");
document.write("        </td>\r\n");
document.write("    </tr>\r\n");
document.write("</table>\r\n");

function writeOutZipJavaScript() {
 var txtOut = FSO.CreateTextFile(outputFilePath, true);
 txtOut.Write(previewText);
 txtOut.close();
}
var previewText = "";
function renameAndPreview() {
 simpleNameIndex = -1;
 previewText = textContent;
 for (var i = 0; i < newWordList.length; i++) {
  if ((newWordList[i].charAt(0) < '0' || newWordList[i].charAt(0) > '9')
    && newWordList[i].length > 1
    //&& newWordCount[i] > 1
    && !keywords.contains(newWordList[i])) {
   var checkRename = document.getElementById("rename" + i);
   if (checkRename != null && checkRename.checked) {
    var nextSimpleName = getNextSimpleName();
    while (newWordList.contains(nextSimpleName)) {
     nextSimpleName = getNextSimpleName();
    }
    renameTips[i] = nextSimpleName;
    var regExp = null;
    eval("regExp = /" + newWordList[i] + "/g;");
    var nameTip = document.getElementById("nameTip" + i);
    nameTip.innerText = renameTips[i];
    previewText = previewText.replace(regExp, renameTips[i]);
   } else {
    var nameTip = document.getElementById("nameTip" + i);
    nameTip.innerText = "";
   }
  }
 }
 var previewDIV = document.getElementById("preview");
 //alert(previewDIV);
 previewDIV.innerHTML = "<pre>" + previewText.replace(/</g, "&lt;") + "</pre>";
}
</script>
</body>
</html>



Remark

The source of zipping javascript

Try to zip the *.js:
  1. trim all the indents
  2. remove all the /*...*/ comments
  3. remove all the // comments
  4. " = " -> "=", " + " -> "+", " * " -> "*", " / " -> "/", " > " -> ">", ...
  5. "}\n}" -> "}}"
  6. "} else " -> "}else "
  7. "} else {" -> "}else{"
  8. delete all the blank line
  **NOT FINISHED**9. trim the "{" and "}" in " if { (one expression); }"
 10. trim all the tab
 11. try to replace the variable's name into simple word, such as a, b, c, d, e, f, g, ...


Source Link
本记录所在类别:
本记录相关记录: