代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link href="toolsite.css" rel="stylesheet" type="text/css"> <title></title> </head> <body> <!--warp--> <div class="wrap"> <!--main--> <div class="main"> <script src="base.js" type="text/javascript"></script> <script type="text/javascript"> function pack_js(base64) { var input = document.getElementById('content').value; var packer = new Packer; if (base64) { var output = packer.pack(input, 1, 0); } else { var output = packer.pack(input, 0, 0); } document.getElementById('content').value = output; } </script> <form name="aspnetForm" method="post" action="JsFormat.aspx" id="aspnetForm"> <div style="color: rgb(0, 106, 173); font-size: 16px; font-weight: bold;" align="center">JavaScript/HTML格式化</div> <textarea id="content" name="content" style="width: 850px; border: 1px solid rgb(197, 226, 242); height: 300px; overflow: visible;" cols="20" rows="20"> function htmlspecialchars(string, quote_style, charset, double_encode) { // Convert special characters to HTML entities // // version: 1004.2314 // discuss at: http://phpjs.org/functions/htmlspecialchars // + original by: Mirek Slugen // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + bugfixed by: Nathan // + bugfixed by: Arno // + revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + bugfixed by: Brett Zamir (http://brett-zamir.me) // + input by: Ratheous // + input by: Mailfaker (http://www.weedem.fr/) // + reimplemented by: Brett Zamir (http://brett-zamir.me) // + input by: felix // + bugfixed by: Brett Zamir (http://brett-zamir.me) // % note 1: charset argument not supported // * example 1: htmlspecialchars("<a href='test'>Test</a>", 'ENT_QUOTES'); // * returns 1: '&lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;' // * example 2: htmlspecialchars("ab\"c'd", ['ENT_NOQUOTES', 'ENT_QUOTES']); // * returns 2: 'ab"c&#039;d' // * example 3: htmlspecialchars("my "&entity;" is still here", null, null, false); // * returns 3: 'my &quot;&entity;&quot; is still here' var optTemp = 0, i = 0, noquotes = false; if (typeof quote_style === 'undefined' || quote_style === null) { quote_style = 2; } string = string.toString(); if (double_encode !== false) { // Put this first to avoid double-encoding string = string.replace(/&/g, '&amp;'); } string = string.replace(/</g, '&lt;').replace(/>/g, '&gt;'); var OPTS = { 'ENT_NOQUOTES': 0, 'ENT_HTML_QUOTE_SINGLE': 1, 'ENT_HTML_QUOTE_DOUBLE': 2, 'ENT_COMPAT': 2, 'ENT_QUOTES': 3, 'ENT_IGNORE': 4 }; if (quote_style === 0) { noquotes = true; } if (typeof quote_style !== 'number') { // Allow for a single string or an array of string flags quote_style = [].concat(quote_style); for (i = 0; i < quote_style.length; i++) { // Resolve string input to bitwise e.g. 'PATHINFO_EXTENSION' becomes 4 if (OPTS[quote_style[i]] === 0) { noquotes = true; } else if (OPTS[quote_style[i]]) { optTemp = optTemp | OPTS[quote_style[i]]; } } quote_style = optTemp; } if (quote_style & OPTS.ENT_HTML_QUOTE_SINGLE) { string = string.replace(/'/g, '&#039;'); } if (!noquotes) { string = string.replace(/"/g, '&quot;'); } return string; } </textarea> <select name="tabsize" id="tabsize"> <option selected="selected" value="1"> 制表符缩进 </option> <option value="2"> 2个空格缩进 </option> <option value="4"> 4个空格缩进 </option> <option value="8"> 8个空格缩进 </option> </select> <input class="but" value="格式化" οnclick="return do_js_beautify()" id="beautify" type="button"> <input class="but" value="普通压缩" οnclick="pack_js(0)" type="button"> <input class="but" value="加密压缩" οnclick="pack_js(1)" type="button"> </form> </div> </div> </body> </html>
另一种:
<script>
a=62;
function encode() {
var code = document.getElementById('code').value;
code = code.replace(/[\r\n]+/g, '');
code = code.replace(/'/g, "\\'");
var tmp = code.match(/\b(\w+)\b/g);
tmp.sort();
var dict = [];
var i, t = '';
for(var i=0; i<tmp.length; i++) {
if(tmp[i] != t) dict.push(t = tmp[i]);
}
var len = dict.length;
var ch;
for(i=0; i<len; i++) {
ch = num(i);
code = code.replace(new RegExp('\\b'+dict[i]+'\\b','g'), ch);
if(ch == dict[i]) dict[i] = '';
}
document.getElementById('code').value = "eval(function(p,a,c,k,e,d){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)d[e(c)]=k[c]||e(c);k=[function(e){return d[e]}];e=function(){return'\\\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\\\b'+e(c)+'\\\\b','g'),k[c]);return p}("
+ "'"+code+"',"+a+","+len+",'"+ dict.join('|')+"'.split('|'),0,{}))";
}
function num(c) {
return(c<a?'':num(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36));
}
function run() {
eval(document.getElementById('code').value);
}
function decode() {
var code = document.getElementById('code').value;
code = code.replace(/^eval/, '');
document.getElementById('code').value = eval(code);
}
</script>
<textarea id=code cols=80 rows=20>
</textarea>
<input type=button οnclick=encode() value=编码>
<input type=button οnclick=run() value=执行>
<input type=button οnclick=decode() value=解码>