Skip to content

hook 与权限 ​

hook ​

所有扩展点共用 HookRegistry。插件进程里是访问器级别(get / set / apply / construct),宿主侧是 apply:

ts
// 插件进程(HostOptions.rendererHooks 指定的模块里)
hooks.on('node:fs.readFileSync', { apply: (ctx, next) => (log(ctx.args[0]), next()) })
hooks.on('utools:getAppVersion', { apply: () => '7.1.0' })
hooks.on('api:identity', { get: (ctx, next) => ({ ...next(), channel: 'beta' }) })

// 宿主侧
host.hooks.on('provider:user.get', { apply: (ctx, next) => ({ ...next(), vip: true }) })
host.hooks.on('window:create', { apply: async (ctx, next) => { const r = await next(); return { ...r, options: { ...r.options, alwaysOnTop: false } } } })
host.hooks.on('panel:detach', { apply: () => undefined })              // 禁止分离窗口
host.hooks.on('source:register', { apply: (c, next) => (c.args[0].id === 'panel' ? null : next()) })  // 不要超级面板
host.hooks.on('permission:prompt', { apply: (c) => myPromptUI(c.args[0]) })
host.hooks.on('plugin:install', { apply: (c) => c.args[0].id.startsWith('trusted-') })   // 自定义安装审核

命名空间:api: <兼容层注入名>: node: electron: event: service: provider: panel: window: entry: source: permission: lifecycle: plugin: schedule: theme: tray: log:。

入口和来源 ​

  • 入口类型(关键字怎么匹配):host.entries.register({ type, match(cmd, input) })。uTools 的 text / regex / over / img / files / window 由兼容层注册。
  • 唤起来源(从哪里进入,即 action.from):host.sources.register({ id, setup(ctx) }),在 setup 里接快捷键、URL scheme、托盘,ctx.emit(input) 交给匹配器或 ctx.enter(...) 直接进入。uTools 的 main / panel / hotkey / redirect 由兼容层注册。

权限 id ​

storage fs.plugin fs.read fs.write clipboard.read clipboard.write notification dialog shell.open shell.external shell.trash screen.read screen.capture window.create window.overlay input.paste input.simulate input.drag network process.spawn process.native electron.raw system.identity system.context schedule features redirect ai mcp browser media.ffmpeg image.sharp payment media.camera media.microphone geolocation device.hardware

询问时可以选「始终允许 / 仅本次 / 禁止 / 以后再说」,连续关掉 3 次自动转为禁止;路径和网址类权限带作用域。

错误码 ​

EWTPERM 权限被拒 · EWTUNSUPPORTED 平台不支持 · EWTINSTALL 安装被策略或用户拒绝 · EWTENCRYPTED upxs 加密包 · EWTCONFIG 构建配置错误 · EWTSIGN 签名凭据不全 · EWTBUILDER 没装 electron-builder · EWTUSAGE / EWTFAIL 命令行用法错误 / 其他失败(--json 输出里的 code)

MIT License