showKeywordDialog: function() {
// 1. 检查是否已在显示对话框(避免重复弹出)
if (typeof isKeywordDialogShowing !== 'boolean' || isKeywordDialogShowing) {
return;
}
isKeywordDialogShowing = true;
// 2. 在主线程中处理 UI 操作(避免子线程问题)
ui.run(() => {
try {
// 3. 获取默认值(确保 currentConfig 已初始化)
let defaultValue;
if (typeof currentConfig === 'object' && currentConfig !== null && Array.isArray(currentConfig.keyword)) {
defaultValue = currentConfig.keyword.join(", ");
} else {
defaultValue = "星星人, 前方高能";
currentConfig = { keyword: defaultValue.split(", ") }; // 初始化默认值
}
// 4. 调用 rawInput(阻塞式,但在主线程安全)
let input = rawInput("修改商品关键词", defaultValue, "输入新关键词(用逗号分隔)");
// 5. 处理用户取消
if (input === null) {
console.log("❌ 用户取消了输入");
return;
}
// 6. 安全处理输入内容
let newKeywords = String(input) // 确保是字符串
.split(/\s*,\s*/) // 分割逗号分隔的关键词
.map(k => k.trim()) // 去除空格
.filter(k => k !== ""); // 过滤空值
// 7. 更新 currentConfig(确保线程安全)
if (!currentConfig) {
currentConfig = {};
}
currentConfig.keyword = newKeywords;
console.log("✅ 商品关键词已更新:", newKeywords);
toast("关键词更新成功!");
} catch (e) {
console.error("❌ 修改关键词时出错:", e);
toast("修改关键词失败: " + e.message);
} finally {
isKeywordDialogShowing = false;
}
});
},
为什么运行后,点击确认,就会闪退hamibot
回答
根据你提供的代码和问题描述,运行后点击确认就闪退可能由以下几个原因导致:
-
rawInput
函数的问题:rawInput
是一个阻塞式输入函数,但在某些情况下可能会导致脚本崩溃。建议使用dialogs.build()
构造对话框来替代rawInput
,以避免潜在的兼容性问题。
-
currentConfig
初始化问题:- 在代码中,
currentConfig
可能未正确初始化,导致在尝试访问currentConfig.keyword
时出现错误。建议确保currentConfig
在使用前已正确初始化。
- 在代码中,
-
UI 操作线程问题:
- 虽然代码中使用了
ui.run(() => { ... })
来确保在主线程中执行 UI 操作,但某些情况下仍可能导致问题。可以尝试移除ui.run
并直接在主线程中执行操作,或者检查是否有其他线程操作干扰。
- 虽然代码中使用了
-
异常处理不充分:
- 当前的
try...catch
块可能无法捕获所有异常,特别是与 UI 相关的异常。可以尝试添加更详细的日志记录,以便更好地定位问题。
- 当前的
替代方案示例
以下是使用 dialogs.build()
的替代方案,避免使用 rawInput
:
// 文档地址 https://docs.hamibot.com/
function showKeywordDialog() {
// 1. 检查是否已在显示对话框(避免重复弹出)
if (typeof isKeywordDialogShowing !== 'boolean' || isKeywordDialogShowing) {
return;
}
isKeywordDialogShowing = true;
// 2. 在主线程中处理 UI 操作(避免子线程问题)
ui.run(() => {
try {
// 3. 获取默认值(确保 currentConfig 已初始化)
let defaultValue;
if (typeof currentConfig === 'object' && currentConfig !== null && Array.isArray(currentConfig.keyword)) {
defaultValue = currentConfig.keyword.join(", ");
} else {
defaultValue = "星星人, 前方高能";
currentConfig = { keyword: defaultValue.split(", ") }; // 初始化默认值
}
// 4. 使用 dialogs.build 创建输入对话框
var input = threads.disposable();
dialogs.build({
title: "修改商品关键词",
inputPrefill: defaultValue,
positive: "确定",
negative: "取消"
}).on("input", (text) => {
input.setAndNotify(text);
}).show();
// 5. 获取用户输入
let inputText = input.blockedGet();
if (inputText === null) {
console.log("❌ 用户取消了输入");
return;
}
// 6. 安全处理输入内容
let newKeywords = String(inputText)
.split(/\s*,\s*/)
.map(k => k.trim())
.filter(k => k !== "");
// 7. 更新 currentConfig(确保线程安全)
if (!currentConfig) {
currentConfig = {};
}
currentConfig.keyword = newKeywords;
console.log("✅ 商品关键词已更新:", newKeywords);
toast("关键词更新成功!");
} catch (e) {
console.error("❌ 修改关键词时出错:", e);
toast("修改关键词失败: " + e.message);
} finally {
isKeywordDialogShowing = false;
}
});
}
建议
- 确保
currentConfig
在使用前已正确初始化。 - 使用
dialogs.build()
替代rawInput
以避免潜在的兼容性问题。 - 添加更多日志记录,以便更好地定位问题。
如果问题仍然存在,建议检查 Hamibot 的官方文档或联系支持团队获取进一步帮助。
ℹ️ 本回答由 AI 生成,内容仅供参考