Detect if another browser tab is using speechRecognition
Asked Answered
A

3

6

Is it possible to tell if another Chrome tab is using webkitSpeechRecognition?

If you try to use webkitSpeechRecognition while another tab is using it, it will throw an error "aborted" without any message. I want to be able to know if webkitSpeechRecognition is open in another tab, and if so, throw a better error that could notify the user.

Abutilon answered 8/7, 2016 at 7:43 Comment(0)
D
1

Unless your customer is on the same website(you could check by logging the ip/browserprint in database and requesting by json) you cannot do that.

Cross domain protection is in effect, and that lets you know zilch about what happens in other tabs or frames.

Dowell answered 8/7, 2016 at 7:47 Comment(2)
Well that sucks. I hope the browsers implement a better error message than just "aborted" for this case. Will leave the question open for another day and see if anyone else knows something different, if not I'll accept this answer - thanks MichaelAbutilon
Usually you can depend on as a rule of thumb: No specifics profivided? cross domain security issueDowell
T
0

I am using webkitSpeechRecognition for chrome ( does not work on FF) and I faced same issues like multiple Chrome tabs. Until the browser implement a better error message a temporary solutions that work for me:

  • You need to detect when a tab is focused or not in Chrome using Javascript.

  • Make javascript code like this

    isChromium = window.chrome; if(isChromium) { if (window.addEventListener) { // bind focus event window.addEventListener("focus", function (event) { console.log("Browser tab focus.."); recognition.stop();// to avoid error recognition.start(); }, false); window.addEventListener("blur", function (event) { console.log("Browser tab blur.."); recognition.stop(); }, false); } }

Triceratops answered 31/5, 2018 at 14:13 Comment(0)
A
0

There's a small workaround for it. You can store the timestamp in a variable upon activating SpeechRecognition and when it exits after a few seconds of inactivity, it will be compared to a timestamp since the SpeechRecognition was activated. Since two tabs are using the API simultaneously, it will exit immediately.

For Chrome, you can use the code below and modify it base on your needs. Firefox doesn't support this yet at the moment.

var transcriptionStartTime;
var timeSinceLastStart;

function configureRecognition(){
    var webkitSpeechRecognition = window.webkitSpeechRecognition || window.SpeechRecognition;
    if ('webkitSpeechRecognition' in window) {
        recognition = new webkitSpeechRecognition();
        recognition.continuous = true;
        recognition.interimResults = true;
        recognition.lang = "en-US";

        recognition.onend = function() {
            timeSinceLastStart = new Date().getTime() - transcriptionStartTime;

            if (timeSinceLastStart < 100) {
                alert('Speech recognition failed to start. Please close the tab that is currently using it.');
            }
        }
    }
}

See browser compatibility here: https://developer.mozilla.org/en-US/docs/Web/API/SpeechRecognition

Astrophysics answered 30/9, 2022 at 8:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.