Updated Creating Plugins (markdown)

Zack 2018-10-14 19:45:22 -04:00
parent 090c5f42ab
commit d63d9e5450
1 changed files with 96 additions and 0 deletions

@ -128,6 +128,16 @@ Removes a style added with [`injectCSS`](#injectcssid-css) below.
***
#### `deleteData(pluginName, key)`
Deletes some saved data for plugin `pluginName` with key `key`.
|Parameter|Type|Description|
|-|-|:-|
|`pluginName`|string|Which plugin this is being used for.|
|`key`|string|Key for which data should be deleted.|
***
#### `findModule(filter)`
Searches for an internal Discord webpack module based on `filter`.
@ -224,6 +234,38 @@ Gets some saved data for plugin `pluginName` with key `key`. Data can be saved w
***
#### `monkeyPatch(module, methodName, options)`
This function monkey-patches a method on an object. The patching callback may be run before, after or instead of target method.
- Be careful when monkey-patching. Think not only about original functionality of target method and your changes, but also about developers of other plugins, who may also patch this method before or after you. Try to change target method behaviour as little as possible, and avoid changing method signatures.
- Display name of patched method is changed, so you can see if a function has been patched (and how many times) while debugging or in the stack trace. Also, patched methods have property `__monkeyPatched` set to `true`, in case you want to check something programmatically.
|Parameter|Type|Description|
|-|-|:-|
|`module`|object|Object to be patched. You can can also pass class prototypes to patch all class instances.|
|`methodName`|string|The name of the target message to be patched.|
|`options`|object|Options object. You should provide at least one of `before`, `after` or `instead` parameters. Other parameters are optional.|
|`[options.once=false]`|boolean|Set to `true` if you want to automatically unpatch method after first call.|
|`[options.silent=false]`|boolean|Set to `true` if you want to suppress log messages about patching and unpatching. Useful to avoid clogging the console in case of frequent conditional patching/unpatching, for example from another monkeyPatch callback.|
|`[options.displayName]`|string|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.|
|`options.before`|PatchFunction|Callback that will be called before original target method call. You can modify arguments here, so it will be passed to original method. Can be combined with `after`.|
|`options.after`|PatchFunction|Callback that will be called after original target method call. You can modify return value here, so it will be passed to external code which calls target method. Can be combined with `before`.|
|`options.instead`|PatchFunction|Callback that will be called instead of original target method call. You can get access to original method using `originalMethod` parameter if you want to call it, but you do not have to. Can't be combined with `before` and `after`.|
**@Returns** `{CancelPatch}` - A cancel function which allows you to undo the patch.
***
#### `onRemoved(node, callback)`
Adds a listener for when the node is removed from the document body.
|Parameter|Type|Description|
|-|-|:-|
|`node`|HTMLElement|Node to wait for.|
|`callback`|function|Function to be performed on event.|
***
#### `saveData(pluginName, key, data)`
Saved some `data` for plugin `pluginName` under `key` key. Gets saved in the plugins folder under `pluginName.config.json`. Data can be saved with [`loadData`](#loaddatapluginname-key).
@ -248,6 +290,29 @@ Shows a simple toast message similar to on Android. An example of the `success`
***
#### `suppressErrors(method, message)`
Wraps a function in a try catch block.
|Parameter|Type|Description|
|-|-|:-|
|`method`|function|Function to wrap.|
|`[message]`|string|Additional info for any errors.|
**@Returns** `{function}` - The wrapped version of the original function.
***
#### `testJSON(data)`
Determines if the input is valid and parseable JSON.
|Parameter|Type|Description|
|-|-|:-|
|`data`|string|Data to test.|
**@Returns** `{boolean}` - True if the data is valid, false otherwise.
***
#### `unlinkJS(id)`
Removes some previously linked JS by [`linkJS`](#linkjsid-url).
@ -257,6 +322,37 @@ Removes some previously linked JS by [`linkJS`](#linkjsid-url).
***
## Custom Types
#### Callback: CancelPatch
Function with no arguments and no return value that may be called to revert changes made by {@link monkeyPatch} method, restoring (unpatching) original method.
***
#### Callback: PatchFunction
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.
|Parameter|Type|Description|
|-|-|:-|
|`PatchData`|object|Data object with information about current call and original method that you may need in your patching callback.|
**@Returns** {*} Makes sense only when used as `instead` parameter in {@link monkeyPatch}. If something other than `undefined` is returned, the returned value replaces the value of `data.returnValue`. If used as `before` or `after` parameters, return value is ignored.
***
#### Interface: PatchData
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.
|Property|Type|Description|
|-|-|:-|
|`thisObject`|object|Original `this` value in current call of patched method.|
|`methodArguments`|Arguments|Original `arguments` object in current call of patched method. Please, never change function signatures, as it may cause a lot of problems in future.|
|`CancelPatch`|CancelPatch|Function with no arguments and no return value that may be called to reverse patching of current method. Calling this function prevents running of this callback on further original method calls.|
|`originalMethod`|function|Reference to the original method that is patched. You can use it if you need some special usage. You should explicitly provide a value for `this` and any method arguments when you call this function.|
|`callOriginalMethod`|function|This is a shortcut for calling original method using `this` and `arguments` from original call.|
|`returnValue`|*|This is a value returned from original function call. This property is available only in `after` callback or in `instead` callback after calling `callOriginalMethod` function.|
***
# Third-party libraries