Browse Source

add: extract

Ben 11 months ago
parent
commit
4eb58a9667
1 changed files with 33 additions and 0 deletions
  1. 33 0
      js/extract_function.js

+ 33 - 0
js/extract_function.js

@@ -0,0 +1,33 @@
+const fs = require("fs");
+
+extractFunction = (jsCode, regexp) => {
+    const match = jsCode.match(regexp)
+    if (!match && match.length <= 1) {
+        return null;
+    }
+    let result = "";
+    const dependencyMatchs = match[0].match(/([$a-zA-Z0-9]+\.[$a-zA-Z0-9]+)/g)
+    const existDependencies = [];
+    if (dependencyMatchs && dependencyMatchs.length >= 1) {
+        for (let currentMatch of dependencyMatchs) {
+            const varName = currentMatch.split('.')[0];
+            if (existDependencies.includes(varName)) {
+                continue
+            }
+            const varNameMatch = jsCode.match(new RegExp(`var \\${varName}={(.|\\n)*?};`), 'ig');
+            if (varNameMatch && varNameMatch.length >= 1) {
+                result += varNameMatch[0] + "\n";
+            }
+            existDependencies.push(varName);
+        }
+    }
+    result += `\n${match[0]}`;
+    console.log(result);
+    return eval(result);
+}
+
+const fileContent = fs.readFileSync('base.js');
+
+const result = extractFunction(fileContent.toString(), /([a-zA-Z0-9]+)=function\([a-zA-Z0-9]+\)\{a=a\.split\(""\).*};/)
+
+console.log(result('adoidjaijdioawjiodjaaa'))