extract_function.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. if (!/^[$A-Z]+$/.test(varName)) {
  17. continue
  18. }
  19. let reg = "var (\$)?" + varName + "={(.|\\n)*?};"
  20. const varNameMatch = jsCode.match(new RegExp(reg), 'ig');
  21. if (varNameMatch && varNameMatch.length >= 1) {
  22. result += varNameMatch[0] + "\n";
  23. }
  24. existDependencies.push(varName);
  25. }
  26. }
  27. result += `\n${match[0]}`;
  28. console.log(result);
  29. return eval(result);
  30. }
  31. const fileContent = fs.readFileSync('base.js');
  32. const result = extractFunction(fileContent.toString(), /([a-zA-Z0-9]+)=function\([a-zA-Z0-9]+\)\{a=a\.split\(""\).*};/)
  33. console.log(result('adoidjaijdioawjiodjaaa'))
  34. const result2 = extractFunction(fileContent.toString(), /([a-zA-Z0-9]+)=function\([a-zA-Z0-9]+\)\{var b=a\.split\(""\)[\s\S]*?};/)
  35. console.log(result2("dwjaiodjaiowidwa"));