self_example/Spider/Chapter11_JavaScript逆向/apply-ast/basic2.js

28 lines
824 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//@Author : dingjiawen
//@Date : 2024/03/25 15:26
//@Usage :
//@Desc :使用AST技术还原code1.js
import {traverse, types} from "@babel/core";
import {parse} from "@babel/parser";
import CodeGenerator from "@babel/generator";
// import * as types from "@babel/types";
import fs from "fs";
const code = fs.readFileSync("code/code2.js", "utf-8");
let ast = parse(code)
traverse(ast, {
"StringLiteral"({node}) {
//这个正则表达式用于匹配字符串中是否包含\u或\x的转义序列这两个序列分别用于Unicode和十六进制的字符转义,g表示全局,i表示不区分大小写
if (node.extra && /\\[ux]/gi.test(node.extra.raw)) {
node.extra.raw = node.extra.rawValue;
}
},
});
const {code: output} = CodeGenerator.default(ast);
console.log(output)