From 7194c70d9cbc8923438c14d51b2cea9aea182b19 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Sat, 13 Apr 2024 21:04:32 +0200 Subject: Fix SpeechRecognition on Phone --- g4f/gui/client/static/js/chat.v1.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'g4f') diff --git a/g4f/gui/client/static/js/chat.v1.js b/g4f/gui/client/static/js/chat.v1.js index 8933b442..e4af281d 100644 --- a/g4f/gui/client/static/js/chat.v1.js +++ b/g4f/gui/client/static/js/chat.v1.js @@ -1074,7 +1074,7 @@ async function load_version() { } setTimeout(load_version, 2000); -for (const el of [imageInput, cameraInput]) { +[imageInput, cameraInput].forEach((el) => { el.addEventListener('click', async () => { el.value = ''; if (imageInput.dataset.src) { @@ -1082,7 +1082,7 @@ for (const el of [imageInput, cameraInput]) { delete imageInput.dataset.src } }); -} +}); fileInput.addEventListener('click', async (event) => { fileInput.value = ''; @@ -1261,9 +1261,11 @@ if (SpeechRecognition) { recognition.interimResults = true; recognition.maxAlternatives = 1; + let shouldStop = false; function may_stop() { if (microLabel.classList.contains("recognition")) { recognition.stop(); + shouldStop = true } } @@ -1277,8 +1279,12 @@ if (SpeechRecognition) { timeoutHandle = window.setTimeout(may_stop, 10000); }; recognition.onend = function() { - microLabel.classList.remove("recognition"); - messageInput.focus(); + if (shouldStop) { + microLabel.classList.remove("recognition"); + messageInput.focus(); + } else { + recognition.start(); + } }; recognition.onresult = function(event) { if (!event.results) { @@ -1310,10 +1316,12 @@ if (SpeechRecognition) { microLabel.addEventListener("click", () => { if (microLabel.classList.contains("recognition")) { window.clearTimeout(timeoutHandle); + shouldStop = true; recognition.stop(); } else { const lang = document.getElementById("recognition-language")?.value; recognition.lang = lang || navigator.language; + shouldStop = false; recognition.start(); } }); -- cgit v1.2.3 From c15242faa02847bc9cdcdd46a12611a73c2bba57 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Sat, 13 Apr 2024 21:05:01 +0200 Subject: Fix SpeechRecognition on Phone --- g4f/gui/client/static/js/chat.v1.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'g4f') diff --git a/g4f/gui/client/static/js/chat.v1.js b/g4f/gui/client/static/js/chat.v1.js index e4af281d..5784b05a 100644 --- a/g4f/gui/client/static/js/chat.v1.js +++ b/g4f/gui/client/static/js/chat.v1.js @@ -1264,8 +1264,8 @@ if (SpeechRecognition) { let shouldStop = false; function may_stop() { if (microLabel.classList.contains("recognition")) { - recognition.stop(); shouldStop = true + recognition.stop(); } } -- cgit v1.2.3 From 070eeadc12dde13c273cc12529541375c6875c7c Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Sat, 13 Apr 2024 21:15:31 +0200 Subject: Fix SpeechRecognition on Phone --- g4f/gui/client/static/js/chat.v1.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'g4f') diff --git a/g4f/gui/client/static/js/chat.v1.js b/g4f/gui/client/static/js/chat.v1.js index 5784b05a..9194ad4f 100644 --- a/g4f/gui/client/static/js/chat.v1.js +++ b/g4f/gui/client/static/js/chat.v1.js @@ -1287,10 +1287,11 @@ if (SpeechRecognition) { } }; recognition.onresult = function(event) { + window.clearTimeout(timeoutHandle); + timeoutHandle = window.setTimeout(may_stop, transcript ? 10000 : 8000); if (!event.results) { return; } - window.clearTimeout(timeoutHandle); let result = event.results[event.resultIndex]; let isFinal = result.isFinal && (result[0].confidence > 0); @@ -1309,8 +1310,6 @@ if (SpeechRecognition) { messageInput.style.height = messageInput.scrollHeight + "px"; messageInput.scrollTop = messageInput.scrollHeight; } - - timeoutHandle = window.setTimeout(may_stop, transcript ? 10000 : 8000); }; microLabel.addEventListener("click", () => { -- cgit v1.2.3 From 97d34592d51f29a44c56ad8c1d54c64a5aff6fac Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Sat, 13 Apr 2024 21:22:30 +0200 Subject: Fix SpeechRecognition on Phone --- g4f/gui/client/static/js/chat.v1.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'g4f') diff --git a/g4f/gui/client/static/js/chat.v1.js b/g4f/gui/client/static/js/chat.v1.js index 9194ad4f..4ba0cd93 100644 --- a/g4f/gui/client/static/js/chat.v1.js +++ b/g4f/gui/client/static/js/chat.v1.js @@ -1288,7 +1288,7 @@ if (SpeechRecognition) { }; recognition.onresult = function(event) { window.clearTimeout(timeoutHandle); - timeoutHandle = window.setTimeout(may_stop, transcript ? 10000 : 8000); + timeoutHandle = window.setTimeout(may_stop, 8000); if (!event.results) { return; } -- cgit v1.2.3 From f323e8850aaee3be3f98205d3b87d03ea485c1c2 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Sat, 13 Apr 2024 21:35:57 +0200 Subject: Fix SpeechRecognition on Phone --- g4f/gui/client/static/js/chat.v1.js | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) (limited to 'g4f') diff --git a/g4f/gui/client/static/js/chat.v1.js b/g4f/gui/client/static/js/chat.v1.js index 4ba0cd93..0f3c5d65 100644 --- a/g4f/gui/client/static/js/chat.v1.js +++ b/g4f/gui/client/static/js/chat.v1.js @@ -1261,38 +1261,27 @@ if (SpeechRecognition) { recognition.interimResults = true; recognition.maxAlternatives = 1; - let shouldStop = false; function may_stop() { if (microLabel.classList.contains("recognition")) { - shouldStop = true recognition.stop(); } } let startValue; - let timeoutHandle; let lastDebounceTranscript; recognition.onstart = function() { microLabel.classList.add("recognition"); startValue = messageInput.value; lastDebounceTranscript = ""; - timeoutHandle = window.setTimeout(may_stop, 10000); }; recognition.onend = function() { - if (shouldStop) { - microLabel.classList.remove("recognition"); - messageInput.focus(); - } else { - recognition.start(); - } + microLabel.classList.remove("recognition"); + messageInput.focus(); }; recognition.onresult = function(event) { - window.clearTimeout(timeoutHandle); - timeoutHandle = window.setTimeout(may_stop, 8000); if (!event.results) { return; } - let result = event.results[event.resultIndex]; let isFinal = result.isFinal && (result[0].confidence > 0); let transcript = result[0].transcript; @@ -1314,13 +1303,10 @@ if (SpeechRecognition) { microLabel.addEventListener("click", () => { if (microLabel.classList.contains("recognition")) { - window.clearTimeout(timeoutHandle); - shouldStop = true; recognition.stop(); } else { const lang = document.getElementById("recognition-language")?.value; recognition.lang = lang || navigator.language; - shouldStop = false; recognition.start(); } }); -- cgit v1.2.3 From cec2a10eaabe04b0cd7bac343e669f75150fce7c Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Sat, 13 Apr 2024 21:41:40 +0200 Subject: Fix SpeechRecognition on Phone --- g4f/gui/client/static/js/chat.v1.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'g4f') diff --git a/g4f/gui/client/static/js/chat.v1.js b/g4f/gui/client/static/js/chat.v1.js index 0f3c5d65..3da43d95 100644 --- a/g4f/gui/client/static/js/chat.v1.js +++ b/g4f/gui/client/static/js/chat.v1.js @@ -1261,22 +1261,22 @@ if (SpeechRecognition) { recognition.interimResults = true; recognition.maxAlternatives = 1; - function may_stop() { - if (microLabel.classList.contains("recognition")) { - recognition.stop(); - } - } - let startValue; + let shouldStop = false; let lastDebounceTranscript; recognition.onstart = function() { microLabel.classList.add("recognition"); startValue = messageInput.value; + shouldStop = false; lastDebounceTranscript = ""; }; recognition.onend = function() { - microLabel.classList.remove("recognition"); - messageInput.focus(); + if (shouldStop) { + microLabel.classList.remove("recognition"); + messageInput.focus(); + } else { + recognition.start(); + } }; recognition.onresult = function(event) { if (!event.results) { @@ -1303,6 +1303,7 @@ if (SpeechRecognition) { microLabel.addEventListener("click", () => { if (microLabel.classList.contains("recognition")) { + shouldStop = true; recognition.stop(); } else { const lang = document.getElementById("recognition-language")?.value; -- cgit v1.2.3 From 307ccd87818f525fdfb6cae70edca0dc689d0473 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Sat, 13 Apr 2024 21:47:22 +0200 Subject: Fix SpeechRecognition on Phone --- g4f/gui/client/static/js/chat.v1.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'g4f') diff --git a/g4f/gui/client/static/js/chat.v1.js b/g4f/gui/client/static/js/chat.v1.js index 3da43d95..7f4011a2 100644 --- a/g4f/gui/client/static/js/chat.v1.js +++ b/g4f/gui/client/static/js/chat.v1.js @@ -1262,7 +1262,7 @@ if (SpeechRecognition) { recognition.maxAlternatives = 1; let startValue; - let shouldStop = false; + let shouldStop; let lastDebounceTranscript; recognition.onstart = function() { microLabel.classList.add("recognition"); @@ -1272,7 +1272,6 @@ if (SpeechRecognition) { }; recognition.onend = function() { if (shouldStop) { - microLabel.classList.remove("recognition"); messageInput.focus(); } else { recognition.start(); @@ -1305,6 +1304,7 @@ if (SpeechRecognition) { if (microLabel.classList.contains("recognition")) { shouldStop = true; recognition.stop(); + microLabel.classList.remove("recognition"); } else { const lang = document.getElementById("recognition-language")?.value; recognition.lang = lang || navigator.language; -- cgit v1.2.3