🔗 Utils for JS runtime hooking & injecting.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
.travis.yml
README.md
hooks.js
hooks.test.js
package.json

README.md

Runtime Hooks

🔗 Utils for JS runtime hooking & injecting.

Coverage Status

What's This?

Adding hooks to any function you need!

中文介绍

Usage

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.

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:

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.

License

MIT