From 37e098ecdfaa47e67e9cfbd53f15bd041b210eea Mon Sep 17 00:00:00 2001 From: balex Date: Sun, 1 Mar 2026 13:38:18 +0100 Subject: [PATCH] max words and sec --- src/public/index.html | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/public/index.html b/src/public/index.html index c968253..af1a827 100644 --- a/src/public/index.html +++ b/src/public/index.html @@ -866,6 +866,8 @@ speakBtn.addEventListener('click', () => { // ── Speech recognition ──────────────────────────────────────────────────────── const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition; +const MAX_RECORD_SECONDS = 10; +const MAX_RECORD_WORDS = 30; function initRecognition() { if (!SpeechRecognition) return null; @@ -895,6 +897,10 @@ function initRecognition() { interim = t; } } + if (finalText.trim().split(/\s+/).length >= MAX_RECORD_WORDS) { + stopRecording(); + return; + } const display = finalText + (interim ? ' ' + interim : ''); updateTranscriptBox(display, !!interim); state.transcript = finalText; @@ -903,8 +909,10 @@ function initRecognition() { r.onend = () => { if (state.isRecording) { - // auto-restart if not manually stopped - try { r.start(); } catch (_) {} + // create a fresh instance to avoid duplicate results on restart + const fresh = initRecognition(); + recognition = fresh; + try { fresh.start(); } catch (_) {} } }; @@ -917,6 +925,8 @@ function initRecognition() { return r; } +let recordingTimer = null; + function startRecording() { if (!SpeechRecognition) { alert('Spracherkennung wird in diesem Browser nicht unterstützt. Bitte nutze Chrome oder Edge.'); @@ -926,9 +936,13 @@ function startRecording() { recognition = initRecognition(); state.isRecording = true; try { recognition.start(); } catch (_) {} + + clearTimeout(recordingTimer); + recordingTimer = setTimeout(() => stopRecording(), MAX_RECORD_SECONDS * 1000); } function stopRecording() { + clearTimeout(recordingTimer); state.isRecording = false; if (recognition) { try { recognition.stop(); } catch (_) {}