const fs = require('fs'); const path = require('path'); const jsPath = path.join(__dirname, 'assets/js/main.js'); let jsCode = fs.readFileSync(jsPath, 'utf8'); // Replace the icon logic jsCode = jsCode.replace( /audioBtn\.innerHTML = '<\/i>';/g, `audioBtn.innerHTML = '';` ).replace( /audioBtn\.innerHTML = '<\/i>';/g, `audioBtn.innerHTML = '';` ); // Add logic to test sound when audio is enabled const repl1 = `if (nextState) { initAudio(); if ('speechSynthesis' in window) { const primeUtterance = new SpeechSynthesisUtterance(''); window.speechSynthesis.speak(primeUtterance); } }`; const repl2 = `if (nextState) { initAudio(); if ('speechSynthesis' in window) { const primeUtterance = new SpeechSynthesisUtterance(''); window.speechSynthesis.speak(primeUtterance); } // Force latest announcement to replay const cards = Array.from(document.querySelectorAll('.announcement-card')); const latest = cards[0]; if (latest) { const storageKey = `hospitalQueue:lastAnnouncement:${locale} `; window.localStorage.removeItem(storageKey); setTimeout(checkAnnouncements, 200); } }`; jsCode = jsCode.replace(repl1, repl2); // Make sure speech synthesis gets voices early, and handle fallback properly const oldVoiceLogic = `if (langVoices.length > 0) { // Try to find a high-quality (Google/Microsoft Natural) voice const bestVoice = langVoices.find(v => v.name.includes('Google') || v.name.includes('Natural') || v.name.includes('Premium') || v.name.includes('Online') ) || langVoices.find(v => v.name.includes('Microsoft')) || langVoices[0]; utterance.voice = bestVoice; }`; const newVoiceLogic = `if (langVoices.length > 0) { // Try to find a high-quality (Google/Microsoft Natural) voice const bestVoice = langVoices.find(v => v.name.includes('Google') || v.name.includes('Natural') || v.name.includes('Premium') || v.name.includes('Online') ) || langVoices.find(v => v.name.includes('Microsoft')) || langVoices[0]; utterance.voice = bestVoice; } else if (voices.length > 0) { // Fallback to any available voice to ensure sound plays utterance.voice = voices[0]; }`; jsCode = jsCode.replace(oldVoiceLogic, newVoiceLogic); // Call getVoices early if (!jsCode.includes('window.speechSynthesis.getVoices();')) { jsCode = jsCode.replace( `document.addEventListener('DOMContentLoaded', () => {`, `document.addEventListener('DOMContentLoaded', () => { if ('speechSynthesis' in window) { window.speechSynthesis.onvoiceschanged = () => { window.speechSynthesis.getVoices(); }; window.speechSynthesis.getVoices(); }` ); } fs.writeFileSync(jsPath, jsCode); console.log('patched');