首页 > web前端 > js教程 > 正文

简单JS代码压缩器_javascript技巧

WBOY
发布: 2016-05-16 19:25:23
原创
1266 人浏览过

1、

复制代码 代码如下:



<script><br>type="text/javascript"> <BR>//去除所有的注释 <BR>String.prototype.DeleteComment = function () <BR>{ <BR> var str = this.replace(/(['"])(.+?)(['"])/g,function(s,s1,s2,s3){return s1+s2.replace(/[\/\*]/g,"\\$&")+s3}); <BR> str = str.replace(/\/\/[^\r]+|\/\*[\s\S]+?\*\//g,""); <BR> str = str.replace(/(['"])(.+?)(['"])/g,function(s,s1,s2,s3){return s1+s2.replace(/\\([^\\])/g,"$1")+s3}); <BR> return str; <BR>} <BR>//格式代码 <BR>String.prototype.FormatCode = function () <BR>{ <BR> return this.replace(/\\$/mg,"").replace(/[^\s>;]$/mg,"$&;"); <BR>} <BR>//删除字符串前后多余的空格 <BR>String.prototype.Trim = function (m) <BR>{ <BR> return this.replace(m ? /^\s*|\s*$/mg : /^\s*|\s*$/g, ""); <BR>} <BR>//运行代码 <BR>function RunCode(obj) <BR>{ <BR> window.open('','_blank').document.write(obj.value); <BR>} <br><br></script>

测试代码:

 


第1步: 
 

第2步: 
 

第3步: 
 

第4步: 
 



2、
复制代码 代码如下:
Format





