Skip to content

JS 脚本

客户端 JS 脚本运行于 V8 (Javet),支持 ES2022,用于 UI 交互与 Ponder 场景。

Behemiron 的 JS 脚本运行在客户端,通过 JNI 绑定 V8 引擎,支持 100% 的 ES2022 标准语法。 主要用于:

  • UI(客户端 Runtime):界面逻辑、交互、热键、音效
  • Ponder:场景注册、演示逻辑

JS 只做表现与交互,服务端逻辑请使用 Kether(见 Kether)。

1. 运行模型

  • JS 运行在客户端,不提供 Node.js / 文件系统 / 网络 API
  • 可用功能由模块注入(UI/Hotkeys/Sound 等)。

2. 安全边界

能力是否可用说明
ES2022 语法可用100% 标准语法支持
DOM / BOM不可用非浏览器环境
Node.js API不可用fs, net, http 等均不存在
文件系统不可用无法读写本地文件
网络请求不可用fetch / XMLHttpRequest

所有可用 API 由 Behemiron 模块注入到上下文,脚本无法访问注入范围之外的任何能力。

3. 全局 API 速查

UI 相关

API说明
document.getElementById(id)id 获取 UI 元素
closeUI()关闭当前 UI
onMounted(fn)UI 挂载生命周期
onUnmounted(fn)UI 卸载生命周期

音效

API说明
Sound.play(id, volume?, pitch?, loop?)播放音效
Sound.playAt(id, x, y, z, volume?, pitch?, loop?)坐标播放
Sound.stop(id)停止音效

热键

API说明
Hotkeys.register(options, callback)注册 UI 热键
Hotkeys.unregister(id)注销热键
Hotkeys.clear()清理全部 UI 热键

完整 API 参见:音效 API | 热键 API | UI JS API

4. 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>

常用函数:

  • document.getElementById("id"):获取 UI 元素
  • onMounted / onUnmounted:生命周期
  • Sound / Hotkeys / Anim / Query 等 API

5. Ponder 脚本

Ponder 脚本由服务端打包下发:

  • 按命名空间管理
  • 支持版本号
  • 客户端校验清单签名后执行

具体 Ponder API 见 Ponder API 速查

6. 性能与规范

规则说明
禁止阻塞循环不要写 while(true) 之类
事件驱动尽量使用回调和事件
避免大量引用JS 中不要持有大量实体引用(容易泄露)

7. 常见问题

现象排查方向
脚本没执行检查是否在正确的 UI / 命名空间内
API 不存在该 API 未注入当前上下文,参见 UI JS API
控制台报错查看客户端日志,关键词 PonderJSBUIServerAPI