Skip to content

Instantly share code, notes, and snippets.

@qb20nh
Forked from tilmanschweitzer/intercept-function.js
Last active December 11, 2021 12:39
Show Gist options
  • Save qb20nh/50f15c14e3b90c666adead1d55c41d32 to your computer and use it in GitHub Desktop.
Save qb20nh/50f15c14e3b90c666adead1d55c41d32 to your computer and use it in GitHub Desktop.
Function to intercept functions calls even to nativ functions.
const interceptFunction = (object, fnName, options) => {
const noop = () => {}
const fnToWrap = object[fnName]
const before = options.before || noop
const after = options.after || noop
object[fnName] = function (...args) {
before.apply(this, args)
const result = fnToWrap.apply(this, args)
after.apply(this, args, result)
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment