-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
159 lines (137 loc) · 4.24 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* GitOK示例插件
* 演示如何创建一个基本的插件,提供动作和自定义视图
*/
// 引入文件系统模块
const fs = require('fs');
const path = require('path');
// 日志函数
const log = {
info: function (message, ...args) {
console.log(`[示例插件] ${message}`, ...args);
},
error: function (message, ...args) {
console.error(`[示例插件] ${message}`, ...args);
},
debug: function (message, ...args) {
console.log(`[示例插件:调试] ${message}`, ...args);
},
};
// 插件信息
const plugin = {
name: '示例插件',
description: '这是一个示例插件,演示如何创建GitOK插件',
version: '1.0.0',
author: 'Coffic',
/**
* 获取插件提供的动作列表
* @param {Object} context 插件上下文
* @param {string} context.keyword 搜索关键词
* @param {string} context.overlaidApp 被覆盖应用名称
* @returns {Promise<Array>} 动作列表
*/
async getActions({ keyword = '', overlaidApp = '' }) {
log.info(
`获取动作列表,关键词: "${keyword}", 被覆盖应用: "${overlaidApp}"`
);
// 创建基础动作列表
const actions = [
{
id: `hello`,
title: '打招呼',
description: '显示一个问候消息',
icon: '👋',
},
{
id: `time`,
title: '当前时间',
description: '显示当前时间',
icon: '🕒',
viewPath: 'views/time.html',
viewMode: 'embedded',
devTools: false,
},
{
id: `calculate`,
title: '计算器',
description: '简单的计算器',
icon: '🧮',
viewPath: 'views/calculator.html',
viewMode: 'window',
devTools: false,
},
];
log.debug(`基础动作列表: ${actions.length} 个动作`);
// 如果有关键词,过滤匹配的动作
if (keyword) {
const lowerKeyword = keyword.toLowerCase();
log.debug(`过滤包含关键词 "${lowerKeyword}" 的动作`);
const filteredActions = actions.filter(
(action) =>
action.title.toLowerCase().includes(lowerKeyword) ||
action.description.toLowerCase().includes(lowerKeyword)
);
log.info(`过滤后返回 ${filteredActions.length} 个动作`);
return filteredActions;
}
log.info(`返回所有 ${actions.length} 个动作`);
return actions;
},
/**
* 执行插件动作
* @param {Object} action 要执行的动作
* @returns {Promise<any>} 动作执行结果
*/
async executeAction(action) {
log.info(`执行动作: ${action.id} (${action.title})`);
try {
switch (action.id) {
case `hello`:
log.debug(`执行打招呼动作`);
return { message: '你好,这是来自示例插件的问候!' };
case `time`:
log.debug(`执行时间动作(有自定义视图)`);
return { success: true };
case `calculate`:
log.debug(`执行计算器动作(有自定义视图)`);
return { success: true };
default:
const errorMsg = `未知的动作ID: ${action.id}`;
log.error(errorMsg);
throw new Error(errorMsg);
}
} catch (error) {
log.error(`执行动作 ${action.id} 失败:`, error);
throw error;
}
},
/**
* 获取视图内容
* @param {string} viewPath 视图路径
* @returns {Promise<string>} HTML内容
*/
async getViewContent(viewPath) {
log.info(`获取视图内容: ${viewPath}`);
try {
// 从文件系统读取HTML文件
const htmlFilePath = path.join(__dirname, viewPath);
log.debug(`尝试读取文件: ${htmlFilePath}`);
if (!fs.existsSync(htmlFilePath)) {
throw new Error(`视图文件不存在: ${htmlFilePath}`);
}
// 读取HTML文件内容
const html = fs.readFileSync(htmlFilePath, 'utf-8');
log.info(
`成功读取视图HTML,文件: ${htmlFilePath},长度: ${html.length} 字节`
);
return html;
} catch (error) {
log.error(`获取视图内容失败:`, error);
throw error;
}
},
};
// 插件初始化输出
log.info(`示例插件已加载: ${plugin.name} v${plugin.version}`);
// 导出插件
module.exports = plugin;