1. 后端处理服务器API
1.1  数据库结构
1.2  后端处理服务器HTTP 接口
1.3. 数据包
1.3.1  数据包的基本信息
1.3.2  所有数据包
2. 后端处理服务器应用集成API
2.1  设置应用集成
2.2  API 格式和签名机制
2.3  API 定义
3. 客户端扩展机制
3.1. 扩展定义
3.1.1  扩展类型
3.1.2  扩展包目录结构
3.1.3  扩展描述文件
3.2. 扩展模块
3.2.1  主入口模块
3.2.2. 扩展模块API
3.2.2.1  扩展 API 概览
3.2.2.2  扩展 API 权限
3.2.2.3  扩展 API 定义
3.2.3  扩展实例对象
3.2.4  数据存储机制
3.2.5  自定义上下文菜单
3.2.6  自定义命令
3.2.7  自定义网址解析
3.3. 开发扩展
3.3.1  载入开发中的扩展
3.3.2  开发应用扩展
3.3.3  开发插件扩展
3.3.4  开发主题扩展
3.4  内置扩展
3.5  分发扩展
4. 会话机器人开发指南
4.1  机器人应用开发

开发插件扩展

2020-08-11 11:14:48
孙浩
5012
最后编辑:孙浩 于 2020-08-11 11:22:15
分享链接

插件扩展通常不包含具体的界面,但可以在界面初始化及关键事件触发时得到通知并执行代码。例如可以通过监听用户发送消息,并在消息发送之前修改消息的内容。

每一个插件扩展需要提供一个入口模块文件,在package.json文件中通过main属性指定。如果不指定此文件则默认使用扩展包目录的index.js文件作为主入口模块文件。扩展主入口模块文件为一个 JavaScript 模块,当喧喧加载完毕时会逐个加载各个扩展的主入口模块。在扩展主入口模块中可以访问全局扩展对象global.xext。

下面为一个简单插件扩展包目录结构:

helloworld-plugin-example/
 ├─ lib/
 │   └─ index.js         # 扩展主入口模块
 └─ package.json         # 扩展包描述文件

扩展包内源码文件内容如下:

package.json
{
    "name": "helloworld-plugin-example",
    "displayName": "插件示例",
    "version": "1.0.0",
    "description": "此插件用于演示喧喧的插件机制。",
    "type": "plugin",
    "icon": "mdi-cards-playing-outline",
    "accentColor": "#afb42b",
    "hot": true,
    "main": "lib/index.js",
    "author": {
        "name": "${author.name}"
    },
    "publisher": "${company}",
    "usesPermissions": [
        "commander.registerCommand",
        "contextmenu.addContextMenuCreator",
        "components.Modal"
    ]
}
lib/index.js
const xext = require('xext');
// 用于存储计时器标志
let timerTask = null;
module.exports = {
    onAttach: (ext) => {
        console.log('>> 扩展【helloworld-plugin-example】:扩展加载完成,刚刚加载等扩展名称是:', ext.displayName, ext.version);
    },
    onReady: (ext) => {
        console.log('>> 扩展【helloworld-plugin-example】:界面已准备就绪。', ext.displayName, ext.version);
    },
    onDetach: (ext) => {
        // 扩展将被卸载,此时应该清理计时器
        clearTimeout(timerTask);
        timerTask = null;
        console.log('>> 扩展【helloworld-plugin-example】:扩展已卸载。', ext.displayName, ext.version);
    },
    onUserLogin: (user, error) => {
        // 当用户登录时在此处可以进行相关操作,下面以显示当前登录等结果和用户名为例
        if (user && !error) { // 表示登录成功
            console.log('>> 扩展【helloworld-plugin-example】:用户登录成功了,用户名称是:', user.displayName);
        }
    },
    onUserLogout: (user) => {
        if (user) {
            console.log('>> 扩展【helloworld-plugin-example】:用户退出登录了,用户名称是:', user.displayName);
        }
    },
    onUserStatusChange: (status, oldStatus, user) => {
        console.log('>> 扩展【helloworld-plugin-example】:用户状态发生变化了', {status, oldStatus, user});
    },
    onSendChatMessages: (messages, chat, user) => {
        console.log('>> 扩展【helloworld-plugin-example】:用户发送了消息', {messages, chat, user});
    },
    onReceiveChatMessages: (messages, user) => {
        console.log('>> 扩展【helloworld-plugin-example】:用户收到了消息', {messages, user});
    },
    commands: {
        saveText: (context, ...params) => {
            console.log('保存文本成功');
            return 'ok';
        }
    },
    contextMenuCreators: [{
        match: 'message.text',
        items: [{
            icon: 'mdi-emoticon-cool',
            label: 'say hello',
            click: () => {
                xext.components.Modal.alert('hello');
            }
        }, {
            icon: 'mdi-earth',
            label: '访问禅道',
            url: 'http://zentao.net'
        }]
    }, {
        match: 'chat.sendbox.toolbar',
        create: context => {
            return [{
                icon: 'mdi-star',
                label: {'zh-cn': '说你好', en: 'say-hello'},
                click: () => {
                    context.sendContent('Hello!');
                }
            }];
        }
    }]
};

此扩展实例可以在 helloworld-plugin-example.zip 获取,更多相关例子:

评论列表
糟糕 2022-09-09 15:19:35
《本地页面应用:在扩展包中提供一个 HTML 文件作为页面嵌入,用于开发一个全新的应用,但不想受官方界面样式表和 React 模式限制,在你的 HTML 文件中执行的 JS 代码仍然可以使用 nodejs 的内置模块;》
实际用不了nodejs的内置模块,或者有无教程?
Bee 2022-09-13 09:38:54
可以联系下官网商务QQ,邀请加入技术交流群,群里一起确认下这个问题。
1/1
发表评论
伍 加 拾 =
评论通过审核后显示。