showKeywordDialog: function() {
// 1. 使用全局变量避免作用域问题
if (global.isKeywordDialogShowing) {
return;
}
global.isKeywordDialogShowing = true;

// 2. 确保在主线程执行 UI 操作
ui.run(() => {
    try {
        // 3. 初始化默认值(更严格的检查)
        let defaultValue;
        if (typeof global.currentConfig === 'object' && 
            global.currentConfig !== null && 
            Array.isArray(global.currentConfig.keyword)) {
            defaultValue = global.currentConfig.keyword.join(", ");
        } else {
            defaultValue = "星星人, 前方高能";
            global.currentConfig = { keyword: defaultValue.split(", ") };
        }

        // 4. 使用 dialogs.input 替代 rawInput(更稳定)
        let inputText = dialogs.input("修改商品关键词", defaultValue, {
            inputType: "text",
            hint: "输入新关键词(用逗号分隔)"
        });

        // 5. 处理用户取消
        if (inputText === null) {
            console.log("❌ 用户取消了输入");
            return;
        }

        // 6. 安全处理输入内容
        let newKeywords = String(inputText)
            .split(/\s*,\s*/)       // 支持 "关键词1, 关键词2" 或 "关键词1,关键词2"
            .map(k => k.trim())     // 去除前后空格
            .filter(k => k !== ""); // 过滤空值

        // 7. 更新 global.currentConfig(确保线程安全)
        if (!global.currentConfig) {
            global.currentConfig = {};
        }
        global.currentConfig.keyword = newKeywords;
        console.log("✅ 商品关键词已更新:", newKeywords);
        toast("关键词更新成功!");

    } catch (e) {
        console.error("❌ 修改关键词时出错:", e);
        toast("修改关键词失败: " + e.message);
    } finally {
        global.isKeywordDialogShowing = false;
    }
});

},
运行后,点击确定,直接闪退hamibot

已存档
xv7j3ndpak · 1 个月前

回答

Hamibot AI
1
· 1 个月前

根据你提供的代码和问题描述,运行后点击确定直接闪退可能是因为代码中存在一些潜在的错误或不兼容的问题。以下是一些可能的原因和解决方案:

1. dialogs.input 的使用

  • dialogs.input 是一个用于显示输入对话框的方法,但在某些情况下可能与当前环境不兼容,尤其是在脚本中没有正确初始化或调用时。
  • 如果你使用的是 Hamibot 环境,建议使用 dialogs.build() 构建对话框,并通过事件处理来获取用户输入。

2. global.isKeywordDialogShowingglobal.currentConfig 的使用

  • 确保 global.isKeywordDialogShowingglobal.currentConfig 在脚本的其他部分已经被正确定义。
  • 如果这些全局变量未被正确初始化,可能导致脚本在执行时出现错误。

3. 代码中的错误处理

  • try...catch 块中,如果发生异常,可能会导致脚本终止。确保在 catch 块中正确处理异常,并避免脚本因未捕获的错误而崩溃。

4. UI 操作的线程问题

  • 使用 ui.run(() => { ... }) 是正确的做法,但需要确保 ui 对象已经正确初始化。

改进建议

以下是改进后的代码示例,使用 dialogs.build() 来构建对话框并处理用户输入:

// 文档地址 https://docs.hamibot.com/
function showKeywordDialog() {
    // 1. 使用全局变量避免作用域问题
    if (global.isKeywordDialogShowing) {
        return;
    }
    global.isKeywordDialogShowing = true;

    // 2. 确保在主线程执行 UI 操作
    ui.run(() => {
        try {
            // 3. 初始化默认值(更严格的检查)
            let defaultValue;
            if (typeof global.currentConfig === 'object' && 
                global.currentConfig !== null && 
                Array.isArray(global.currentConfig.keyword)) {
                defaultValue = global.currentConfig.keyword.join(", ");
            } else {
                defaultValue = "星星人, 前方高能";
                global.currentConfig = { keyword: defaultValue.split(", ") };
            }

            // 4. 使用 dialogs.build 构建对话框
            var input = threads.disposable();
            dialogs.build({
                title: "修改商品关键词",
                inputPrefill: defaultValue,
                hint: "输入新关键词(用逗号分隔)",
                positive: "确定",
                negative: "取消"
            }).on("input", function(text) {
                input.setAndNotify(text);
            }).on("positive", function(dialog) {
                // 5. 处理用户输入
                let newKeywords = String(text)
                    .split(/\s*,\s*/)       // 支持 "关键词1, 关键词2" 或 "关键词1,关键词2"
                    .map(k => k.trim())     // 去除前后空格
                    .filter(k => k !== ""); // 过滤空值

                // 6. 更新 global.currentConfig(确保线程安全)
                if (!global.currentConfig) {
                    global.currentConfig = {};
                }
                global.currentConfig.keyword = newKeywords;
                console.log("✅ 商品关键词已更新:", newKeywords);
                toast("关键词更新成功!");
            }).on("negative", function(dialog) {
                console.log("❌ 用户取消了输入");
            }).show();

        } catch (e) {
            console.error("❌ 修改关键词时出错:", e);
            toast("修改关键词失败: " + e.message);
        } finally {
            global.isKeywordDialogShowing = false;
        }
    });
}

其他建议

  • 调试:在脚本中添加更多的日志输出,以便了解脚本在哪个部分出现了问题。
  • 测试:在不同的环境中测试脚本,确保它在所有支持的环境中都能正常运行。

如果你仍然遇到问题,建议将完整的脚本代码发送到 support@hamibot.cn 获取进一步的帮助。

ℹ️ 本回答由 AI 生成,内容仅供参考