2026-01-02 21:06:54 +08:00
|
|
|
import { createPanel } from './panel.js';
|
2026-01-11 12:22:49 +08:00
|
|
|
import { handleCommand, COMMANDS } from './commands.js';
|
2026-01-02 21:06:54 +08:00
|
|
|
import { initVoice } from './voice.js';
|
2026-01-11 12:22:49 +08:00
|
|
|
import { translateToCommand } from './ai.js';
|
2026-01-02 21:06:54 +08:00
|
|
|
|
2026-01-11 12:22:49 +08:00
|
|
|
// 等待页面加载完成
|
|
|
|
|
const init = async () => {
|
|
|
|
|
console.log("🚀 插件主程序启动...");
|
|
|
|
|
const ui = createPanel();
|
2026-01-02 21:06:54 +08:00
|
|
|
|
2026-01-11 12:22:49 +08:00
|
|
|
async function startProcess(text) {
|
|
|
|
|
const rawText = text.trim();
|
|
|
|
|
if (!rawText) return;
|
2026-01-02 21:06:54 +08:00
|
|
|
|
2026-01-11 12:22:49 +08:00
|
|
|
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);
|
2026-01-02 21:06:54 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-11 12:22:49 +08:00
|
|
|
// 输入框回车事件监听
|
|
|
|
|
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);
|
|
|
|
|
}
|