mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
49 lines
1.2 KiB
TypeScript
49 lines
1.2 KiB
TypeScript
"use strict";
|
|
|
|
import * as esprima from 'esprima';
|
|
import * as escodegen from 'escodegen';
|
|
|
|
import { IProgramNode } from './src/interfaces/nodes/IProgramNode';
|
|
|
|
import { Obfuscator } from './src/Obfuscator';
|
|
|
|
export class JavaScriptObfuscator {
|
|
/**
|
|
* @type any
|
|
*/
|
|
private static escodegenParams: any = {
|
|
format: {
|
|
compact: true
|
|
},
|
|
verbatim: 'x-verbatim-property'
|
|
};
|
|
|
|
/**
|
|
* @param sourceCode
|
|
* @param options
|
|
*/
|
|
public static obfuscate (sourceCode: string, options: any = {}): string {
|
|
let astTree: IProgramNode = esprima.parse(sourceCode),
|
|
obfuscator: Obfuscator = new Obfuscator(options);
|
|
|
|
obfuscator.obfuscateNode(astTree);
|
|
|
|
return JavaScriptObfuscator.generateCode(astTree, options);
|
|
}
|
|
|
|
/**
|
|
* @param astTree
|
|
* @param options
|
|
*/
|
|
private static generateCode (astTree: IProgramNode, options: any = {}): string {
|
|
let escodegenParams: any = Object.assign({}, JavaScriptObfuscator.escodegenParams);
|
|
|
|
if (options.hasOwnProperty('compact')) {
|
|
escodegenParams.format.compact = options.compact;
|
|
}
|
|
|
|
return escodegen.generate(astTree, escodegenParams);
|
|
}
|
|
}
|
|
|
|
module.exports = JavaScriptObfuscator; |