JS 压缩/混淆/美化/加密 源码分享

如果想自己写的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 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值