插件
关于 OneComme 搭载的插件功能
OneComme(5.2 及以上版本)内置了基于 JavaScript(Node.js)运行的插件机制
开发 --
使用 console 输出的内容会写入 日志文件 的 plugin.log 中。
console.info("Hello OneComme!");
示例
OneComme 示例插件兼模板
https://github.com/OneComme/OneCommeOrderSpeechPlugin
OneComme 评论过滤示例插件
https://github.com/OneComme/OneCommeFilterSamplePlugin
※ 示例插件可自由修改和再分发
代码整体结构
const plugin = {
name: '示例插件', // @required plugin name
uid: 'com.onecoome.sampleplugin', // @required unique plugin id
version: '0.0.1', // @required semver version
author: 'OneComme', // @required author name
url: 'https://onecomme.com', // @optional link (ex. documentation link)
permissions: ['comments'], // @required https://onecomme.com/docs/developer/websocket-api/#%E3%82%A4%E3%83%99%E3%83%B3%E3%83%88%E3%81%AE%E7%A8%AE%E9%A1%9E%E3%81%A8%E3%83%87%E3%83%BC%E3%82%BF
defaultState: { // @optional key-value custom state
count: 0
},
/**
*
* @param { dir: string, filepath: string, store: ElectronStore} param
* dir: plugin directory path
* filepath: this script's path
* store: ElectronStore Instance https://github.com/sindresorhus/electron-store?tab=readme-ov-file#instance
*/
init({ dir, store }, initialData) {},
/**
* called on exit or when activated
* @optional
*/
destroy() {},
/**
* called when the event specified in permissions occurs ( exclude connected event )
* @optional
* https://onecomme.com/docs/developer/websocket-api
*/
subscribe(type, ...args) {
switch (type) {
case 'comments': {
}
}
},
/**
* filter comment
* @param {Comment} Comment
* @param {Service} Service
* @param {UserNameData | null} UserData
* @returns Promise
*/
filterComment(comment, service, userData) {
if (comment.service === 'sample') return false
return comment
},
/**
* filter speech
* @param {string} text
* @param {UserNameData | null} userData
* @param {SpeechConfig} config
* @param optional {Comment} comment
* @returns Promise
*/
filterSpeech(text, userData, config, comment) {
if (!userData) return false
return text
},
/**
* called when a request is made to the plugin-specific RestAPI
* @param {
* url: string // request url
* method: 'GET' | 'POST' | 'PUT' | 'DELETE'
* params: {[key: string]: string} // querystrings
* body?: any // request body
* } req
* @returns {
* code: number // status code
* response: Object or Array // response data
* }
*/
async request(req: PluginRequest) {
// [GET, POST, PUT, DELETE]
// endpoint: localhost:11180/api/plugins/com.onecomme.plugin-sample
switch (req.method) {
case 'GET': {}
case 'POST': {}
case 'PUT': {}
case 'DELETE': {}
}
return {
code: 404,
response: {}
}
}
}
module.exports = plugin
| name | 必需 | 插件的名称 |
| uid | 必需 | 插件的固有 ID。必须是与其他插件都不重复的唯一 ID |
| version | 必需 | 插件本身的版本号 |
| author | 必需 | 插件开发者的名称 |
| url | 设置后,将在 OneComme 的插件页面显示打开该链接的按钮(请链接到设置页面或使用手册页面) | |
| permissions | 必需 | 以数组形式列出插件所使用的数据类型 未在此处列出的数据将无法获取 |
| defaultState | 定义插件所保存数据的初始值 state 会通过 store 保存为 json 文件并持久化 | |
| init({ dir, store },initialData):void | 在插件被启用时,或在启用状态下 OneComme 启动时执行 dir:插件目录路径 store:ElectronStore 实例 | |
| destroy():void | 在插件被停用时,或在启用状态下 OneComme 退出时执行 | |
| subscribe(type: SendType, …args: any[]) | 在接收到 permissions 中指定的数据时执行 ※ 第二个参数之后的内容会因数据类型而异 | |
| filterComment(comment: Comment, service: Service, userData: UserData): Promise<Comment | boolean> | 在接收到评论时执行,通过返回评论数据,可以将加工后的评论等返回给 OneComme 返回 false 时,该评论会被过滤掉 ※ 需要在 permissions 中指定 'filter.comment' | |
| filterSpeech(text: string, userData: UserNameData, config: SpeechConfig, comment?: Comment ): Promise<string | boolean> | 在朗读前执行,通过返回朗读文本,可以按加工后的内容进行朗读 返回 false 时,将不会进行朗读 ※ 需要在 permissions 中指定 'filter.speech' ※ 请注意,当通过朗读专用 API 直接发送等情况下,可能不包含 comment | |
| request(req: PluginRequest): Promise< PluginResponse > | 在接收到对各插件提供的 RestAPI 的请求时执行 可用于从设置画面等向插件保存数据,或接收插件中保存的数据等用途 ※ 请参阅后文的插件 RestAPI |
插件 RestAPI
当插件处于启用状态时,OneComme 会为该插件提供 RestAPI
可以对 http://localhost:11180/api/plugins/$\{PLUGIN_UID\} 发起 GET/POST/PUT/DELETE 请求
请求会被发送到插件侧的 request 函数
请求方示例: https://github.com/OneComme/OneCommeOrderSpeechPlugin/blob/main/static/script.js
接收方示例: https://github.com/OneComme/OneCommeOrderSpeechPlugin/blob/main/src/index.ts