๐ Utils for JS runtime hooking & injecting.
Adding hooks to any function you need!
Install via NPM:
npm install runtime-hooks
Basic usage:
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.
(originalFn: function, hookFn: function): function
Given original function, return a new high-order function that:
- Calls your customs hook function beforehand.
- 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:
window.alert = withHookBefore(window.alert, console.log)
(originalFn: function, hookFn: function): function
Given original function, return a new high-order function that:
- Calls the original function.
- Calls your customs hook function afterwards.
(originalFn: function, argsGetter: function): function
Given original function, runs a new high-order function that:
- Calls
argsGetter
with args of original function. - If
argsGetter
returns an array, replace original args with it, or else keep the args. - Calls original function with these args, returning what it returns.
(originalFn: function, outputGetter: function): function
Given original function, runs a new high-order function that:
- Calls original function and get its output.
- Calls
outputGetter
with output, returning what it returns.
Both
hookOutput
andwithHookAfter
runs after original function.hookOutput
replaces the output, butwithHookAfter
won't.
MIT