Skip to content
document.addEventListener('DOMContentLoaded', () => {
const searchInput = document.querySelector('.search-field'); // Default Astra search input
const voiceSearchBtn = document.getElementById('voiceSearchBtn');// Check for Web Speech API support
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;if (SpeechRecognition) {
const recognition = new SpeechRecognition();
recognition.lang = 'en-US'; // Set language (modify as needed, e.g., 'hi-IN' for Hindi)
recognition.interimResults = false;recognition.onresult = (event) => {
const transcript = event.results[0][0].transcript; // Recognized text
searchInput.value = transcript; // Populate the search field// Trigger the WordPress search functionality
const searchEvent = new Event('input');
searchInput.dispatchEvent(searchEvent);
};recognition.onerror = (event) => {
alert('Error with voice search. Please try again.');
console.error('Speech recognition error:', event.error);
};// Start recognition when the button is clicked
voiceSearchBtn.addEventListener('click', () => {
recognition.start();
});
} else {
alert('Your browser does not support voice search. Please use Google Chrome.');
}
});