@chamaileon-sdk/plugin-interface

中文文档
1.1.5发布于 2026/03/18MITESM + CJS零依赖构建来源已验证近 30 天下载 1882

This project is a postMessage interface that creates and maintains the communication between window objects, like a web page and an iframe inside it.

原始使用文档
文档目录13 节
  1. Installation and Initialization
  2. Fullscreen plugin
  3. Initialization
  4. Interface
  5. Inline plugin
  6. Interface
  7. providePlugin
  8. Interface
  9. Update hooks
  10. Starting context
  11. Update the hooks while keeping the already defined ones
  12. Update the hooks and only keep the new ones
  13. Example

Chamaileon plugin interface

This project is a postMessage interface that creates and maintains the communication between window objects, like a web page and an iframe inside it.

Chamaileon.io built and maintains this open-source project to provide a secure and standardized way to use its plugins. We use it in our plugins: email editor, preview, gallery, etc. You can visit our website for more information: chamaileon.io.

Installation and Initialization

npm i @chamaileon-sdk/plugin-interface

The package provides three functions, initFullscreenPlugin, initInlinePlugin and providePlugin.

A plugin initaialization consists of two parts:

  • On the parent side, you have to run either initFullscreenPlugin or initInlinePlugin, based on the usage. The function creates an iframe, and starts loading the plugin based on the src given to it.

  • Inside the plugin, you have to call the providePlugin function on opening. This function responds to the parent side init mechanisms, and returns the interface.

Fullscreen plugin

Initialization

To initialize a fullscreen plugin, you have to call the initFullscreenPlugin function with the following parameters:

initFullscreenPlugin(
    {
        data: Object,
        settings: Object,
        hooks: Object,
    },
    {
        id: String,
        src: String,
        parentElem: String | HTMLElement,
        beforeInit: Function,
        timeout: Number,
    }
);

Parameters in the first object

The parameters in the first object will be sent to the plugin directly.

  • data: you can pass static data to your plugin.
  • settings: you can pass down custom settings that modify the look and operation of the plugin.
  • hooks: you can pass down functions that the plugin will call at certain actions or events.

Parameters in the second object

The initFullscreenPlugin function creates and iframe based on the src provided, and appends it to the parentElem. The second parameter object contains information for the library to create the iframe and append it to your application DOM.

  • id: is the id which will represent the iframe.

  • src: this is the iframe source as a string.

  • parentElem: this is a query selector or HTMLElement that you want the plugin iframe to be inserted into. Default is document.body

  • beforeInit: this function will run after the iframe is created and the container and iframe both can be reached inside of this.

    beforeInit({ container, iframe }) {
        // your code here
    }
    
  • timeout: this is a number in milliseconds. This defines how long should the init function wait for an answer from the providePlugin before throwing an error.

Interface

In the returned object you will get the following properties:

{
    _container: HTMLElement,
    _src: String,
    methods: Object,
    showSplashScreen: Function,
    hideSplashScreen: Function,
    show: Function,
    hide: Function,
    destroy: Function,
}
  • _container: is and HTML element containing the plugin iframe
  • _src: is the source of the plugin iframe
  • methods: through the methods object you can reach the plugins declared methods
  • destroy: this function removes the iframe from the container

Splashscreen

If the settings param provided in the initialization object contains a splashScreenUrl, the plugin will have a separate iframe appended to the container, which you can show and hide with the provided showSplashScreen and hideSplashScreen functions.

Show / Hide

