Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pnigos/hookjs
javascript function hook
https://github.com/pnigos/hookjs
Last synced: about 2 months ago
JSON representation
javascript function hook
- Host: GitHub
- URL: https://github.com/pnigos/hookjs
- Owner: pnigos
- Created: 2013-02-15T09:25:58.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2020-10-06T00:23:18.000Z (about 4 years ago)
- Last Synced: 2024-08-01T03:40:57.774Z (4 months ago)
- Language: JavaScript
- Size: 5.86 KB
- Stars: 107
- Watchers: 7
- Forks: 42
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- stars - pnigos/hookjs
README
hook.js
===
A hooks to any Javascript function.##Notice
```
[bool]hook:params{
realFunc[String|must]:用于保存原始函数的函数名称,用于unHook;
hookFunc[Function|must]:替换的hook函数;
context[Object|opt]:目标函数所在对象,用于hook非window对象下的函数,如String.protype.slice,carInstance1
methodName[String|opt]:匿名函数需显式传入目标函数名eg:this.Begin = function(){....};
}
[bool]unhook:params{
realFunc[String|must]:用于保存原始函数的函数名称,用于unHook;
funcName[String|must]:被Hook的函数名称
context[Object|opt]:目标函数所在对象,用于hook非window对象下的函数,如String.protype.slice,carInstance1
}
```
##Examples
```var myHook = new Hooks();
myHook.initEnv();//普通全局函数
var _alert = null;
function myalert(param){console.log("before hook");}
alert.hook("_alert",myalert);
alert.unhook("_alert","alert");
alert(1);//自定义对象匿名函数
function Person() {
this.getName = function(name) {
alert('Call' + name);
}
}
var p = new Person();
var _p_getName = null;
function mygetName(name){alert("Hooked");}
p.getName.hook("_p_getName",mygetName,p,"getName");
p.getName.unhook("_p_getName","getName",p);
p.getName("pnig0s");//原型对象函数
var _slice = null;
function myslice(param){alert("Hooked");}
String.prototype.slice.hook("_slice",myslice,String.prototype);
String.prototype.slice.unhook("_slice","slice",String.prototype);
var str = "pnig0s";
str.slice(1);myHooks.cleanEnv(); //clear hooks
```