Repository navigation
Expand file tree
/
Copy pathchrome_proto.js
More file actions
113 lines (97 loc) · 3.09 KB
/
Copy pathchrome_proto.js
File metadata and controls
113 lines (97 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const SR = window.SpeechRecognition || window.webkitSpeechRecognition;
const dot = document.getElementById('engine-dot');
const engineTxt = document.getElementById('engine-txt');
const talk = document.getElementById('talk');
const heard = document.getElementById('heard');
const textIn = document.getElementById('textIn'); // Added missing reference
const plan = document.getElementById('plan');
const gate = document.getElementById('gate');
let rec = null;
// 'idle' | 'listening' | 'stopping'. Tracks the real async lifecycle of the
// recognition session instead of a plain boolean, so a fast tap can't call
// start() again before the previous session has actually torn down — that
// race is what makes Chrome misreport a fresh mic prompt (onerror 'not-allowed')
// even though permission was already granted.
let state = 'idle';
let restartRequested = false;
function listen() {
if (!rec) return;
if (state === 'idle') {
try {
rec.start();
state = 'listening';
talk.textContent = 'Listening...';
} catch (err) {
console.warn('Recognition start error:', err);
}
} else if (state === 'stopping') {
// Button pressed again before the previous session finished closing;
// start it once onend confirms the mic is actually free.
restartRequested = true;
}
}
function stopTalk() {
if (!rec || state !== 'listening') return;
state = 'stopping';
restartRequested = false;
rec.stop();
talk.textContent = 'Hold to talk';
}
function parse(text) {
// Simple command parser logic
const cleaned = text.toLowerCase().trim();
return {
raw: text,
intent: cleaned.includes('open') ? 'NAVIGATION' : 'UNKNOWN',
action: cleaned
};
}
function render(planObj) {
plan.innerHTML = `<p><strong>Plan Intent:</strong> ${planObj.intent}</p>`;
}
function run(planObj) {
gate.innerHTML = `<p><strong>Executing:</strong> "${planObj.action}"</p>`;
}
// Check SpeechRecognition Support
if (SR) {
rec = new SR();
rec.continuous = true;
rec.interimResults = true;
rec.lang = 'en-US';
dot.classList.add('live');
engineTxt.textContent = 'webspeech ready';
rec.onresult = (e) => {
let txt = '';
for (let i = 0; i < e.results.length; i++) {
txt += e.results[i][0].transcript;
}
// Update transcript elements
if (textIn) textIn.value = txt;
heard.textContent = txt;
// Process pipeline
const parsedData = parse(txt);
render(parsedData);
run(parsedData);
};
rec.onerror = (ev) => {
engineTxt.textContent = 'mic: ' + ev.error;
stopTalk();
};
rec.onend = () => {
state = 'idle';
talk.textContent = 'Hold to talk';
if (restartRequested) {
restartRequested = false;
listen();
}
};
// Push-to-Talk Event Listeners (Mouse & Touch)
talk.addEventListener('mousedown', listen);
talk.addEventListener('mouseup', stopTalk);
talk.addEventListener('mouseleave', stopTalk);
talk.addEventListener('touchstart', (e) => { e.preventDefault(); listen(); });
talk.addEventListener('touchend', stopTalk);
} else {
engineTxt.textContent = 'no mic — type instead';
talk.disabled = true;
}