擴充功能
關於 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: 'わんコメ', // @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