extract_function.js 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. const fs = require("fs");
  2. extractFunction = (jsCode, regexp) => {
  3. const match = jsCode.match(regexp)
  4. if (!match && match.length <= 1) {
  5. return null;
  6. }
  7. let result = "";
  8. const dependencyMatchs = match[0].match(/([$a-zA-Z0-9]+\.[$a-zA-Z0-9]+)/g)
  9. const existDependencies = [];
  10. if (dependencyMatchs && dependencyMatchs.length >= 1) {
  11. for (let currentMatch of dependencyMatchs) {
  12. const varName = currentMatch.split('.')[0];
  13. if (existDependencies.includes(varName)) {
  14. continue
  15. }
  16. const varNameMatch = jsCode.match(new RegExp(`var \\${varName}={(.|\\n)*?};`), 'ig');
  17. if (varNameMatch && varNameMatch.length >= 1) {
  18. result += varNameMatch[0] + "\n";
  19. }
  20. existDependencies.push(varName);
  21. }
  22. }
  23. result += `\n${match[0]}`;
  24. console.log(result);
  25. return eval(result);
  26. }
  27. const fileContent = fs.readFileSync('base.js');
  28. const result = extractFunction(fileContent.toString(), /([a-zA-Z0-9]+)=function\([a-zA-Z0-9]+\)\{a=a\.split\(""\).*};/)
  29. console.log(result('adoidjaijdioawjiodjaaa'))