I used webkitSpeechRecognition for voice controlled navigation on my website. It works perfectly fine on Google Chrome, but on other browsers such as Edge and Opera the website will actually break.
How can I run the code responsible for firing this feature only if the website is displayed on a Chrome Browser ?
This is the code responsible for the feature:
SpeechRecognition.js
const SpeechRecognition =
window.SpeechRecognition || window.webkitSpeechRecognition;
export const recognition = new SpeechRecognition();
recognition.start();
Main.js
const history = useHistory();
recognition.onresult = (event) => {
const command = event.results[0][0].transcript;
if (command.includes("navigate to") || command.includes("go to")) {
if (command.includes("home") || command.includes("index")) {
history.push("/home");
} else if (command.includes("education") || command.includes("index")) {
history.push("/education");
} else if (command.includes("experience") || command.includes("index")) {
history.push("/experience");
} else if (command.includes("contact") || command.includes("index")) {
history.push("/contact");
} else if (command.includes("projects") || command.includes("index")) {
history.push("/projects");
} else if (command.includes("hobbies") || command.includes("index")) {
history.push("/hobbies");
}
} else if (command.includes("go back")) {
history.goBack();
}
};
recognition.onend = () => {
recognition.start();
};
I tried surrounding the code with the following both in Main.js and SpeechRecognition.js:
const isChromeBrowser = /Chrome/.test(navigator.userAgent)
&& /Google Inc/.test(navigator.userAgent);
if (isChromeBrowser) { // Code Here ... }
This still doesn't fix the issue, opening the web app in Opera gives a blank page.
Can anyone help with this ?