2016-11-02 15:31:58 +01:00
|
|
|
.. raw:: html
|
|
|
|
|
|
|
|
<div id="banner"><a href="https://github.com/jcbrand/converse.js/blob/master/docs/source/theming.rst">Edit me on GitHub</a></div>
|
|
|
|
|
2016-12-20 10:31:11 +01:00
|
|
|
.. _`writing-a-plugin`:
|
|
|
|
|
2017-03-08 12:15:19 +01:00
|
|
|
Writing a plugin
|
|
|
|
================
|
2016-10-21 16:33:47 +02:00
|
|
|
|
2016-11-02 15:31:58 +01:00
|
|
|
Introduction
|
|
|
|
------------
|
|
|
|
|
2019-05-30 13:58:17 +02:00
|
|
|
Converse exposes a plugin architecture through which developers can modify
|
2017-09-03 22:10:45 +02:00
|
|
|
and extend its functionality.
|
|
|
|
|
2018-01-05 17:57:28 +01:00
|
|
|
Using plugins is good engineering practice, and using them is the *only* recommended
|
2019-05-30 13:58:17 +02:00
|
|
|
way of changing Converse or adding new features to it.
|
2017-09-03 22:10:45 +02:00
|
|
|
|
2018-01-05 17:57:28 +01:00
|
|
|
In particular, plugins have the following advantages:
|
2016-10-21 16:33:47 +02:00
|
|
|
|
2018-01-05 17:57:28 +01:00
|
|
|
The main benefit of plugins is their *isolation of concerns* (and features).
|
|
|
|
From this benefit flows various 2nd degree advantages, such as the ability to
|
|
|
|
make smaller production builds (by excluding unused plugins) and an easier
|
2019-05-30 13:58:17 +02:00
|
|
|
upgrade path by avoiding touching Converse's internals.
|
2018-01-05 17:57:28 +01:00
|
|
|
|
2019-05-30 13:58:17 +02:00
|
|
|
Each plugin comes in its own file, and Converse's plugin architecture,
|
2018-01-05 17:57:28 +01:00
|
|
|
called `pluggable.js <https://github.com/jcbrand/pluggable.js/>`_, provides you
|
|
|
|
with the ability to "hook in" to the core code and other plugins.
|
|
|
|
|
2019-05-30 13:58:17 +02:00
|
|
|
Converse itself is composed out of plugins and uses pluggable.js. Take a look at the
|
2018-01-05 17:57:28 +01:00
|
|
|
`src <https://github.com/jcbrand/converse.js/tree/master/src>`_ directory. All
|
2018-01-06 22:32:57 +01:00
|
|
|
the files that follow the pattern `converse-*.js` are plugins.
|
2018-01-05 17:57:28 +01:00
|
|
|
|
|
|
|
Plugins (by way of Pluggable.js) enable developers to extend and override existing objects,
|
|
|
|
functions and the `Backbone <http://backbonejs.org/>`_ models and views that make up
|
2019-05-30 13:58:17 +02:00
|
|
|
Converse.
|
2018-01-05 17:57:28 +01:00
|
|
|
|
|
|
|
Besides that, in plugins you can also write new Backbone (or other) models and views,
|
2018-01-06 22:32:57 +01:00
|
|
|
in order to add a new functionality.
|
2016-10-21 16:33:47 +02:00
|
|
|
|
2017-09-03 22:10:45 +02:00
|
|
|
To more deeply understand how this plugin architecture works, please read the
|
2016-10-21 16:33:47 +02:00
|
|
|
`pluggable.js documentation <https://jcbrand.github.io/pluggable.js/>`_
|
2018-01-06 22:32:57 +01:00
|
|
|
and to understand its inner workings, please refer to the `annotated source code
|
2016-10-21 16:33:47 +02:00
|
|
|
<https://jcbrand.github.io/pluggable.js/docs/pluggable.html>`_.
|
|
|
|
|
2017-09-04 09:24:38 +02:00
|
|
|
.. note:: **Trying out a plugin in JSFiddle**
|
2017-09-03 22:10:45 +02:00
|
|
|
|
2019-05-30 13:58:17 +02:00
|
|
|
Because Converse consists only of JavaScript, HTML and CSS (with no backend
|
2017-09-04 09:24:38 +02:00
|
|
|
code required like PHP, Python or Ruby) it runs fine in JSFiddle.
|
2017-03-09 15:01:31 +01:00
|
|
|
|
2019-05-30 13:58:17 +02:00
|
|
|
Here's a Fiddle with a Converse plugin that calls ``alert`` once it gets
|
2017-09-04 09:24:38 +02:00
|
|
|
initialized and also when a chat message gets rendered: https://jsfiddle.net/4drfaok0/15/
|
2017-03-09 15:01:31 +01:00
|
|
|
|
|
|
|
|
2017-09-04 09:24:38 +02:00
|
|
|
.. note:: **Generating a plugin with Yeoman**
|
2017-09-04 07:51:02 +02:00
|
|
|
|
2019-05-30 13:58:17 +02:00
|
|
|
The rest of this document explains how to write a plugin for Converse and
|
2017-09-04 09:24:38 +02:00
|
|
|
ends with a documented example of a plugin.
|
2017-09-04 07:51:02 +02:00
|
|
|
|
2017-09-04 09:24:38 +02:00
|
|
|
There is a `Yeoman <http://yeoman.io/>`_ code generator, called
|
|
|
|
`generator-conversejs <https://github.com/jcbrand/generator-conversejs>`_, which
|
2018-01-06 22:32:57 +01:00
|
|
|
you can use to generate plugin scaffolding/boilerplate that serves as a
|
2017-09-04 09:24:38 +02:00
|
|
|
starting point and basis for writing your plugin.
|
2017-09-04 07:51:02 +02:00
|
|
|
|
2017-09-04 09:24:38 +02:00
|
|
|
Please refer to the `generator-conversejs <https://github.com/jcbrand/generator-conversejs>`_
|
|
|
|
README for information on how to use it.
|
2017-09-04 07:51:02 +02:00
|
|
|
|
2017-03-09 15:01:31 +01:00
|
|
|
Registering a plugin
|
|
|
|
--------------------
|
|
|
|
|
2017-09-04 07:51:02 +02:00
|
|
|
Plugins need to be registered (and whitelisted) before they can be loaded and
|
|
|
|
initialized.
|
|
|
|
|
2019-05-30 13:58:17 +02:00
|
|
|
You register a Converse plugin by calling ``converse.plugins.add``.
|
2017-09-04 07:51:02 +02:00
|
|
|
|
|
|
|
The plugin itself is a JavaScript object which usually has at least an
|
|
|
|
``initialize`` method, which gets called at the end of the
|
|
|
|
``converse.initialize`` method which is the top-level method that gets called
|
2019-05-30 13:58:17 +02:00
|
|
|
by the website to configure and initialize Converse itself.
|
2017-09-04 07:51:02 +02:00
|
|
|
|
|
|
|
Here's an example code snippet:
|
2016-10-21 16:33:47 +02:00
|
|
|
|
|
|
|
.. code-block:: javascript
|
|
|
|
|
|
|
|
converse.plugins.add('myplugin', {
|
2016-12-22 14:00:03 +01:00
|
|
|
|
|
|
|
initialize: function () {
|
|
|
|
// This method gets called once converse.initialize has been called
|
|
|
|
// and the plugin itself has been loaded.
|
|
|
|
|
|
|
|
// Inside this method, you have access to the closured
|
|
|
|
// _converse object as an attribute on "this".
|
|
|
|
// E.g. this._converse
|
|
|
|
},
|
2016-10-21 16:33:47 +02:00
|
|
|
});
|
|
|
|
|
2017-03-09 15:01:31 +01:00
|
|
|
.. note:: It's important that `converse.plugins.add` is called **before**
|
|
|
|
`converse.initialize` is called. Otherwise the plugin will never get
|
|
|
|
registered and never get called.
|
|
|
|
|
|
|
|
Whitelisting of plugins
|
|
|
|
-----------------------
|
|
|
|
|
2019-05-30 13:58:17 +02:00
|
|
|
As of Converse 3.0.0 and higher, plugins need to be whitelisted before they
|
2017-03-09 15:01:31 +01:00
|
|
|
can be used. This is because plugins have access to a powerful API. For
|
|
|
|
example, they can read all messages and send messages on the user's behalf.
|
|
|
|
|
|
|
|
To avoid malicious plugins being registered (i.e. by malware infected
|
|
|
|
advertising networks) we now require whitelisting.
|
|
|
|
|
|
|
|
To whitelist a plugin simply means to specify :ref:`whitelisted_plugins` when
|
|
|
|
you call ``converse.initialize``.
|
|
|
|
|
2018-01-05 17:57:28 +01:00
|
|
|
If you're adding a "core" plugin, which means a plugin that will be
|
2019-05-30 13:58:17 +02:00
|
|
|
included in the default, open-source version of Converse, then you'll
|
2018-01-05 17:57:28 +01:00
|
|
|
instead whitelist the plugin by adding its name to the `core_plugins` array in
|
2019-04-17 21:02:00 +02:00
|
|
|
`./src/headless/converse-core.js <https://github.com/jcbrand/converse.js/blob/master/src/headless/converse-core.js>`_.
|
2019-04-18 16:16:55 +02:00
|
|
|
or the `WHITELISTED_PLUGINS` array in `./src/converse.js <https://github.com/jcbrand/converse.js/blob/master/src/converse.js>`_.
|
|
|
|
|
|
|
|
Where you add it depends on whether your plugin is part of the headless build
|
|
|
|
(which means it doesn't contain any view code) or not.
|
2018-01-05 17:57:28 +01:00
|
|
|
|
2016-10-21 16:33:47 +02:00
|
|
|
Security and access to the inner workings
|
|
|
|
-----------------------------------------
|
|
|
|
|
|
|
|
The globally available ``converse`` object, which exposes the API methods, such
|
|
|
|
as ``initialize`` and ``plugins.add``, is a wrapper that encloses and protects
|
2016-12-20 10:31:11 +01:00
|
|
|
a sensitive inner object, named ``_converse`` (not the underscore prefix).
|
2016-10-21 16:33:47 +02:00
|
|
|
|
2016-12-20 10:31:11 +01:00
|
|
|
This inner ``_converse`` object contains all the Backbone models and views,
|
|
|
|
as well as various other attributes and functions.
|
2016-10-21 16:33:47 +02:00
|
|
|
|
|
|
|
Within a plugin, you will have access to this internal
|
|
|
|
`"closured" <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures>`_
|
2016-12-20 10:31:11 +01:00
|
|
|
``_converse`` object, which is normally not exposed in the global variable scope.
|
|
|
|
|
2016-12-22 14:00:03 +01:00
|
|
|
The inner ``_converse`` object is made private in order to safely hide and
|
2016-12-20 10:31:11 +01:00
|
|
|
encapsulate sensitive information and methods which should not be exposed
|
|
|
|
to any 3rd-party scripts that might be running in the same page.
|
2016-10-21 16:33:47 +02:00
|
|
|
|
2017-03-08 12:15:19 +01:00
|
|
|
|
|
|
|
Accessing 3rd party libraries
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Immediately inside the module shown above you can access 3rd party libraries (such
|
2019-05-06 11:16:56 +02:00
|
|
|
dayjs and lodash) via the ``converse.env`` map.
|
2016-12-22 14:00:03 +01:00
|
|
|
|
2019-05-30 13:58:17 +02:00
|
|
|
The code for it could look something like this:
|
2017-03-08 12:15:19 +01:00
|
|
|
|
|
|
|
.. code-block:: javascript
|
|
|
|
|
|
|
|
// Commonly used utilities and variables can be found under the "env"
|
|
|
|
// namespace of the "converse" global.
|
2019-05-30 13:58:17 +02:00
|
|
|
const { Backbone, Promise, Strophe, dayjs, sizzle, _, $build, $iq, $msg, $pres } = converse.env;
|
2017-03-08 12:15:19 +01:00
|
|
|
|
|
|
|
These dependencies are closured so that they don't pollute the global
|
|
|
|
namespace, that's why you need to access them in such a way inside the module.
|
|
|
|
|
|
|
|
Overrides
|
|
|
|
---------
|
|
|
|
|
2019-05-30 13:58:17 +02:00
|
|
|
Plugins can override core code or code from other plugins. You can specify
|
|
|
|
overrides in the object passed to ``converse.plugins.add``.
|
|
|
|
|
|
|
|
In an override you can still call the overridden function, by calling
|
|
|
|
``this.__super__.methodName.apply(this, arguments);`` where ``methodName`` is
|
|
|
|
the name of the function or method you're overriding.
|
|
|
|
|
|
|
|
The following code snippet provides an example of two different overrides:
|
|
|
|
|
|
|
|
.. code-block:: javascript
|
|
|
|
|
|
|
|
overrides: {
|
|
|
|
/* The *_converse* object has a method "onConnected".
|
|
|
|
* You can override that method as follows:
|
|
|
|
*/
|
|
|
|
onConnected: function () {
|
|
|
|
// Overrides the onConnected method in Converse
|
|
|
|
|
|
|
|
// Top-level functions in "overrides" are bound to the
|
|
|
|
// inner "_converse" object.
|
|
|
|
const _converse = this;
|
2016-12-22 14:00:03 +01:00
|
|
|
|
2019-05-30 13:58:17 +02:00
|
|
|
// Your custom code can come here ...
|
|
|
|
|
|
|
|
// You can access the original function being overridden
|
|
|
|
// via the __super__ attribute.
|
|
|
|
// Make sure to pass on the arguments supplied to this
|
|
|
|
// function and also to apply the proper "this" object.
|
|
|
|
_converse.__super__.onConnected.apply(this, arguments);
|
|
|
|
|
|
|
|
// Your custom code can come here ...
|
|
|
|
},
|
|
|
|
|
|
|
|
/* On the XMPPStatus Backbone model is a method sendPresence.
|
|
|
|
* We can override is as follows:
|
|
|
|
*/
|
|
|
|
XMPPStatus: {
|
|
|
|
sendPresence: function (type, status_message, jid) {
|
|
|
|
// The "_converse" object is available via the __super__
|
|
|
|
// attribute.
|
|
|
|
const _converse = this.__super__._converse;
|
|
|
|
|
|
|
|
// Custom code can come here ...
|
|
|
|
|
|
|
|
// You can call the original overridden method, by
|
|
|
|
// accessing it via the __super__ attribute.
|
|
|
|
// When calling it, you need to apply the proper
|
|
|
|
// context as reference by the "this" variable.
|
|
|
|
this.__super__.sendPresence.apply(this, arguments);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Use the ``overrides`` feature with caution. It basically resorts to
|
2016-12-22 14:00:03 +01:00
|
|
|
monkey patching which pollutes the call stack and can make your code fragile
|
2019-05-30 13:58:17 +02:00
|
|
|
and prone to bugs when Converse gets updated. Too much use of ``overrides``
|
2016-12-22 14:00:03 +01:00
|
|
|
is therefore a "code smell" which should ideally be avoided.
|
|
|
|
|
2019-05-30 13:58:17 +02:00
|
|
|
A better approach is to listen to the events emitted by Converse, and to add
|
2016-12-22 14:00:03 +01:00
|
|
|
your code in event handlers. This is however not always possible, in which case
|
|
|
|
the overrides are a powerful tool.
|
|
|
|
|
2019-05-30 13:58:17 +02:00
|
|
|
Also, while it's possible to add new methods to classes via the ``overrides``
|
|
|
|
feature, it's better and more explicit to use composition with
|
|
|
|
``Object.assign``.
|
|
|
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: javascript
|
|
|
|
|
|
|
|
function doSomething () {
|
|
|
|
// Your code comes here
|
|
|
|
}
|
|
|
|
Object.assign(_converse.ChatBoxView.prototype, { doSomething });
|
|
|
|
|
|
|
|
|
|
|
|
Overriding a template
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Converse uses various templates, loaded with lodash, to generate its HTML.
|
|
|
|
|
|
|
|
It's not possible to override a template with the plugin's ``overrides``
|
|
|
|
feature, instead you should configure a new path to your own template via your
|
|
|
|
module bundler.
|
|
|
|
|
|
|
|
For example, with Webpack (which Converse uses internall), you can specify an
|
|
|
|
``alias`` for the template you want to override. This alias then points to your
|
|
|
|
own custom template.
|
|
|
|
|
|
|
|
For example, in your webpack config file, you could add the following to the
|
|
|
|
``config`` object that gets exported:
|
|
|
|
|
|
|
|
.. code-block:: javascript
|
|
|
|
|
2019-05-30 16:48:20 +02:00
|
|
|
module: {
|
|
|
|
{
|
|
|
|
test: /templates\/.*\.(html|svg)$/,
|
|
|
|
use: [{
|
|
|
|
loader: 'lodash-template-webpack-loader',
|
|
|
|
options: {
|
|
|
|
escape: /\{\{\{([\s\S]+?)\}\}\}/g,
|
|
|
|
evaluate: /\{\[([\s\S]+?)\]\}/g,
|
|
|
|
interpolate: /\{\{([\s\S]+?)\}\}/g,
|
|
|
|
// By default, template places the values from your data in the
|
|
|
|
// local scope via the with statement. However, you can specify
|
|
|
|
// a single variable name with the variable setting. This can
|
|
|
|
// significantly improve the speed at which a template is able
|
|
|
|
// to render.
|
|
|
|
variable: 'o',
|
|
|
|
prependFilenameComment: __dirname
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
},
|
2019-05-30 13:58:17 +02:00
|
|
|
resolve: {
|
|
|
|
extensions: ['.js'],
|
|
|
|
modules: [
|
|
|
|
path.join(__dirname, 'node_modules'),
|
|
|
|
path.join(__dirname, 'node_modules/converse.js/src')
|
|
|
|
],
|
|
|
|
alias: {
|
|
|
|
'templates/profile_view.html$': path.resolve(__dirname, 'templates/profile_view.html')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-30 16:48:20 +02:00
|
|
|
You'll need to install ``lodash-template-webpack-loader``.
|
|
|
|
|
|
|
|
Currently Converse uses a fork of `lodash-template-webpack-loader <https://github.com/jcbrand/lodash-template-webpack-loader>`_.
|
|
|
|
|
|
|
|
To install it, you can add ``"lodash-template-webpack-loader": "jcbrand/lodash-template-webpack-loader"``
|
|
|
|
to your package.json's ``devDependencies``.
|
|
|
|
|
|
|
|
|
2018-01-10 13:59:45 +01:00
|
|
|
.. _`dependencies`:
|
2017-08-09 15:50:24 +02:00
|
|
|
|
2018-01-10 13:59:45 +01:00
|
|
|
Plugin dependencies
|
2019-05-30 13:58:17 +02:00
|
|
|
-------------------
|
2017-03-08 12:15:19 +01:00
|
|
|
|
|
|
|
When using ``overrides``, the code that you want to override (which is either
|
2018-01-10 13:59:45 +01:00
|
|
|
in ``converse-core`` or in other plugins), needs to be parsed already by the
|
|
|
|
time your ``overrides`` are being parsed.
|
2017-03-08 12:15:19 +01:00
|
|
|
|
2018-01-10 13:59:45 +01:00
|
|
|
Additionally, when you register event or promise handlers in your plugin for
|
|
|
|
events/promises that fire in other plugins, then you want those plugins to have
|
|
|
|
been loaded before your plugin gets loaded.
|
2017-03-08 12:15:19 +01:00
|
|
|
|
2018-01-10 13:59:45 +01:00
|
|
|
To resolve this problem we have the ``dependencies`` Array attribute.
|
|
|
|
With this you can specify those dependencies which need to be loaded before
|
|
|
|
your plugin is loaded.
|
|
|
|
|
|
|
|
In some cases, you might want to depend on another plugin if it's available,
|
|
|
|
but don't care when it's not available.
|
|
|
|
An example is the `converse-dragresize <https://github.com/jcbrand/converse.js/blob/master/src/converse-dragresize.js>`_
|
2017-03-08 12:15:19 +01:00
|
|
|
plugin, which will add drag-resize handles to the headlines box (which shows
|
2018-01-10 13:59:45 +01:00
|
|
|
messages of type ``headline``) but doesn't care when that particular plugin is
|
|
|
|
not available.
|
|
|
|
|
|
|
|
If the :ref:`strict_plugin_dependencies` setting is set to ``false`` (which is
|
|
|
|
its default value), then no error will be raised if the plugin is not found.
|
2017-03-08 12:15:19 +01:00
|
|
|
|
|
|
|
In this case, you can't specify the plugin as a dependency in the ``define``
|
|
|
|
statement at the top of the plugin, since it might not always be available,
|
|
|
|
which would cause ``require.js`` to throw an error.
|
|
|
|
|
2019-05-30 13:58:17 +02:00
|
|
|
Extending Converse's configuration settings
|
|
|
|
----------------------------------------------
|
2017-07-05 11:03:13 +02:00
|
|
|
|
2019-05-30 13:58:17 +02:00
|
|
|
Converse comes with various :ref:`configuration-settings` that can be used to
|
2017-07-05 11:03:13 +02:00
|
|
|
modify its functionality and behavior.
|
|
|
|
|
|
|
|
All configuration settings have default values which can be overridden when
|
2019-06-05 10:10:49 +02:00
|
|
|
`converse.initialize` (see `converse.initialize </docs/html/api/converse.html#.initialize>`_)
|
|
|
|
gets called.
|
2017-07-05 11:03:13 +02:00
|
|
|
|
|
|
|
Plugins often need their own additional configuration settings and you can add
|
2019-06-05 10:10:49 +02:00
|
|
|
these settings with the `_converse.api.settings.update </docs/html/api/-_converse.api.settings.html#.update>`_
|
|
|
|
method.
|
2017-07-05 11:03:13 +02:00
|
|
|
|
|
|
|
Exposing promises
|
2019-05-30 13:58:17 +02:00
|
|
|
-----------------
|
2017-07-05 11:03:13 +02:00
|
|
|
|
2019-06-05 10:10:49 +02:00
|
|
|
Converse has a `waitUntil </docs/html/api/-_converse.api.html#.waitUntil>`_ API method
|
2017-07-05 11:03:13 +02:00
|
|
|
which allows you to wait for various promises to resolve before executing a
|
|
|
|
piece of code.
|
|
|
|
|
|
|
|
You can add new promises for your plugin by calling
|
2019-06-05 10:10:49 +02:00
|
|
|
`_converse.api.promises.add </docs/html/api/-_converse.api.promises.html#.add>`_.
|
2017-07-05 11:03:13 +02:00
|
|
|
|
|
|
|
Generally, your plugin will then also be responsible for making sure these
|
2019-06-05 10:10:49 +02:00
|
|
|
promises are resolved. You do this by calling
|
|
|
|
`_converse.api.trigger </docs/html/api/-_converse.api.html#.trigger>`_, which not
|
2017-07-05 11:03:13 +02:00
|
|
|
only resolves the plugin but will also emit an event with the same name.
|
2017-03-08 12:15:19 +01:00
|
|
|
|
2017-09-04 09:54:18 +02:00
|
|
|
Dealing with asynchronicity
|
|
|
|
---------------------------
|
|
|
|
|
2019-05-30 13:58:17 +02:00
|
|
|
Due to the asynchronous nature of XMPP, many subroutines in Converse execute
|
2017-09-04 09:54:18 +02:00
|
|
|
at different times and not necessarily in the same order.
|
|
|
|
|
|
|
|
In many cases, when you want to execute a piece of code in a plugin, you first
|
|
|
|
want to make sure that the supporting data-structures that your code might rely
|
|
|
|
on have been created and populated with data.
|
|
|
|
|
|
|
|
There are two ways of waiting for the right time before executing your code.
|
|
|
|
You can either listen for certain events, or you can wait for promises to
|
|
|
|
resolve.
|
|
|
|
|
2017-10-23 14:15:57 +02:00
|
|
|
For example, when you want to query the message archive between you and a
|
|
|
|
friend, you would call ``this._converse.api.archive.query({'with': 'friend@example.org'});``
|
|
|
|
|
|
|
|
However, simply calling this immediately in the ``initialize`` method of your plugin will
|
|
|
|
not work, since the user is not logged in yet.
|
|
|
|
|
|
|
|
In this case, you should first listen for the ``connection`` event, and then do your query, like so:
|
|
|
|
|
|
|
|
.. code-block:: javascript
|
|
|
|
|
|
|
|
converse.plugins.add('myplugin', {
|
|
|
|
initialize: function () {
|
2019-05-24 13:52:15 +02:00
|
|
|
const _converse = this._converse;
|
2017-10-23 14:15:57 +02:00
|
|
|
|
2019-03-29 14:15:03 +01:00
|
|
|
_converse.api.listen.on('connected', function () {
|
2017-10-23 14:15:57 +02:00
|
|
|
_converse.api.archive.query({'with': 'admin2@localhost'});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Another example is in the ``Bookmarks`` plugin (in
|
|
|
|
`src/converse-bookmarks.js <https://github.com/jcbrand/converse.js/blob/6c3aa34c23d97d679823a64376418cd0f40a8b94/src/converse-bookmarks.js#L528>`_).
|
|
|
|
Before bookmarks can be fetched and shown to the user, we first have to wait until
|
2017-09-04 09:54:18 +02:00
|
|
|
the `"Rooms"` panel of the ``ControlBox`` has been rendered and inserted into
|
|
|
|
the DOM. Otherwise we have no place to show the bookmarks yet.
|
|
|
|
|
|
|
|
Therefore, there are the following lines of code in the ``initialize`` method of
|
|
|
|
`converse-bookmarks.js <https://github.com/jcbrand/converse.js/blob/6c3aa34c23d97d679823a64376418cd0f40a8b94/src/converse-bookmarks.js#L528>`_:
|
|
|
|
|
|
|
|
.. code-block:: javascript
|
|
|
|
|
|
|
|
Promise.all([
|
|
|
|
_converse.api.waitUntil('chatBoxesFetched'),
|
|
|
|
_converse.api.waitUntil('roomsPanelRendered')
|
|
|
|
]).then(initBookmarks);
|
|
|
|
|
|
|
|
What this means, is that the plugin will wait until the ``chatBoxesFetched``
|
|
|
|
and ``roomsPanelRendered`` promises have been resolved before it calls the
|
|
|
|
``initBookmarks`` method (which is defined inside the plugin).
|
|
|
|
|
|
|
|
This way, we know that we have everything in place and set up correctly before
|
|
|
|
fetching the bookmarks.
|
|
|
|
|
2017-10-23 14:15:57 +02:00
|
|
|
As yet another example, there is also the following code in the ``initialize``
|
2017-09-04 09:54:18 +02:00
|
|
|
method of the plugin:
|
|
|
|
|
|
|
|
.. code-block:: javascript
|
|
|
|
|
2019-03-29 14:15:03 +01:00
|
|
|
_converse.api.listen.on('chatBoxOpened', function renderMinimizeButton (view) {
|
2017-09-04 09:54:18 +02:00
|
|
|
// Inserts a "minimize" button in the chatview's header
|
|
|
|
|
|
|
|
// Implementation code removed for brevity
|
|
|
|
// ...
|
|
|
|
});
|
|
|
|
|
|
|
|
In this case, the plugin waits for the ``chatBoxOpened`` event, before it then
|
2018-02-09 11:55:53 +01:00
|
|
|
calls ``renderMinimizeButton``, which adds a new button to the chatbox (which
|
2017-09-04 09:54:18 +02:00
|
|
|
enables you to minimize it).
|
|
|
|
|
|
|
|
Finding the right promises and/or events to listen to, can be a bit
|
|
|
|
challenging, and sometimes it might be necessary to create new events or
|
|
|
|
promises.
|
|
|
|
|
2019-06-05 10:10:49 +02:00
|
|
|
Please refer to the `API documentation </docs/html/api/http://localhost:8008/docs/html/api/>`_
|
|
|
|
for an overview of what's available to you. If you need new events or promises, then
|
2017-09-04 09:54:18 +02:00
|
|
|
`please open an issue or make a pull request on Github <https://github.com/jcbrand/converse.js>`_
|
|
|
|
|
2017-03-08 12:15:19 +01:00
|
|
|
A full example plugin
|
|
|
|
---------------------
|
|
|
|
|
2017-09-04 09:54:18 +02:00
|
|
|
Below follows a documented example of a plugin. This is the same code that gets
|
|
|
|
generated by `generator-conversejs <https://github.com/jcbrand/generator-conversejs>`_.
|
|
|
|
|
2016-10-21 16:33:47 +02:00
|
|
|
.. code-block:: javascript
|
|
|
|
|
2019-05-24 13:52:15 +02:00
|
|
|
import converse from "@converse/headless/converse-core";
|
|
|
|
|
|
|
|
// Commonly used utilities and variables can be found under the "env"
|
|
|
|
// namespace of the "converse" global.
|
2019-05-30 13:58:17 +02:00
|
|
|
const { Backbone, Promise, Strophe, dayjs, sizzle, _, $build, $iq, $msg, $pres } = converse.env;
|
2019-05-24 13:52:15 +02:00
|
|
|
|
|
|
|
// The following line registers your plugin.
|
|
|
|
converse.plugins.add("myplugin", {
|
|
|
|
|
|
|
|
/* Dependencies are other plugins which might be
|
|
|
|
* overridden or relied upon, and therefore need to be loaded before
|
|
|
|
* this plugin. They are "optional" because they might not be
|
|
|
|
* available, in which case any overrides applicable to them will be
|
|
|
|
* ignored.
|
|
|
|
*
|
|
|
|
* NB: These plugins need to have already been imported or loaded,
|
|
|
|
* either in your plugin or somewhere else.
|
|
|
|
*
|
|
|
|
* It's possible to make these dependencies "non-optional".
|
|
|
|
* If the setting "strict_plugin_dependencies" is set to true,
|
|
|
|
* an error will be raised if the plugin is not found.
|
|
|
|
*/
|
2019-05-30 13:58:17 +02:00
|
|
|
dependencies: [],
|
2019-05-24 13:52:15 +02:00
|
|
|
|
2019-05-30 13:58:17 +02:00
|
|
|
/* Converse's plugin mechanism will call the initialize
|
2019-05-24 13:52:15 +02:00
|
|
|
* method on any plugin (if it exists) as soon as the plugin has
|
|
|
|
* been loaded.
|
|
|
|
*/
|
2019-05-30 13:58:17 +02:00
|
|
|
initialize: function () {
|
2019-05-24 13:52:15 +02:00
|
|
|
/* Inside this method, you have access to the private
|
|
|
|
* `_converse` object.
|
|
|
|
*/
|
|
|
|
const _converse = this._converse;
|
|
|
|
_converse.log("The \"myplugin\" plugin is being initialized");
|
|
|
|
|
|
|
|
/* From the `_converse` object you can get any configuration
|
|
|
|
* options that the user might have passed in via
|
|
|
|
* `converse.initialize`.
|
|
|
|
*
|
|
|
|
* You can also specify new configuration settings for this
|
|
|
|
* plugin, or override the default values of existing
|
|
|
|
* configuration settings. This is done like so:
|
|
|
|
*/
|
|
|
|
_converse.api.settings.update({
|
|
|
|
'initialize_message': 'Initializing myplugin!'
|
|
|
|
});
|
|
|
|
|
|
|
|
/* The user can then pass in values for the configuration
|
|
|
|
* settings when `converse.initialize` gets called.
|
|
|
|
* For example:
|
|
|
|
*
|
|
|
|
* converse.initialize({
|
|
|
|
* "initialize_message": "My plugin has been initialized"
|
|
|
|
* });
|
|
|
|
*/
|
|
|
|
alert(this._converse.initialize_message);
|
2017-09-04 09:54:18 +02:00
|
|
|
|
2019-05-24 13:52:15 +02:00
|
|
|
/* Besides `_converse.api.settings.update`, there is also a
|
|
|
|
* `_converse.api.promises.add` method, which allows you to
|
|
|
|
* add new promises that your plugin is obligated to fulfill.
|
|
|
|
*
|
|
|
|
* This method takes a string or a list of strings which
|
|
|
|
* represent the promise names:
|
|
|
|
*
|
|
|
|
* _converse.api.promises.add('myPromise');
|
|
|
|
*
|
|
|
|
* Your plugin should then, when appropriate, resolve the
|
|
|
|
* promise by calling `_converse.api.emit`, which will also
|
|
|
|
* emit an event with the same name as the promise.
|
|
|
|
* For example:
|
2018-02-21 16:59:16 +01:00
|
|
|
*
|
2019-05-24 13:52:15 +02:00
|
|
|
* _converse.api.trigger('operationCompleted');
|
2018-02-21 16:59:16 +01:00
|
|
|
*
|
2019-05-24 13:52:15 +02:00
|
|
|
* Other plugins can then either listen for the event
|
|
|
|
* `operationCompleted` like so:
|
|
|
|
*
|
|
|
|
* _converse.api.listen.on('operationCompleted', function { ... });
|
|
|
|
*
|
|
|
|
* or they can wait for the promise to be fulfilled like so:
|
|
|
|
*
|
|
|
|
* _converse.api.waitUntil('operationCompleted', function { ... });
|
2018-02-21 16:59:16 +01:00
|
|
|
*/
|
2019-05-24 13:52:15 +02:00
|
|
|
},
|
2017-09-04 09:54:18 +02:00
|
|
|
|
2019-05-24 13:52:15 +02:00
|
|
|
/* If you want to override some function or a Backbone model or
|
2019-05-30 13:58:17 +02:00
|
|
|
* view defined elsewhere in Converse, then you do that under
|
2019-05-24 13:52:15 +02:00
|
|
|
* the "overrides" namespace.
|
|
|
|
*/
|
2019-05-30 13:58:17 +02:00
|
|
|
overrides: {
|
2019-05-24 13:52:15 +02:00
|
|
|
/* For example, the private *_converse* object has a
|
|
|
|
* method "onConnected". You can override that method as follows:
|
2017-09-04 09:54:18 +02:00
|
|
|
*/
|
2019-05-30 13:58:17 +02:00
|
|
|
onConnected: function () {
|
|
|
|
// Overrides the onConnected method in Converse
|
2019-05-24 13:52:15 +02:00
|
|
|
|
|
|
|
// Top-level functions in "overrides" are bound to the
|
|
|
|
// inner "_converse" object.
|
|
|
|
const _converse = this;
|
|
|
|
|
|
|
|
// Your custom code can come here ...
|
|
|
|
|
|
|
|
// You can access the original function being overridden
|
|
|
|
// via the __super__ attribute.
|
|
|
|
// Make sure to pass on the arguments supplied to this
|
|
|
|
// function and also to apply the proper "this" object.
|
|
|
|
_converse.__super__.onConnected.apply(this, arguments);
|
|
|
|
|
|
|
|
// Your custom code can come here ...
|
2017-09-04 09:54:18 +02:00
|
|
|
},
|
|
|
|
|
2019-05-30 13:58:17 +02:00
|
|
|
/* Override Converse's XMPPStatus Backbone model so that we can override the
|
2019-05-24 13:52:15 +02:00
|
|
|
* function that sends out the presence stanza.
|
2017-09-04 09:54:18 +02:00
|
|
|
*/
|
2019-05-30 13:58:17 +02:00
|
|
|
XMPPStatus: {
|
|
|
|
sendPresence: function (type, status_message, jid) {
|
2019-05-24 13:52:15 +02:00
|
|
|
// The "_converse" object is available via the __super__
|
|
|
|
// attribute.
|
|
|
|
const _converse = this.__super__._converse;
|
|
|
|
|
|
|
|
// Custom code can come here ...
|
|
|
|
|
|
|
|
// You can call the original overridden method, by
|
|
|
|
// accessing it via the __super__ attribute.
|
|
|
|
// When calling it, you need to apply the proper
|
|
|
|
// context as reference by the "this" variable.
|
|
|
|
this.__super__.sendPresence.apply(this, arguments);
|
|
|
|
|
|
|
|
// Custom code can come here ...
|
2017-09-04 09:54:18 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-24 13:52:15 +02:00
|
|
|
}
|
|
|
|
});
|