Skip to content

其他工具函数

提示

请务必先阅读如何正确使用酒馆助手

点击查看对应类型定义文件 (可发给 AI 或 IDE 使用, 酒馆助手界面中提供了打包下载)

reloadIframe

在前端界面或脚本内使用, 从而重新加载前端界面或脚本.

这相当于调用 window.location.reload(), 会让分享到全局的接口失效; 如果有需要在重新加载前端界面后沿用的数据, 你应该自行编写重新加载方式而不是使用这个函数.

function reloadIframe(): void;

示例

ts
// 当聊天文件变更时, 重新加载前端界面或脚本
let current_chat_id = SillyTavern.getCurrentChatId();
eventOn(tavern_events.CHAT_CHANGED, chat_id => {
  if (current_chat_id !== chat_id) {
    current_chat_id = chat_id;
    reloadIframe();
  }
})

substitudeMacros

替换字符串中的 SillyTavern 宏

ts
function substitudeMacros(text: string): string;

参数

text

  • 类型: string
  • 描述: 需要替换的字符串

返回值

  • 替换结果: string

示例

ts
const text = substitudeMacros("{{char}} speaks in {{lastMessageId}}");
text === "少女歌剧 speaks in 5";

getLastMessageId

获取最新楼层号

ts
function getLastMessageId(): number;

返回值

  • 最新楼层号: number

errorCatched

包装任意函数, 返回一个会将报错消息通过酒馆通知显示出来的同功能函数.

ts
function errorCatched<T extends any[], U>(fn: (...args: T) => U): (...args: T) => U;

参数

fn

  • 类型: (...args: T) => U
  • 描述: 要包装的函数

返回值

  • 包装后的函数: (...args: T) => U
  • 描述: 包装后的函数会捕获 fn 函数的报错, 并通过酒馆通知显示出来

示例

ts
function test() {
  throw Error(`test`);
}
const error_catched_test = errorCatched(test);
error_catched_test();
// 酒馆通知: test

getIframeName 🚫TavernHelper

获取前端界面或脚本的 iframe 标识名称

ts
function getIframeName(): string;

返回值

  • iframe 标识名称: string
    • 对于楼层消息是 TH-message--楼层号--前端界面是该楼层第几个界面
    • 对于脚本库是 TH-script--脚本名称--脚本id

getMessageId 🚫TavernHelper

从前端界面的 iframe 标识名称 iframe_name 获取它所在楼层的楼层号, 只能对前端界面 iframe 标识名称使用

ts
function getMessageId(iframe_name: string): number;

参数

iframe_name

  • 类型: string
  • 描述: 前端界面的 iframe 标识名称

返回值

  • 楼层号: number

异常

  • 如果提供的 iframe_name 不是前端界面 iframe 标识名称, 将会抛出错误

getCurrentMessageId 🚫TavernHelper

获取本前端界面所在楼层的楼层号, 只能在前端界面内使用

ts
function getCurrentMessageId(): number;

返回值

  • 楼层号: number

异常

  • 如果不在前端界面内使用, 将会抛出错误

getScriptId 🚫TavernHelper

获取脚本库的脚本 id

ts
function getScriptId(): string;

返回值

  • 脚本 id: string

异常

  • 如果不在脚本内使用, 将会抛出错误

作者:KAKAA, 青空莉想做舞台少女的狗