Description

Patcher that can patch other functions allowing you to run code before, after or instead of the original function. Can also alter arguments and return values.

This is a modified version of what we have been working on in BDv2. https://github.com/JsSucks/BetterDiscordApp/blob/master/client/src/modules/patcher.js

Details

Methods


<static> getPatchesByCaller( name )

Description

Returns all the patches done by a specific caller

Parameters
Name Type Description
name string

Name of the patch caller

Details

<static> unpatchAll( patches )

Description

Unpatches all patches passed, or when a string is passed unpatches all patches done by that specific caller.

Parameters
Name Type Description
patches Array | string

Either an array of patches to unpatch or a caller name

Details

<static> before( caller, moduleToPatch, functionName, callback, options ) → {module:Patcher~unpatch}

Description

This method patches onto another function, allowing your code to run beforehand. Using this, you are also able to modify the incoming arguments before the original method is run.

Parameters
Name Type Description
caller string

Name of the caller of the patch function. Using this you can undo all patches with the same name using module:Patcher.unpatchAll. Use "" if you don't care.

moduleToPatch object

Object with the function to be patched. Can also patch an object's prototype.

functionName string

Name of the method to be patched

callback module:Patcher~patchCallback

Function to run before the original method

options object

Object used to pass additional options.

Name Type Attributes Default Description
displayName string <optional>

You can provide meaningful name for class/object provided in what param for logging purposes. By default, this function will try to determine name automatically.

forcePatch boolean <optional>
true

Set to true to patch even if the function doesnt exist. (Adds noop function in place).

Returns

Function with no arguments and no return value that should be called to cancel (unpatch) this patch. You should save and run it when your plugin is stopped.

Details

<static> after( caller, moduleToPatch, functionName, callback, options ) → {module:Patcher~unpatch}

Description

This method patches onto another function, allowing your code to run after. Using this, you are also able to modify the return value, using the return of your code instead.

Parameters
Name Type Description
caller string

Name of the caller of the patch function. Using this you can undo all patches with the same name using module:Patcher.unpatchAll. Use "" if you don't care.

moduleToPatch object

Object with the function to be patched. Can also patch an object's prototype.

functionName string

Name of the method to be patched

callback module:Patcher~patchCallback

Function to run instead of the original method

options object

Object used to pass additional options.

Name Type Attributes Default Description
displayName string <optional>

You can provide meaningful name for class/object provided in what param for logging purposes. By default, this function will try to determine name automatically.

forcePatch boolean <optional>
true

Set to true to patch even if the function doesnt exist. (Adds noop function in place).

Returns

Function with no arguments and no return value that should be called to cancel (unpatch) this patch. You should save and run it when your plugin is stopped.

Details

<static> instead( caller, moduleToPatch, functionName, callback, options ) → {module:Patcher~unpatch}

Description

This method patches onto another function, allowing your code to run instead. Using this, you are also able to modify the return value, using the return of your code instead.

Parameters
Name Type Description
caller string

Name of the caller of the patch function. Using this you can undo all patches with the same name using module:Patcher.unpatchAll. Use "" if you don't care.

moduleToPatch object

Object with the function to be patched. Can also patch an object's prototype.

functionName string

Name of the method to be patched

callback module:Patcher~patchCallback

Function to run after the original method

options object

Object used to pass additional options.

Name Type Attributes Default Description
displayName string <optional>

You can provide meaningful name for class/object provided in what param for logging purposes. By default, this function will try to determine name automatically.

forcePatch boolean <optional>
true

Set to true to patch even if the function doesnt exist. (Adds noop function in place).

Returns

Function with no arguments and no return value that should be called to cancel (unpatch) this patch. You should save and run it when your plugin is stopped.

Details

<static> pushChildPatch( caller, moduleToPatch, functionName, callback, options ) → {module:Patcher~unpatch}

Description

This method patches onto another function, allowing your code to run before, instead or after the original function. Using this you are able to modify the incoming arguments before the original function is run as well as the return value before the original function actually returns.

Parameters
Name Type Description
caller string

Name of the caller of the patch function. Using this you can undo all patches with the same name using module:Patcher.unpatchAll. Use "" if you don't care.

moduleToPatch object

Object with the function to be patched. Can also patch an object's prototype.

functionName string

Name of the method to be patched

callback module:Patcher~patchCallback

Function to run after the original method

options object

Object used to pass additional options.

Name Type Attributes Default Description
type string <optional>
after

Determines whether to run the function before, instead, or after the original.

displayName string <optional>

You can provide meaningful name for class/object provided in what param for logging purposes. By default, this function will try to determine name automatically.

forcePatch boolean <optional>
true

Set to true to patch even if the function doesnt exist. (Adds noop function in place).

Returns

Function with no arguments and no return value that should be called to cancel (unpatch) this patch. You should save and run it when your plugin is stopped.

Details

Type Definitions


unpatch()

Description

Function with no arguments and no return value that may be called to revert changes made by module:Patcher, restoring (unpatching) original method.

Details
function

patchCallback( thisObject, arguments, extraValue ) → {*}

Description

A callback that modifies method logic. This callback is called on each call of the original method and is provided all data about original call. Any of the data can be modified if necessary, but do so wisely.

The third argument for the callback will be undefined for before patches. originalFunction for instead patches and returnValue for after patches.

Parameters
Name Type Description
thisObject object

this in the context of the original function.

arguments arguments

The original arguments of the original function.

extraValue function | *

For instead patches, this is the original function from the module. For after patches, this is the return value of the function.

Returns

Makes sense only when using an instead or after patch. If something other than undefined is returned, the returned value replaces the value of returnValue. If used for before the return value is ignored.

Details
function