# LIFF plugin ## What is LIFF plugin LIFF plugin is a feature to extend the LIFF SDK. Using a LIFF plugin, you can add your own APIs to the LIFF SDK or change the behavior of LIFF APIs. A LIFF plugin is an object or a class with specific properties and a specific method. ## Operating environment of LIFF Plugin LIFF plugin is available in LIFF v2.19.0 or later. ## Using a LIFF plugin Use the [`liff.use()`](https://developers.line.biz/en/reference/liff/#use) method to activate a LIFF plugin. Passing a LIFF plugin to the [`liff.use()`](https://developers.line.biz/en/reference/liff/#use) method activates the LIFF plugin. When the LIFF plugin is activated, the `liff` object will be extended and the API of the LIFF plugin will be available. The following is an example of activating a LIFF plugin called `GreetPlugin` and executing the `liff.$greet.hello()` method. ### If the LIFF plugin is a class If the LIFF plugin is a class, you need to pass the instance to the [`liff.use()`](https://developers.line.biz/en/reference/liff/#use) method. ```js class GreetPlugin { constructor() { this.name = "greet"; } install() { return { hello: this.hello, }; } hello() { console.log("Hello, World!"); } } liff.use(new GreetPlugin()); liff.$greet.hello(); // Hello, World! liff .init({ liffId: "123456-abcedfg", // Use own liffId }) .then(() => { // ... }); ``` ### If the LIFF plugin is an object ```js const hello = () => { console.log("Hello, World!"); }; const greetPlugin = { name: "greet", install() { return { hello, }; }, }; liff.use(greetPlugin); liff.$greet.hello(); // Hello, World! liff .init({ liffId: "123456-abcedfg", // Use own liffId }) .then(() => { // ... }); ``` As you can see above, when a LIFF plugin is activated, a property is added to the `liff` object with a `$` prefix to the value of the `name` property. This allows you to use the API of a LIFF plugin in `liff.${value of the name property of the LIFF plugin}.{property name}` and `liff.${value of the name property of the LIFF plugin}.{method name}()` formats. ## Creating a LIFF plugin You can create a LIFF plugin as an object or a class that has the [`name`](https://developers.line.biz/en/docs/liff/liff-plugin/#name) property and the [`install()`](https://developers.line.biz/en/docs/liff/liff-plugin/#install) method. The following is an example of a LIFF plugin called `GreetPlugin` which offers the `hello` method and the `goodbye()` method as APIs. ### If the LIFF plugin is a class ```js class GreetPlugin { constructor() { this.name = "greet"; } install() { return { hello: this.hello, goodbye: this.goodbye, }; } hello() { console.log("Hello, World!"); } goodbye() { console.log("Goodbye, World!"); } } liff.use(new GreetPlugin()); liff.$greet.hello(); // Hello, World! liff.$greet.goodbye(); // Goodbye, World! ``` ### If the LIFF plugin is an object ```js const hello = () => { console.log("Hello, World!"); }; const goodbye = () => { console.log("Goodbye, World!"); }; const greetPlugin = { name: "greet", install() { return { hello, goodbye, }; }, }; liff.use(greetPlugin); liff.$greet.hello(); // Hello, World! liff.$greet.goodbye(); // Goodbye, World! ``` ### name propery The value of the `name` property is the name of a LIFF plugin. Specify the `name` property a string. The specified value will be the property name of the `liff` object, as in `liff.${value of the name property of the LIFF plugin}`. ### install() method The `install()` method is a function that does the following: - [Describing the initialization process of the LIFF plugin](https://developers.line.biz/en/docs/liff/liff-plugin/#describe-initialization-process-for-liff-plugin) - [Defining the API of the LIFF plugin](https://developers.line.biz/en/docs/liff/liff-plugin/#define-liff-plugin-api) #### Describing the initialization process of the LIFF plugin The `install()` method will be executed by the [`liff.use()`](https://developers.line.biz/en/reference/liff/#use) method when the LIFF plugin is activated. Therefore, you can describe the initialization process of the LIFF plugin within the `install()` method. #### Defining the API of the LIFF plugin The API of the LIFF plugin is defined as the return value of the `install()` method. You can define multiple APIs by returning an object. If the LIFF plugin has only one API, it's possible to use that API as the return value. The following is an example of the `install()` method that returns a function instead of an object. ```js class GreetPlugin { constructor() { this.name = "greet"; } install() { return this.hello; } hello() { console.log("Hello, World!"); } } liff.use(new GreetPlugin()); liff.$greet(); // Hello, World! ``` #### Arguments of the `install()` method The `install()` method takes a [`context`](https://developers.line.biz/en/docs/liff/liff-plugin/#context) object as the first argument and an [`option`](https://developers.line.biz/en/docs/liff/liff-plugin/#option) as the second argument. ```js class GreetPlugin { constructor() { this.name = "greet"; } install(context, option) {} } ``` ##### `context` object The first argument of the `install()` method. The `context` object has the following two properties: | Property | Value | | --- | --- | | `liff` | `liff` object | | `hooks` | Object that provides methods for [registering a callback on a hook](https://developers.line.biz/en/docs/liff/liff-plugin/#register-callback-with-hook) | ##### `option` The second argument of the `install()` method. The value specified in the second argument of the [`liff.use()`](https://developers.line.biz/en/reference/liff/#use) method is passed. If the second argument of the [`liff.use()`](https://developers.line.biz/en/reference/liff/#use) method is unspecified, the value of `option` will be `undefined`. You can use `option` to customize the behavior of a LIFF plugin by passing an argument to the [`liff.use()`](https://developers.line.biz/en/reference/liff/#use) method. ## About hook Hook is a mechanism in LIFF plugin that allows pre-registered callbacks to be executed at a specific time during the processing of a LIFF API. You can think of hook in the same way as event processing in JavaScript. If a callback is registered with a hook, the callback will be executed at the timing when the hook is fired. In addition to using hooks provided by the LIFF API, LIFF plugins can provide their own hooks. ### Hooks for LIFF API At this time, the LIFF API provides hooks only for the [`liff.init()`](https://developers.line.biz/en/reference/liff/#initialize-liff-app) method.
| LIFF API | Hook | Hook type | When the hook fires |
|---|---|---|---|
liff.init()method |
before hook |
async hook | Immediately after calling liff.init() (before the initialization of a LIFF app) |
after hook |
async hook | Immediately before calling successCallback (after the initialization of a LIFF app) |