2026-01-11 17:50:27 +08:00

64 lines
1.8 KiB
JavaScript

import { createPanel } from './scripts/panel.js';
import { handleCommand, COMMANDS } from './scripts/commands.js';
import { initVoice } from './scripts/voice.js';
import { translateToCommand } from './scripts/ai.js';
// 等待页面加载完成
const init = async () => {
console.log("🚀 插件主程序启动...");
const ui = createPanel();
async function startProcess(text) {
const rawText = text.trim();
if (!rawText) return;
console.log("📩 接收到输入:", rawText);
// 1. 本地匹配逻辑
const localMatch = COMMANDS.find(c => rawText.includes(c.key));
if (localMatch) {
console.log("🎯 本地关键词匹配成功:", localMatch.key);
handleCommand(localMatch.key);
return;
}
// 2. AI 翻译逻辑
ui.setLoading(true);
try {
const aiResult = await translateToCommand(rawText);
if (aiResult) {
handleCommand(aiResult);
} else {
console.warn("❌ AI 无法理解该指令");
}
} catch (err) {
console.error("AI 流程出错:", err);
} finally {
ui.setLoading(false);
}
}
// 输入框回车事件监听
ui.input.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
e.preventDefault(); // 防止某些页面表单提交刷新
const val = ui.input.value;
ui.input.value = "";
startProcess(val);
}
});
// 语音识别初始化
// 修正:确保 voice.js 里的 handleCommand 回调指向我们的 startProcess
initVoice(document.getElementById("automation-ai-panel"), (spokenText) => {
ui.input.value = spokenText;
startProcess(spokenText);
});
};
// 确保在 DOM 完成后运行
if (document.readyState === "complete" || document.readyState === "interactive") {
init();
} else {
window.addEventListener("DOMContentLoaded", init);
}