0

I am trying to create an extension that takes the audio from a tab and outputs text using webkitSpeechRecognition.

However, for some reason whenever I try to use speech recognition it errors with the error being "not-allowed". My current code is inside of the "service_worker" script:

window.SpeechRecognition = window.webkitSpeechRecognition || window.SpeechRecognition
const recognition = new SpeechRecognition()
recognition.start()
recognition.onresult = (event) => {
    const speechToText = event.results[0][0].transcript
    
    console.log(speechToText)
}
recognition.onerror = (event) => {
    console.error(event)
}

Why does it error, and how can I fix it?

  • Do it in a visible page like the action popup or inside a [web_accessible_resources iframe](https://stackoverflow.com/a/25100953) on the web page. – wOxxOm Mar 26 '21 at 12:31
  • I'm sorry, I must not be reading it correctly. How exactly do I do it inside a web_accessible_resources iframe? I don't want the user to have to keep the popup open. – MrSprinkleToes Mar 26 '21 at 12:41
  • The page inside the iframe will be like any extension page (like the action popup) so you can do anything you want there. – wOxxOm Mar 26 '21 at 12:43
  • But what do you mean by web_accessible_resources? Is there something I need to put in the manifest? Or is it just a regular iframe added to the web page? – MrSprinkleToes Mar 26 '21 at 12:45
  • Okay, I've created an iframe with the speechrecognition code & added it to web_accessible_resources. It added it to the page correctly, however the same error occurs again. – MrSprinkleToes Mar 26 '21 at 13:00
  • Looks like Chrome wants you to use this API inside a so-called user gesture i.e. inside a listener for an event like `mousedown`, `click`, `keypress`, etc. – wOxxOm Mar 26 '21 at 13:02
  • Thank you! It turns out that was the issue the whole time. Works now! – MrSprinkleToes Mar 26 '21 at 13:22
  • Hi MrSprinkleToes and @wOxxOm could you explain this more? I'm executing this via onClick in the React, and it doesn't work! Could you give me more information on how you solved it? – Maulik Suchak Sep 01 '23 at 20:45

0 Answers0