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
的操作有关。以下是一些可能的问题和修复建议:
1. currentConfig
未正确初始化
在代码中,currentConfig
可能未被正确初始化,导致后续操作时出现错误。例如:
if (typeof currentConfig === 'object' && currentConfig !== null && Array.isArray(currentConfig.keyword)) {
defaultValue = currentConfig.keyword.join(", ");
} else {
defaultValue = "星星人, 前方高能";
currentConfig = { keyword: defaultValue.split(", ") }; // 初始化默认值
}
问题:如果 currentConfig
未定义,typeof currentConfig
会返回 "undefined"
,导致进入 else
分支。但在 else
分支中直接赋值 currentConfig = { keyword: ... }
,这可能会导致 currentConfig
在其他地方未被正确引用。
修复:确保 currentConfig
在脚本开始时被初始化为一个对象。
// 确保 currentConfig 已初始化
if (typeof currentConfig === 'undefined') {
currentConfig = {};
}
2. currentConfig.keyword
未正确设置
在 else
分支中,currentConfig
被赋值为 { keyword: defaultValue.split(", ") }
,但后续代码中可能直接使用了 currentConfig.keyword
,而 currentConfig
可能未被正确初始化。
修复:确保 currentConfig
是一个对象,并且 keyword
属性存在。
// 初始化 currentConfig
if (typeof currentConfig === 'undefined') {
currentConfig = {};
}
if (!currentConfig.keyword) {
currentConfig.keyword = [];
}
3. input.blockedGet()
的使用
input.blockedGet()
是一个阻塞方法,用于等待用户输入。如果在非主线程中调用,可能会导致问题。
修复:确保 input.blockedGet()
在主线程中调用,避免多线程问题。
// 在主线程中调用 input.blockedGet()
ui.run(() => {
// ...
let inputText = input.blockedGet();
// ...
});
4. threads.disposable()
的使用
threads.disposable()
用于创建一个可释放的线程对象,确保在获取用户输入后正确释放资源。
修复:确保 input
对象在使用后被正确释放。
var input = threads.disposable();
// ...
input.release(); // 在不需要时释放资源
5. dialogs.build()
的使用
dialogs.build()
用于构建对话框,确保对话框的配置正确。
修复:检查 dialogs.build()
的参数是否正确。
dialogs.build({
title: "修改商品关键词",
inputPrefill: defaultValue,
positive: "确定",
negative: "取消"
}).on("input", (text) => {
input.setAndNotify(text);
}).show();
总结
确保 currentConfig
正确初始化,并且在使用 currentConfig.keyword
时不会出现 undefined
。同时,确保所有 UI 操作在主线程中执行,并正确释放资源。以下是修复后的代码示例:
function showKeywordDialog() {
// 1. 检查是否已在显示对话框(避免重复弹出)
if (typeof isKeywordDialogShowing !== 'boolean' || isKeywordDialogShowing) {
return;
}
isKeywordDialogShowing = true;
// 2. 在主线程中处理 UI 操作(避免子线程问题)
ui.run(() => {
try {
// 3. 确保 currentConfig 已初始化
if (typeof currentConfig === 'undefined') {
currentConfig = {};
}
if (!currentConfig.keyword) {
currentConfig.keyword = [];
}
// 4. 获取默认值
let defaultValue = currentConfig.keyword.join(", ") || "星星人, 前方高能";
// 5. 使用 dialogs.build 创建输入对话框
var input = threads.disposable();
dialogs.build({
title: "修改商品关键词",
inputPrefill: defaultValue,
positive: "确定",
negative: "取消"
}).on("input", (text) => {
input.setAndNotify(text);
}).show();
// 6. 获取用户输入
let inputText = input.blockedGet();
if (inputText === null) {
console.log("❌ 用户取消了输入");
return;
}
// 7.