Skip to content

Instantly share code, notes, and snippets.

@tgfjt
Created March 7, 2023 08:14
Show Gist options
  • Save tgfjt/1ff78e36e1c16e8d9dfaf0ba781ee85e to your computer and use it in GitHub Desktop.
Save tgfjt/1ff78e36e1c16e8d9dfaf0ba781ee85e to your computer and use it in GitHub Desktop.
type SpeechRecognitionListener = (e: SpeechRecognitionEvent) => void;
export function createSpeechRecognition(
onResult: SpeechRecognitionListener,
): SpeechRecognition | undefined {
if (typeof window !== 'object') return;
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
if (typeof SpeechRecognition !== 'undefined') {
const recognition = new SpeechRecognition();
recognition.continuous = true;
recognition.addEventListener('result', onResult);
return recognition;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment