https://github.com/gaoding-inc/runtime-hooks
π Utils for JS runtime hooking & injecting.
https://github.com/gaoding-inc/runtime-hooks
aop hof hook inject intercep javascript utils
Last synced: 9 months ago
JSON representation
π Utils for JS runtime hooking & injecting.
- Host: GitHub
- URL: https://github.com/gaoding-inc/runtime-hooks
- Owner: gaoding-inc
- Created: 2019-01-09T06:04:44.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-01-20T14:52:18.000Z (over 7 years ago)
- Last Synced: 2025-10-05T00:59:07.698Z (9 months ago)
- Topics: aop, hof, hook, inject, intercep, javascript, utils
- Language: JavaScript
- Homepage:
- Size: 5.86 KB
- Stars: 58
- Watchers: 7
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Runtime Hooks
π Utils for JS runtime hooking & injecting.
## What's This?
Adding hooks to any function you need!
[δΈζδ»η»](https://zhuanlan.zhihu.com/p/54460682)
## Usage
Install via NPM:
``` bash
npm install runtime-hooks
```
Basic usage:
``` js
import { withHookBefore } from 'runtime-hooks'
window.alert = withHookBefore(window.alert, console.log)
```
> These utils are mainly designed for advanced usage (logging / testing / debugging...), don't abusing them in business code.
### withHookBefore
`(originalFn: function, hookFn: function): function`
Given original function, return a new high-order function that:
1. Calls your customs hook function beforehand.
2. Calls the original function.
If your hook function returns `false`, the original function will not be executed. You can replace the reference to original function with the generated function, which makes sense for function hooking:
``` js
window.alert = withHookBefore(window.alert, console.log)
```
### withHookAfter
`(originalFn: function, hookFn: function): function`
Given original function, return a new high-order function that:
1. Calls the original function.
2. Calls your customs hook function afterwards.
### hookArgs
`(originalFn: function, argsGetter: function): function`
Given original function, runs a new high-order function that:
1. Calls `argsGetter` with args of original function.
2. If `argsGetter` returns an array, replace original args with it, or else keep the args.
3. Calls original function with these args, returning what it returns.
### hookOutput
`(originalFn: function, outputGetter: function): function`
Given original function, runs a new high-order function that:
1. Calls original function and get its output.
2. Calls `outputGetter` with output, returning what it returns.
> Both `hookOutput` and `withHookAfter` runs after original function. `hookOutput` replaces the output, but `withHookAfter` won't.
## License
MIT