@whitesev/utils
Version:
一个常用的工具库
88 lines (84 loc) • 2.33 kB
text/typescript
class Hooks {
/**
* 在Function原型上添加自定义方法.hook和.unhook
*/
initEnv() {
(Function.prototype as any).hook = function (
realFunc: any,
hookFunc: any,
context: Window & typeof globalThis
) {
let _context = null; //函数上下文
let _funcName = null; //函数名
_context = context || window;
_funcName = getFuncName(this);
(_context as any)["realFunc_" + _funcName] = this;
if (
(_context[_funcName] as any).prototype &&
(_context[_funcName] as any).prototype.isHooked
) {
console.log("Already has been hooked,unhook first");
return false;
}
function getFuncName(fn: any) {
// 获取函数名
let strFunc = fn.toString();
let _regex = /function\s+(\w+)\s*\(/;
let patten = strFunc.match(_regex);
if (patten) {
return patten[1];
}
return "";
}
try {
eval(
"_context[_funcName] = function " +
_funcName +
"(){\n" +
"let args = Array.prototype.slice.call(arguments,0);\n" +
"let obj = this;\n" +
"hookFunc.apply(obj,args);\n" +
"return _context['realFunc_" +
_funcName +
"'].apply(obj,args);\n" +
"};"
);
(_context as any)[_funcName].prototype.isHooked = true;
return true;
} catch (e) {
console.log("Hook failed,check the params.");
return false;
}
};
(Function.prototype as any).unhook = function (
realFunc: any,
funcName: any,
context: Window & typeof globalThis
) {
let _context = null;
let _funcName = null;
_context = context || window;
_funcName = funcName;
if (!(_context[_funcName] as any).prototype.isHooked) {
console.log("No function is hooked on");
return false;
}
(_context[_funcName] as any) = (_context as any)["realFunc" + _funcName];
Reflect.deleteProperty(_context, "realFunc_" + _funcName);
return true;
};
}
/**
* 删除在Function原型上添加的自定义方法.hook和.unhook
*/
cleanEnv() {
if (Function.prototype.hasOwnProperty("hook")) {
Reflect.deleteProperty(Function.prototype, "hook");
}
if (Function.prototype.hasOwnProperty("unhook")) {
Reflect.deleteProperty(Function.prototype, "unhook");
}
return true;
}
}
export { Hooks };