You can show and hide your created plugins with the provided show and hide. While using these functions, the plugin won`t be destroyed, it will keep its state while hidden.

Animations

The show can be called with the following parameters:

show({ x = "-100vw", y = "0px", opacity = 0.5, scale = 1, time = 500 })

The show function provides an easy way to customize your show animation. With the provided parameters, you can set the default hidden state, described by coordinates, opacity, and scale of the plugin, along with the time of the animation. When the function is called, the plugin will move to a fullscreen view from that hidden position. The animation uses the translate3d css function. Likewise, the hide function moves the plugin back to its set hidden state.

The default hidden state is moved to the left, so the show function will move the plugin to view form the left. See the content editor example for more configuration.

Inline plugin

To initialize an inline plugin, you have to call the initInlinePlugin function with the following parameters:

initInlinePlugin(
    {
        data: Object,
        settings: Object,
        hooks: Object,
    },
    {
        src: String,
        container: String | HTMLElement,
        beforeInit: Function,
        timeout: Number,
    }
)

Parameters in the first object

The parameters in the first object will be sent to the plugin directly.

  • data: you can pass static data to your plugin.
  • settings: you can pass down custom settings that modify the look and operation of the plugin.
  • hooks: you can pass down functions that the plugin will call at certain actions or events.

Parameters in the second object

The second object contains information for the library to create the iframe and append it to your application DOM.

  • src: this is the iframe source as a string.

  • container: the element you want the plugin to append to.

  • beforeInit: this function will run after the iframe is created and the container and iframe both can be reached inside of this.

    beforeInit({ container, iframe }) {
        // your code here
    }
    
  • timeout: this is a number in milliseconds. This defines how long should the init function wait for an answer from the providePlugin before throwing an error.

Interface

In the returned object you will get the following properties:

{
    _container: HTMLElement,
    methods: Object,
    destroy: Function,
}
  • _container: is and HTML element containing the plugin iframe
  • methods: through the methods object you can reach the plugins declared methods
  • destroy: this function removes the iframe from the container

providePlugin

When your plugin is loaded from the provided src, your script in the iframe has to call the providePlugin function, in order to respond to the plugin-interface initialization

providePlugin({
    hooks: Array,
    methods: Object,
    validator: Function,
});
  • hooks: This is an array of hook names that the plugin accepts and uses
  • methods: These are functions can be called from outside and are used to interact directly with the plugin from the outside
  • validator: Is a function that will run when the provided data, settings and hooks arrive from the parent side

Interface

The providePlugin function should resolve to an object containing these fields:

{
    data: Object,
    settings: Object,
    hooks: Object,
    terminate: Function,
}
  • data: The data that was sent at the init stage
  • settings: The settings that were sent at the init stage
  • hooks: Hooks that were sent at the init stage and were filtered with the list of hooks that are accepted by the plugin
  • terminate: A function designed to terminate the communication between the window objects.

Update hooks

The updateHooks method can be defined in the plugin side. It can be used to update the hooks that were defined on initialization. We provide two options that you can see on the example below.

Starting context

const onSave = () => {};
const onDelete = () => {};
const onFail = () => {};

const activeHooks = [];

const pluginInterface = await initFullscreenPlugin(...);
// For this example let's say that we sent
// the "onFail" hook with the init function

const pluginInstance = await providePlugin({
    hooks: ["onSave", "onDelete", "onFail" ],
    methods: {
        updateHooks(hooks) {
            activeHooks = hooks;
        },
    },
    validator: () => {},
})

pluginInstanceMethods.updateHooks = (hooks) => {
    activeHooks = hooks;
}

Update the hooks while keeping the already defined ones

await pluginInterface.methods.updateHooks({ hooks: { onSave } });
// after the method call the activeHooks should be equal with ["onSave", "onFail", "error"];

Update the hooks and only keep the new ones

await pluginInterface.methods.updateHooks({ hooks: { onDelete }, resetHooks: true });
// after the method call the activeHooks should be equal with ["onDelete", "error"];

Example

You can run the examples, with static server

$ npm -g install static-server
$ static-server -c "*" -zp 8080

版本与发布记录

@chamaileon-sdk/plugin-interface 共发布 26 个版本,首次发布于 2022/02/15;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.1.5latest2026/03/187 个月前
1.1.42025/09/111.2 年前
1.1.32025/09/111.2 年前
1.1.22025/02/181.7 年前
1.1.12024/02/022 年前
1.1.02022/08/054 年前

关于 @chamaileon-sdk/plugin-interface 的常见问题

@chamaileon-sdk/plugin-interface 是什么?

This project is a postMessage interface that creates and maintains the communication between window objects, like a web page and an iframe inside it.

如何安装 @chamaileon-sdk/plugin-interface?

在项目目录运行 npm install @chamaileon-sdk/plugin-interface,或使用 pnpm add / yarn add / bun add。

@chamaileon-sdk/plugin-interface 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/chamaileon-sdk__plugin-interface 是否存在。

@chamaileon-sdk/plugin-interface 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@chamaileon-sdk/plugin-interface 可以商用吗?

@chamaileon-sdk/plugin-interface 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@chamaileon-sdk/plugin-interface 的下载量有多少?

根据 npm 官方下载统计,@chamaileon-sdk/plugin-interface 在 2026/10/10 – 2026/11/08 这 30 天内被下载 1,882 次,日均约 63 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@chamaileon-sdk/plugin-interface 还在维护吗?

最新版本 1.1.5 发布于 2026/03/18(7 个月前);至今共发布 26 个版本,其中过去 12 个月发布了 1 个;首次发布于 2022/02/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。