实现关键词语音或文字输入跳转界面
This commit is contained in:
parent
a803d467ae
commit
7923a945cd
130
content.js
Normal file
130
content.js
Normal file
@ -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 = `
|
||||||
|
<div style="display:flex;gap:6px;">
|
||||||
|
<input
|
||||||
|
id="voiceTextInput"
|
||||||
|
placeholder="输入指令,如:添加设备"
|
||||||
|
style="flex:1;padding:6px;border:1px solid #dcdfe6;border-radius:4px;"
|
||||||
|
/>
|
||||||
|
<button
|
||||||
|
id="voiceBtn"
|
||||||
|
style="padding:6px 10px;border:none;border-radius:4px;background:#409eff;color:#fff;cursor:pointer;"
|
||||||
|
>🎤</button>
|
||||||
|
</div>
|
||||||
|
<div style="margin-top:6px;color:#999;font-size:12px;">
|
||||||
|
支持语音 / 回车文字指令
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
});
|
||||||
12
manifest.json
Normal file
12
manifest.json
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
{
|
||||||
|
"manifest_version": 3,
|
||||||
|
"name": "语音界面跳转",
|
||||||
|
"version": "1.0",
|
||||||
|
"content_scripts": [
|
||||||
|
{
|
||||||
|
"matches": ["*://1718cloud.com/*"],
|
||||||
|
"js": ["content.js"]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"permissions": ["activeTab"]
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user