主题
JS 脚本
Behemiron 的 JS 脚本运行在客户端,通过 JNI 绑定 V8 引擎和 Node JS (Javet),支持 100% 的 ES2022 标准语法。 主要用于:
- UI(BUI):界面逻辑、交互、热键、音效
- Ponder:场景注册、演示逻辑
JS 只做表现与交互,服务端逻辑请使用 Kether(见 Kether)。
1. 运行模型
- 每个 UI 或 Ponder 命名空间都有独立上下文。
- JS 运行在客户端,不提供 Node.js / 文件系统 / 网络 API。
- 可用功能由模块注入(UI/Hotkeys/Sound 等)。
可用 API 以 UI JS API 为准。
2. UI 脚本
UI 脚本写在 BUI 模板内:
xml
<template>
<Panel>
<Label :text="title" />
<Button @click="onClick">确认</Button>
</Panel>
</template>
<script>
let title = "任务确认"
function onClick() {
sound.play("ui/confirm")
closeUI()
}
</script>常用函数:
ref("id"):获取 UI 元素onMounted / onUnmounted:生命周期sound / hotkeys / animation / query等 API
3. Ponder 脚本
Ponder 脚本由服务端打包下发:
- 按命名空间管理
- 支持版本号
- 客户端校验清单签名后执行
具体 Ponder API 见 Ponder API 速查。
4. 性能与规范
- 不要写阻塞循环(
while(true)之类)。 - 尽量使用事件驱动和回调。
- JS 中不要持有大量实体引用(容易泄露)。
5. 常见问题
- 脚本没执行:检查是否在正确的 UI/命名空间内。
- API 不存在:该 API 未注入当前上下文,参见 UI JS API。
- 控制台报错:查看客户端日志,关键词
PonderJS或BUI。