<script> <BR><!-- <BR>/**//**//**//** <BR>** ================================================================================================== <BR>** 类名:CLASS_FORMATER <BR>** 功能:JS格式化 <BR>** 示例: <BR> --------------------------------------------------------------------------------------------------- <br><br> var xx = new CLASS_FORMATER(code); <br><br> document.getElementById("display").innerHTML = xx.format(); <br><br> --------------------------------------------------------------------------------------------------- <BR>** 作者:ttyp <BR>** 邮件:<a href="mailto:ttyp@21cn.com">ttyp@21cn.com <BR>** 日期:2006-5-21 <BR>** 版本:0.1 <BR>** ================================================================================================== <BR>**/ <br><br>function CLASS_FORMAT(code){ <BR> //哈希表类 <BR> function Hashtable(){ <BR> this._hash = new Object(); <BR> this.add = function(key,value){ <BR> if(typeof(key)!="undefined"){ <BR> if(this.contains(key)==false){ <BR> this._hash[key]=typeof(value)=="undefined"?null:value; <BR> return true; <BR> } else { <BR> return false; <BR> } <BR> } else { <BR> return false; <BR> } <BR> } <BR> this.remove = function(key){delete this._hash[key];} <BR> this.count = function(){var i=0;for(var k in this._hash){i++;} return i;} <BR> this.items = function(key){return this._hash[key];} <BR> this.contains = function(key){return typeof(this._hash[key])!="undefined";} <BR> this.clear = function(){for(var k in this._hash){delete this._hash[k];}} <br><br> } <br><br> this._caseSensitive = true; <br><br> //字符串转换为哈希表 <BR> this.str2hashtable = function(key,cs){ <br><br> var _key = key.split(/,/g); <BR> var _hash = new Hashtable(); <BR> var _cs = true; <br><br> <BR> if(typeof(cs)=="undefined"||cs==null){ <BR> _cs = this._caseSensitive; <BR> } else { <BR> _cs = cs; <BR> } <br><br> for(var i in _key){ <BR> if(_cs){ <BR> _hash.add(_key[i]); <BR> } else { <BR> _hash.add((_key[i]+"").toLowerCase()); <BR> } <br><br> } <BR> return _hash; <BR> } <br><br> //获得需要转换的代码 <BR> this._codetxt = code; <br><br> if(typeof(syntax)=="undefined"){ <BR> syntax = ""; <BR> } <br><br> this._deleteComment = false; <BR> //是否大小写敏感 <BR> this._caseSensitive = true; <BR> //可以后面加块语句的关键字 <BR> this._blockElement = this.str2hashtable("switch,if,while,try,finally"); <BR> //是函数申明 <BR> this._function = this.str2hashtable("function"); <BR> //本行括号内分号不做换行 <BR> this._isFor = "for"; <br><br> this._choiceElement = this.str2hashtable("else,catch"); <br><br> this._beginBlock = "{"; <BR> this._endBlock = "}"; <br><br> this._singleEyeElement = this.str2hashtable("var,new,return,else,delete,in,case"); <BR> //得到分割字符 <BR> this._wordDelimiters= "  ,.?!;:\\/<>(){}[]\"'\r\n\t=+-|*%@#$^&"; <BR> //引用字符 <BR> this._quotation = this.str2hashtable("\",'"); <BR> //行注释字符 <BR> this._lineComment = "//"; <BR> //转义字符 <BR> this._escape = "\\"; <BR> //多行引用开始 <BR> this._commentOn = "/*"; <BR> //多行引用结束 <BR> this._commentOff = "*/"; <BR> //行结束词 <BR> this._rowEnd = ";"; <br><br> this._in = "in"; <br><br><BR> this.isCompress = false; <BR> this.style = 0; <br><br> this._tabNum = 0; <br><br><BR> this.format = function() { <BR> var codeArr = new Array(); <BR> var word_index = 0; <BR> var htmlTxt = new Array(); <br><br> if(this.isCompress){ <BR> this._deleteComment = true; <BR> } <br><br><BR> //得到分割字符数组(分词) <BR> for (var i = 0; i < this._codetxt.length; i++) { <BR> if (this._wordDelimiters.indexOf(this._codetxt.charAt(i)) == -1) { //找不到关键字 <BR> if (codeArr[word_index] == null || typeof(codeArr[word_index]) == 'undefined') { <BR> codeArr[word_index] = ""; <BR> } <BR> codeArr[word_index] += this._codetxt.charAt(i); <BR> } else { <BR> if (typeof(codeArr[word_index]) != 'undefined' && codeArr[word_index].length > 0) <BR> word_index++; <BR> codeArr[word_index++] = this._codetxt.charAt(i); <BR> } <BR> } <br><br><BR> var quote_opened = false; //引用标记 <BR> var slash_star_comment_opened = false; //多行注释标记 <BR> var slash_slash_comment_opened = false; //单行注释标记 <BR> var line_num = 1; //行号 <BR> var quote_char = ""; //引用标记类型 <br><br> var function_opened = false; <br><br> var bracket_open = false; <BR> var for_open = false; <br><br> //按分割字,分块显示 <BR> for (var i=0; i <=word_index; i++){ <BR> //处理空行(由于转义带来) <BR> if(typeof(codeArr[i])=="undefined"||codeArr[i].length==0){ <BR> continue; <BR> } else if(codeArr[i]==" "||codeArr[i]=="\t"){ <BR> if(slash_slash_comment_opened||slash_star_comment_opened){ <BR> if(!this._deleteComment){ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> } <BR> } <BR> if(quote_opened){ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> } <BR> } else if(codeArr[i]=="\n"){ <BR> //处理换行 <BR> } else if (codeArr[i] == "\r"){ <BR> slash_slash_comment_opened = false; <BR> quote_opened = false; <BR> line_num++; <BR> if(!this.isCompress){ <BR> htmlTxt[htmlTxt.length] = "\r\n"+ this.getIdent(); <BR> } <BR> //处理function里的参数标记 <BR> } else if (!slash_slash_comment_opened&&!slash_star_comment_opened && !quote_opened &&this.isFunction(codeArr[i])){ <BR> htmlTxt[htmlTxt.length] = codeArr[i] + " "; <BR> function_opened = true; <BR> } else if (!slash_slash_comment_opened&&!slash_star_comment_opened && !quote_opened &&codeArr[i]==this._isFor){ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> for_open = true; <BR> } else if (!slash_slash_comment_opened&&!slash_star_comment_opened && !quote_opened &&codeArr[i]=="("){ <BR> bracket_open = true; <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> } else if (!slash_slash_comment_opened&&!slash_star_comment_opened && !quote_opened &&codeArr[i]==")"){ <BR> bracket_open = false; <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> } else if (!slash_slash_comment_opened&&!slash_star_comment_opened && !quote_opened &&codeArr[i]==this._rowEnd){ <BR> if(!this.isCompress){ <BR> if(!for_open){ <BR> if(i<word_index&&(codeArr[i+1]!="\r"&&codeArr[i+1]!="\n")){ <BR> htmlTxt[htmlTxt.length] = codeArr[i] + "\n" + this.getIdent(); <BR> }else{ <BR> htmlTxt[htmlTxt.length] = codeArr[i] + this.getIdent(); <BR> } <BR> }else{ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> } <BR> }else{ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> } <BR> } else if(!slash_slash_comment_opened&&!slash_star_comment_opened && !quote_opened &&codeArr[i]==this._beginBlock){ <BR> for_open = false; <BR> if(!this.isCompress){ <BR> switch(this.style){ <BR> case 0: <BR> this._tabNum++; <BR> htmlTxt[htmlTxt.length] = codeArr[i] + "\n" + this.getIdent(); <BR> break; <BR> case 1: <BR> htmlTxt[htmlTxt.length] = "\n" + this.getIdent(); <BR> this._tabNum++; <BR> htmlTxt[htmlTxt.length] = codeArr[i] + "\n"+ this.getIdent(); <BR> break; <BR> default: <BR> this._tabNum++; <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> break; <br><br> } <BR> }else{ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> } <br><br> } else if(!slash_slash_comment_opened&&!slash_star_comment_opened && !quote_opened &&codeArr[i]==this._endBlock){ <BR> if(!this.isCompress){ <BR> this._tabNum--; <BR> if(i<word_index&&codeArr[i+1]!=this._rowEnd){ <BR> htmlTxt[htmlTxt.length] = "\n" + this.getIdent() + codeArr[i]; <BR> }else{ <BR> htmlTxt[htmlTxt.length] = "\n" + this.getIdent() + codeArr[i]; <BR> } <BR> }else{ <BR> if(i<word_index&&codeArr[i+1]!=this._rowEnd){ <BR> htmlTxt[htmlTxt.length] = codeArr[i] + this._rowEnd; <BR> }else{ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> } <BR> } <BR> //处理关键字 <BR> } else if (!slash_slash_comment_opened&&!slash_star_comment_opened && !quote_opened && this.isBlockElement(codeArr[i])){ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> //处理内置对象(后面加一个空格) <BR> } else if (!slash_slash_comment_opened&&!slash_star_comment_opened && !quote_opened && this.isSingleEyeElement(codeArr[i])){ <BR> if(codeArr[i]==this._in){ <BR> htmlTxt[htmlTxt.length] = " "; <BR> } <BR> htmlTxt[htmlTxt.length] = codeArr[i] + " "; <BR> //处理双引号(引号前不能为转义字符) <BR> } else if (!slash_star_comment_opened&&!slash_slash_comment_opened&&this._quotation.contains(codeArr[i])){ <BR> if (quote_opened){ <BR> //是相应的引号 <BR> if(quote_char==codeArr[i]){ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> quote_opened = false; <BR> quote_char = ""; <BR> } else { <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> } <BR> } else { <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> quote_opened = true; <BR> quote_char = codeArr[i]; <BR> } <BR> //处理转义字符 <BR> } else if(codeArr[i] == this._escape){ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> if(i<word_index-1){ <BR> if(codeArr[i+1].charCodeAt(0)>=32&&codeArr[i+1].charCodeAt(0)<=127){ <BR> htmlTxt[htmlTxt.length] = codeArr[i+1].substr(0,1); <BR> htmlTxt[htmlTxt.length] = codeArr[i+1].substr(1); <BR> i=i+1; <BR> } <BR> } <BR> //处理多行注释的开始 <BR> } else if (!slash_slash_comment_opened && !slash_star_comment_opened&&!quote_opened&&this.isStartWith(this._commentOn,codeArr,i)){ <BR> slash_star_comment_opened = true; <BR> if(!this._deleteComment){ <BR> htmlTxt[htmlTxt.length] = this._commentOn; <BR> } <BR> i = i + this.getSkipLength(this._commentOn); <BR> //处理单行注释 <BR> } else if (!slash_slash_comment_opened && !slash_star_comment_opened&&!quote_opened&&this.isStartWith(this._lineComment,codeArr,i)){ <BR> slash_slash_comment_opened = true; <BR> if(!this._deleteComment){ <BR> htmlTxt[htmlTxt.length] = this._lineComment; <BR> } <BR> i = i + this.getSkipLength(this._lineComment); <BR> //处理忽略词 <BR> } else if (!slash_slash_comment_opened && !slash_star_comment_opened&&!quote_opened&&this.isStartWith(this._ignore,codeArr,i)){ <BR> slash_slash_comment_opened = true; <BR> htmlTxt[htmlTxt.length] = this._ignore; <BR> i = i + this.getSkipLength(this._ignore); <BR> //处理多行注释结束 <BR> } else if (!quote_opened&&!slash_slash_comment_opened&&this.isStartWith(this._commentOff,codeArr,i)){ <BR> if (slash_star_comment_opened) { <BR> slash_star_comment_opened = false; <BR> if(!this._deleteComment){ <BR> htmlTxt[htmlTxt.length] = this._commentOff; <BR> } <BR> i = i + this.getSkipLength(this._commentOff); <BR> } <BR> } else { <BR> //不是在字符串中 <BR> if(!quote_opened){ <BR> //如果不是在注释重 <BR> if(!slash_slash_comment_opened && !slash_star_comment_opened){ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> //注释中 <BR> }else{ <BR> if(!this._deleteComment){ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> } <BR> } <BR> }else{ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> } <BR> } <br><br> } <br><br> return htmlTxt.join(""); <BR> } <br><br> this.isStartWith = function(str,code,index){ <br><br> if(typeof(str)!="undefined"&&str.length>0){ <BR> var cc = new Array(); <BR> for(var i=index;i<index+str.length;i++){ <BR> cc[cc.length] = code[i]; <BR> } <BR> var c = cc.join(""); <BR> if(this._caseSensitive){ <BR> if(str.length>=code[index].length&&c.indexOf(str)==0){ <BR> return true; <BR> } <BR> }else{ <BR> if(str.length>=code[index].length&&c.toLowerCase().indexOf(str.toLowerCase())==0){ <BR> return true; <BR> } <BR> } <BR> return false; <br><br> } else { <BR> return false; <BR> } <BR> } <br><br> this.isFunction = function(val){ <BR> return this._function.contains(this._caseSensitive?val:val.toLowerCase()); <BR> } <br><br> this.isBlockElement = function(val) { <BR> return this._blockElement.contains(this._caseSensitive?val:val.toLowerCase()); <BR> } <br><br> this.isChoiceElement = function(val) { <BR> return this._choiceElement.contains(this._caseSensitive?val:val.toLowerCase()); <BR> } <br><br> this.isSingleEyeElement = function(val) { <BR> return this._singleEyeElement.contains(this._caseSensitive?val:val.toLowerCase()); <BR> } <br><br> this.isNextElement = function(from,word){ <BR> for(var i=from;i<word.length;i++){ <BR> if(word[i]!=" "&&word[i]!="\t"&&word[i]!="\r"&&word[i]!="\n"){ <BR> return this.isChoiceElement(word[i]); <BR> } <BR> } <BR> return false; <BR> } <br><br> this.getSkipLength = function(val){ <BR> var count = 0; <BR> for(var i=0;i<val.length;i++){ <BR> if(this._wordDelimiters.indexOf(val.charAt(i))>=0){ <BR> count++; <BR> } <BR> } <BR> if(count>0){ <BR> count=count-1; <BR> } <BR> return count; <BR> } <br><br> this.getIdent=function(){ <BR> var n = []; <BR> for(var i=0;i<this._tabNum;i++){ <BR> n[n.length] = "\t"; <BR> } <BR> return n.join(""); <BR> } <BR>} <br><br>function doformat(o){ <BR> var htmltxt = ""; <br><br> if (o == null){ <BR> alert("domNode is null!"); <BR> return; <BR> } <br><br> var _codetxt = ""; <br><br> if(typeof(o)=="object"){ <BR> switch(o.tagName){ <BR> case "TEXTAREA": <BR> case "INPUT": <BR> _codetxt = o.value; <BR> break; <BR> case "DIV": <BR> case "SPAN": <BR> _codetxt = o.innerText; <BR> break; <BR> default: <BR> _codetxt = o.innerHTML; <BR> break; <BR> } <BR> }else{ <BR> _codetxt = o; <BR> } <br><br> var _syn = new CLASS_FORMAT(_codetxt); <BR> htmltxt = _syn.format(); <BR> return htmltxt; <BR>} <br><br><BR>function go() <BR>{ <BR> var code = document.getElementById("code").value; <BR> var xx = new CLASS_FORMAT(code); <BR> var a = new Date(); <br><br> if(document.getElementById('cboOperate').selectedIndex==1){ <BR> xx.isCompress=true; <BR> }else{ <BR> xx.style = parseInt(document.getElementById('cboStyle').value); <BR> } <BR> document.getElementById("display").value = xx.format(); <BR> alert("共花:" + (new Date().getTime()-a.getTime()) + "ms"); <BR>} <BR>//--> <BR></script>
 








相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!