lmath-api-docs/docs/plugin.md

51 lines
1.2 KiB
Markdown
Raw Normal View History

2021-04-08 00:11:48 +03:00
# Plugins
Here is a brief specification of the plugin module itself.
All loadable plugin modules need to have an `init` method.
## Backend
Backend modules get two initialization parameters: `logger` and `LMathPluginAPI`.
Feel free to use these in any way you want. Below you'll find a simple example.
```js
let logger, api;
module.exports = {
init: (_logger, _api) => {
logger = _logger;
api = _api;
logger.info('Hewwo! It is me, the new plugin!');
api.messages.on('testMessage', (d) => {
logger.info('Someone sent me a message! ' + d);
});
}
};
```
## Frontend
Frontend modules also get two parameters: `window` (the actual window object) and `LMathPluginAPI` (frontend).
jQuery and other useful things can be found like presented below.
```js
let window, api, $, jQuery;
module.exports = {
init: (_window, _api) => {
window = _window;
api = _api;
$ = window.jQuery;
jQuery = window.jQuery;
console.log('Hewwo wowld! This is the frontend side!');
setInterval(() => api.messages.send('testMessage', Date.now()), 2000);
}
}
```