如果想自己写的JS代码不想别人轻易使用,进行一下简单的加工是有必要的,网络上有太多这样的功能,但是如果能私定一个专属自己的加解密系统,这个是一个非常好的参考。
源码已上传,点击下载
使用方法
先引用JS文件
var packer = new Packer;
就使用一个 pack
方法
压缩
output = packer.pack(源码); // output 为压缩后的代码
加密
output = packer.pack(源码, 1); // output 为加密后的代码
混淆
output = packer.pack(源码, 0, 1); // output 为混淆后的代码
output = packer.pack(源码, 1, 1); // 混淆加密
美化
output = js_beautify(源码, 1, '\t');
美化代码好像没有打进包里,源码贴出来
function js_beautify(js_source_text, indent_size, indent_character, indent_level)
{
var input, output, token_text, last_type, last_text, last_word, current_mode, modes, indent_string;
var whitespace, wordchar, punct, parser_pos, line_starters, in_case;
var prefix, token_type, do_block_just_closed, var_line, var_line_tainted;
function trim_output()
{
while (output.length && (output[output.length - 1] === ' ' || output[output.length - 1] === indent_string)) {
output.pop();
}
}
function print_newline(ignore_repeated)
{
ignore_repeated = typeof ignore_repeated === 'undefined' ? true: ignore_repeated;
trim_output();
if (!output.length) {
return; // no newline on start of file
}
if (output[output.length - 1] !== "\n" || !ignore_repeated) {
output.push("\n");
}
for (var i = 0; i < indent_level; i++) {
output.push(indent_string);
}
}
function print_space()
{
var last_output = output.length ? output[output.length - 1] : ' ';
if (last_output !== ' ' && last_output !== '\n' && last_output !== indent_string) {
// prevent occassional duplicate space
output.push(' ');
}
}
function print_token()
{
output.push(token_text);
}
function indent()
{
indent_level++;
}
function unindent()
{
if (indent_level) {
indent_level--;
}
}
function remove_indent()
{
if (output.length && output[output.length - 1] === indent_string) {
output.pop();
}
}
function set_mode(mode)
{
modes.push(current_mode);
current_mode = mode;
}
function restore_mode()
{
do_block_just_closed = current_mode === 'DO_BLOCK';
current_mode = modes.pop();
}
function in_array(what, arr)
{
for (var i = 0; i < arr.length; i++)
{
if (arr[i] === what) {
return true;
}
}
return false;
}
function get_next_token()
{
var n_newlines = 0;
var c = '';
do {
if (parser_pos >= input.length) {
return ['', 'TK_EOF'];
}
c = input.charAt(parser_pos);
parser_pos += 1;
if (c === "\n") {
n_newlines += 1;
}
}
while (in_array(c, whitespace));
if (n_newlines > 1) {
for (var i = 0; i < 2; i++) {
print_newline(i