【原创】 JS脚本 格式化 着色 工具

代码来源网络,对其进行了整合:
将下述代码另存为 *.HTA 后双击执行即可! Enjoy!

##########################################################
-//W3C//DTD HTML 3.2 Final//EN">
Javascript代码格式化与着色--By knityster
  
  Content-Type" content="text/html; charset=gb2312">
<script>
self.resizeTo(560,420);   //改变大小
//self.moveTo(screen.availWidth-900,200);   //改变位置
// javascript format script
// copyright Stephen Chapman 8th June 2005
// for use on http://javascript.about.com/ only
var kw = ['abstract', 'as', 'boolean', 'break', 'byte', 'case', 'catch', 'char', 'class', 'const', 'continue', 'default', 'delete', 'do', 'double', 'else', 'enum', 'export', 'extends', 'false', 'final', 'finally', 'float', 'for', 'function', 'goto', 'if', 'implements', 'import', 'in', 'instanceof', 'int', 'interface', 'long', 'namespace', 'native', 'new', 'null', 'package', 'private', 'protected', 'public', 'return', 'short', 'static', 'super', 'switch', 'synchronized', 'this', 'throw', 'throws', 'transient', 'true', 'try', 'typeof', 'use', 'var', 'void', 'volatile', 'while', 'with'];
var ob = ['Anchor', 'anchors', 'Applet', 'applets', 'Area', 'Array', 'Body', 'Button', 'Checkbox', 'Date', 'document', 'FileUpload', 'Form', 'forms', 'Frame', 'frames', 'Hidden', 'History', 'history', 'Image', 'images', 'Layer', 'layers', 'Link', 'links', 'location', 'Math', 'MimeType', 'mimeTypes', 'navigator', 'Number', 'Option', 'options', 'Password', 'Plugin', 'plugins', 'Radio', 'RegExp', 'Reset', 'screen', 'Script', 'Select', 'String', 'Style', 'StyleSheet', 'Submit', 'Text', 'Textarea', 'window'];
var sym = '-+*<=>?:&|/!%';
function JSfmt(s) {
this.i = this.r = this.lvl = this.pr = 0;
this.row = [''];
this.lW = this.nC = this.pC = '';
this.decode = function() {
s = s.replace(/(/r/n|/r|/n)/g, '/n');
while (this.i < s.length) { var c = s.charAt(this.i); if (s.length - 1 == this.i) this.nC = ''; else this.nC = s.charAt(this.i + 1);
if (//w/.test(c)) {if (this.lW) this.lW += c; else this.lW = c; this.row[this.r] += c; } else switch (c) {
case '/n': break;
case ' ': case '/t': this.hl(); this.row[this.r] += ' '; break;
case '.': this.hl(); this.row[this.r] += '.'; break;
case '{': this.hl(); var currentLine = this.row[this.r]; if (currentLine.length) {var lastChar = currentLine[currentLine.length - 1]; if (lastChar != ' ' && lastChar != '/t') this.row[this.r] += ' {'; else this.row[this.r] += '{';} else this.row[this.r] += '{'; this.lvl++; this.wl(); break;
case '}': this.hl(); this.lvl--; this.row[this.r] += '}'; this.wl(); break;
case ';': this.hl(); this.row[this.r] += '; '; if (this.pr == 0) this.wl(); break;
case '(': this.pr++; this.hl(); this.row[this.r] += '('; break;
case ')': this.pr--; this.hl(); this.row[this.r] += ')'; break;
case '"': case "'": this.hl(); var escaped = false; this.row[this.r] += '"literal">' + c; while (this.i < s.length - 1) {this.i++; var ch = s.charAt(this.i); if (ch == '//') escaped = !escaped; if (ch == '&') ch = '&'; if (ch == '<') ch = '<'; if (ch == '>') ch = '>'; this.row[this.r] += ch; if (c == ch && !escaped) {this.row[this.r] += '
     
     ';break;} if (ch != '//') escaped = false;} this.row[this.r] += '
     
     '; break;
case '/': if ('/' == this.nC) {this.row[this.r] += 'class="comment">//'; this.i++; while (this.i < s.length - 1) {this.i++; var c = s.charAt(this.i); if (c == '&') c = '&'; if (c == '<') c = '<'; if (c == '>') c = '>'; if (c == '/n') { this.row[this.r] += '
    
    ';   this.wl();break;} this.row[this.r] += c;}} else if (this.nC == '*') {this.row[this.r] += '/*'; this.i++; var c = ''; var prevC = ''; while (this.i < s.length - 1) {this.i++; prevC = c; c = s.charAt(this.i); if (c ==' ' || c == '/t' || c == '/n') {if (c == ' ') {if (this.row[this.r]) this.row[this.r] += ' ';} else if (c == '/t') {if (this.row[this.r]) this.row[this.r] += '   ';} else if (c == '/n') this.wl();} else this.row[this.r] += c; if (c == '/' && prevC == '*') {this.row[this.r] += '
     
     '; break;}} this.wl();} else if (this.lW) {this.hl(); if (this.nC == '=') this.row[this.r] += ' /'; else this.row[this.r] += ' / ';} else if (this.pC == '*') this.row[this.r] += '/ '; else {if (this.pC == '=') this.row[this.r] += ' /'; else this.row[this.r] += '/'; while (this.i < s.length - 1) {this.i++; var c = s.charAt(this.i); if (c == '(') this.pr++; if (c == ')') this.pr--; if (c == '//') escaped = !escaped; this.row[this.r] += c; if (c == ';' && this.pr == 0) {this.wl(); break;} if (c =='/') if (!escaped) break; if (c != '//') escaped = false;}} break;
case ',': this.hl(); this.row[this.r] += ', '; break;
case '-': case '+': case '*': case '%': case '<': case '=': case '>': case '?': case ':': case '&': case '|': case '!': this.hl(); if (c == '!' && this.nC != '=') {this.row[this.r] += c; break;} if (c == ':' && this.pC == "'") {this.row[this.r] += c; break;} if (c == '+' || c == '-') if (c == this.nC || c == this.pC) {this.row[this.r] += c; break;} if (sym.indexOf(this.pC) != -1) {if (sym.indexOf(this.nC) != -1) this.row[this.r] += c; else this.row[this.r] += c + ' ';} else {if (sym.indexOf(this.nC) != -1) this.row[this.r] += ' ' + c; else this.row[this.r] += ' ' + c + ' ';} break;
default: this.hl(); this.row[this.r] += c; break;}
if (!//w/.test(c)) if (c != ' ' && c != '/t') {this.hl(); this.lW = '';}
this.pC = c; this.i++;}
return this.row.join('
');}
this.hl = function() { if (this.lW && kw.indexOf(this.lW) != -1) {this.row[this.r] = this.row[this.r].substr(0,this.row[this.r].lastIndexOf(this.lW)) + 'class="keyword">' + this.lW + '
   
   '; this.lW = '';} else if (this.lW && ob.indexOf(this.lW) != -1) {this.row[this.r] = this.row[this.r].substr(0,this.row[this.r].lastIndexOf(this.lW)) + 'class="object">' + this.lW + '
    
    '; this.lW = '';}}
this.wl = function() {this.row.push(''); this.r++; var i = 0; while (i < this.lvl) {this.row[this.r] += '   '; i++;}}
}
if (typeof Array.prototype.indexOf == 'undefined') {Array.prototype.indexOf = function(item) {for (var i = 0; i < this.length; i++) {if ((typeof this[i] == typeof item) && (this[i] == item)) {return i;}}return -1;}}
function decode() {var jsformat = new JSfmt(document.getElementById('in').value); res(jsformat.decode());}
function res(t) {var mh = screen.height-150; var mw = screen.width-20; TheResWin = window.open('','format','height=' + mh + ',width=' + mw + ',toolbar=no,directories=no,status=no,' + 'menubar=no,scrollbars=yes,resizable=yes'); TheResWin.document.write('-//W3C//DTD XHTML 1.0 Transitional//EN"/n"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">http:www.w3.org//1999//xhtml">/nResults<//title>
  
  /n

right">#" οnclick="self.close();return false;">

right">#" οnclick="self.close();return false;"> '); TheResWin.moveTo(0,0); TheResWin.focus();}

</script>
  
  

center">Javascript 脚本格式化

cTb" align="center" cellpadding="0" cellspacing="0">
    
      

  
  
cTb2">
          00100%
            
              
                
              
            
          
0" cellpadding="" cellspacing="" width="">
                    verdana, geneva, helvetica" size="2">
                    
             
             
                    
                    

center"> verdana, geneva, helvetica" size="2">

                      
                      
                      格式化" οnclick="decode()" type="button">
                      重置" type="reset">
                    

                    verdana, geneva, helvetica" size="2">
                    
             
             
                    
                    

          
       
       
    

##########################################################
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值