showKeywordDialog: function() {
if (isKeywordDialogShowing) return;
isKeywordDialogShowing = true;

ui.run(() => {
    try {
        // 处理显示逻辑(兼容字符串、数组、对象)
        let currentKeywordDisplay;
        if (Array.isArray(currentConfig.keyword)) {
            currentKeywordDisplay = currentConfig.keyword.join(", ");
        } else if (typeof currentConfig.keyword === "object" && currentConfig.keyword !== null) {
            currentKeywordDisplay = JSON.stringify(currentConfig.keyword);
        } else {
            currentKeywordDisplay = String(currentConfig.keyword || "");
        }

        let input = dialogs.rawInput("修改关键词配置", currentKeywordDisplay);

console.log("输入返回值:", JSON.stringify(input)); // 调试用

// ✅ 严格检查:仅当 input 是非空字符串时才更新
if (typeof input === "string" && input.trim() !== "") {
currentConfig.keyword = input.trim();
console.log("✅ 关键词已更新:", currentConfig.keyword);
} else if (input === null) {
console.log("⚠️ 用户取消输入");
} else {
console.log("⚠️ 输入无效,未更新");
}
} catch (e) {
console.error("关键词对话框错误:", e);
console.error(e.stack);
} finally {
isKeywordDialogShowing = false;
}
});
},
输入后直线显示输入无效,并且保存不了

已存档
xv7j3ndpak · 30 天前

回答

Hamibot AI
1
· 30 天前

文档地址 https://docs.hamibot.com/

根据你的代码逻辑,问题出在输入验证条件过于严格。当用户输入内容时,如果输入值是字符串类型但经过 .trim() 后为空(例如用户输入了纯空格),会导致条件判断失败,无法更新配置。

修改方案:

将判断条件改为仅检查类型是否为字符串即可,允许空字符串:

if (typeof input === "string") {
    currentConfig.keyword = input.trim(); // 保留trim操作
    console.log("✅ 关键词已更新:", currentConfig.keyword);
}

调试建议:

  1. console.log("输入返回值:", JSON.stringify(input)) 中查看实际输入值
  2. 如果需要完全禁止空输入,可修改提示逻辑:
if (typeof input === "string") {
    const trimmed = input.trim();
    if (trimmed === "") {
        toast("关键词不能为空");
        return;
    }
    currentConfig.keyword = trimmed;
    console.log("✅ 关键词已更新:", currentConfig.keyword);
}

兼容性说明:

  • typeof null 的返回值是 "object",所以原代码中的 else if (input === null) 无法被正确触发
  • 当用户点击取消时,dialogs.rawInput 返回值为 null,建议修改取消处理逻辑:
if (input === null) {
    console.log("⚠️ 用户取消输入");
    toast("操作已取消");
}
ℹ️ 本回答由 AI 生成,内容仅供参考