From d431076623ec760e69a023836af5406d30a9f732 Mon Sep 17 00:00:00 2001 From: Cx330 <1487537121@qq.com> Date: Fri, 2 Jan 2026 20:28:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8C=87=E4=BB=A4=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- content.js | 47 ++++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/content.js b/content.js index c451ca1..abd8141 100644 --- a/content.js +++ b/content.js @@ -46,7 +46,7 @@ function expandParentMenu(span) { if (subMenu) { const title = subMenu.querySelector(".el-sub-menu__title"); if (title && !subMenu.classList.contains("is-opened")) { - title.click(); // 展开父菜单 + title.click(); console.log("📂 已展开父菜单"); } } @@ -54,39 +54,40 @@ function expandParentMenu(span) { // ========= 4. 路由跳转函数 ========= function goToRoute(route) { - // 直接修改 hash window.location.hash = route; console.log("✅ 已跳转到路由:", route); } -// ========= 5. 指令解析(文字/语音共用) ========= +// ========= 5. 指令配置 ========= +const COMMANDS = [ + { key: "首页", menu: "首页", route: "/首页" }, + { key: "添加设备", menu: "添加设备", route: "/添加设备/添加设备" }, + { key: "监控中心", menu: "监控中心", route: "/监控中心/监控中心" }, +]; + +// ========= 6. 指令解析(文字 & 语音共用) ========= function handleCommand(text) { console.log("📥 收到指令:", text); - // 找菜单 DOM + const command = COMMANDS.find(c => text.includes(c.key)); + + if (!command) { + alert("未识别指令:" + text); + return; + } + + // 找菜单 DOM 并展开父菜单 const span = [...document.querySelectorAll("span")] - .find(el => el.innerText.trim() === text); + .find(el => el.innerText.trim() === command.menu); - if (text.includes("添加设备")) { - // 展开父菜单(如果存在) - span && expandParentMenu(span); - // 路由跳转 - goToRoute("/添加设备/添加设备"); - return; - } + span && expandParentMenu(span); - if (text.includes("设备")) { - span && expandParentMenu(span); - goToRoute("/设备管理"); - return; - } - - alert("未识别指令:" + text); + // 路由跳转 + goToRoute(command.route); } -// ========= 6. 文字输入支持 ========= +// ========= 7. 文字输入支持 ========= const input = panel.querySelector("#voiceTextInput"); - input.addEventListener("keydown", (e) => { if (e.key === "Enter") { const value = input.value.trim(); @@ -97,7 +98,7 @@ input.addEventListener("keydown", (e) => { } }); -// ========= 7. 语音识别支持 ========= +// ========= 8. 语音识别支持 ========= let recognition; if (supportSpeech) { recognition = new SpeechRecognition(); @@ -122,7 +123,7 @@ if (supportSpeech) { panel.querySelector("#voiceBtn").innerText = "❌"; } -// ========= 8. 快捷键 Alt+V 支持语音 ========= +// ========= 9. 快捷键 Alt+V 支持语音 ========= document.addEventListener("keydown", (e) => { if (e.altKey && e.key.toLowerCase() === "v") { if (supportSpeech && recognition) recognition.start();