GithubHelp home page GithubHelp logo

runtime-hooks's Introduction

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.

Both hookOutput and withHookAfter runs after original function. hookOutput replaces the output, but withHookAfter won't.

License

MIT

runtime-hooks's People

Contributors

doodlewind avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

runtime-hooks's Issues

hookAfter传递output

var output = originalFn.apply(this, arguments)
hookFn.apply(this, arguments)
// 这里可以把output传给hookFn,在插document.createElement这类API的时候,有时候要拿到最后的执行结果
return output

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.