diff --git a/content.js b/content.js new file mode 100644 index 0000000..c451ca1 --- /dev/null +++ b/content.js @@ -0,0 +1,130 @@ +console.log("✅ 语音 + 文字插件已运行"); + +// ========= 1. 浏览器能力检测 ========= +const SpeechRecognition = + window.SpeechRecognition || window.webkitSpeechRecognition; + +const supportSpeech = !!SpeechRecognition; + +// ========= 2. 创建控制面板 ========= +const panel = document.createElement("div"); +panel.style.cssText = ` + position: fixed; + bottom: 24px; + right: 24px; + z-index: 999999; + background: white; + border-radius: 10px; + box-shadow: 0 4px 12px rgba(0,0,0,.15); + padding: 10px; + width: 220px; + font-size: 14px; +`; + +panel.innerHTML = ` +
+ + +
+
+ 支持语音 / 回车文字指令 +
+`; + +document.body.appendChild(panel); + +// ========= 3. 父菜单展开函数 ========= +function expandParentMenu(span) { + const subMenu = span.closest(".el-sub-menu"); + if (subMenu) { + const title = subMenu.querySelector(".el-sub-menu__title"); + if (title && !subMenu.classList.contains("is-opened")) { + title.click(); // 展开父菜单 + console.log("📂 已展开父菜单"); + } + } +} + +// ========= 4. 路由跳转函数 ========= +function goToRoute(route) { + // 直接修改 hash + window.location.hash = route; + console.log("✅ 已跳转到路由:", route); +} + +// ========= 5. 指令解析(文字/语音共用) ========= +function handleCommand(text) { + console.log("📥 收到指令:", text); + + // 找菜单 DOM + const span = [...document.querySelectorAll("span")] + .find(el => el.innerText.trim() === text); + + if (text.includes("添加设备")) { + // 展开父菜单(如果存在) + span && expandParentMenu(span); + // 路由跳转 + goToRoute("/添加设备/添加设备"); + return; + } + + if (text.includes("设备")) { + span && expandParentMenu(span); + goToRoute("/设备管理"); + return; + } + + alert("未识别指令:" + text); +} + +// ========= 6. 文字输入支持 ========= +const input = panel.querySelector("#voiceTextInput"); + +input.addEventListener("keydown", (e) => { + if (e.key === "Enter") { + const value = input.value.trim(); + if (value) { + handleCommand(value); + input.value = ""; + } + } +}); + +// ========= 7. 语音识别支持 ========= +let recognition; +if (supportSpeech) { + recognition = new SpeechRecognition(); + recognition.lang = "zh-CN"; + recognition.continuous = false; + recognition.interimResults = false; + + recognition.onresult = (event) => { + const text = event.results[0][0].transcript.trim(); + handleCommand(text); + }; + + recognition.onerror = (event) => { + console.error("语音识别错误", event.error); + }; + + panel.querySelector("#voiceBtn").onclick = () => { + recognition.start(); + }; +} else { + panel.querySelector("#voiceBtn").disabled = true; + panel.querySelector("#voiceBtn").innerText = "❌"; +} + +// ========= 8. 快捷键 Alt+V 支持语音 ========= +document.addEventListener("keydown", (e) => { + if (e.altKey && e.key.toLowerCase() === "v") { + if (supportSpeech && recognition) recognition.start(); + } +}); diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..e010519 --- /dev/null +++ b/manifest.json @@ -0,0 +1,12 @@ +{ + "manifest_version": 3, + "name": "语音界面跳转", + "version": "1.0", + "content_scripts": [ + { + "matches": ["*://1718cloud.com/*"], + "js": ["content.js"] + } + ], + "permissions": ["activeTab"] +}