插件开发指南
用 HTML + CSS + JS 开发插件,无需学习新框架。灵核宿主基于 Electron,插件运行在隔离的 BrowserView 中,通过 window.host.* 调用内核能力。
三大支柱
- 微内核 Microkernel:宿主只提供
window.host能力接口,插件之间完全隔离。一个插件崩了不影响其他。 - 声明式权限:插件在
plugin.json声明需要什么权限,内核加载时校验,未授权直接拦截。 - Web 技术栈:插件就是 HTML + CSS + JS,打包成
.kpkg发布。
架构概览
┌─────────────────────────────────────┐
│ 灵核宿主 (Electron) │
│ ┌─────────┐ ┌─────────┐ ┌──────┐ │
│ │ 插件 A │ │ 插件 B │ │ ... │ │ ← BrowserView 隔离
│ │(index.html)│ │(index.html)│ │ │ │
│ └────┬────┘ └────┬────┘ └──┬───┘ │
│ │ window.host.* │ │
│ ┌────▼─────────────────────────▼───┐ │
│ │ 内核 / Kernel │ │ ← 权限校验 + 能力实现
│ │ clipboard · fs · net · shell ... │ │
│ └───────────────────────────────────┘ │
└─────────────────────────────────────┘
关键: 插件通过
window.host调用内核能力,所有调用必须有对应权限声明。插件无法直接访问 Node.js API。
5 步发布你的第一个插件
STEP 1:创建目录
建一个以插件 id 命名的目录,放入 plugin.json 和入口 index.html。
my-plugin/
├── plugin.json // 插件清单
├── index.html // 主界面
├── style.css // 样式(可选)
└── script.js // 逻辑(可选)
STEP 2:写 plugin.json
{
"id": "com.example.hello",
"name": "Hello",
"version": "1.0.0",
"description": "示例插件",
"author": "你的名字",
"main": "index.html",
"features": [
{ "code": "hello", "cmds": ["你好", "hello"], "explain": "弹出问候" }
],
"permissions": []
}
STEP 3:写 index.html
插件就是一个网页,通过 window.host.* 调用宿主能力:
<!doctype html>
<html>
<body>
<h1>Hello, Nucleon!</h1>
<button id="btn">读取剪贴板</button>
<script>
document.getElementById('btn').addEventListener('click', async () => {
// 通过 window.host 调用内核能力
const plugins = await window.host.listPlugins();
console.log(plugins);
});
</script>
</body>
</html>
STEP 4:打包成 .kpkg
将整个目录压缩成 zip,改扩展名为 .kpkg。或者在插件中心「我的插件」点「打包」。
STEP 5:发布到市场
登录开发者控制台,填写信息并上传 .kpkg 即可发布。
下一步: 阅读 plugin.json 参考 了解完整字段,以及 API 参考 查看所有宿主 API。