utils.ts (2082B) - raw


      1 export function dumpObject(obj: any, indent = 0) {
      2     if (typeof obj !== "object") return console.log(obj);
      3     let prefix = ""
      4     for (let i = 0; i < indent; i++) {
      5         prefix += "    ";
      6     }
      7     for (let key of Object.keys(obj)) {
      8         try {
      9             console.log(prefix, key, typeof obj[key], obj[key]);
     10             if (key == "renderer") continue
     11             if (typeof obj[key] === "object" && indent < 10) dumpObject(obj[key], indent + 1);
     12         } catch (e) {}
     13     }
     14 }
     15 
     16 export function proxyProperty(module: any, functionName: string, handler: any) {
     17     if (!module || !module[functionName]) {
     18         console.warn("Function not found", functionName);
     19         return;
     20     }
     21     module[functionName] = new Proxy(module[functionName], {
     22         apply: (a, b, c) => handler(a, b, c),
     23         construct: (a, b, c) => handler(a, b, c)
     24     });
     25 }
     26 
     27 export function interceptComponent(moduleName: string, className: string, functions: any) {
     28     proxyProperty(require(moduleName), className, (target: any, args: any[], newTarget: any) => {
     29         let initProxy = functions["<init>"]
     30         let component: any;
     31 
     32         if (initProxy) {
     33             initProxy(args, (newArgs: any[]) => {
     34                 component = Reflect.construct(target, newArgs || args, newTarget);
     35             });
     36         } else {
     37             component = Reflect.construct(target, args, newTarget);
     38         }
     39 
     40         for (let funcName of Object.keys(functions)) {
     41             if (funcName == "<init>" || !component[funcName]) continue
     42             proxyProperty(component, funcName, (target: any, thisArg: any, argumentsList: any[]) => {
     43                 let result: any;
     44                 try {
     45                     functions[funcName](component, argumentsList, (newArgs: any[]) => {
     46                         result = Reflect.apply(target, thisArg, newArgs || argumentsList);
     47                     });
     48                 } catch (e) {
     49                     console.error("Error in", funcName, e);
     50                 }
     51                 return result;
     52             });
     53         }
     54 
     55         return component;
     56     })
     57 }