var ttsInterval;
(function() {
// Ensure DOM is loaded
function initTTS() {
var btn = document.getElementById('ttsPlayBtn');
if (!btn) return;
btn.addEventListener('click', function() {
var content = document.querySelector('.entry-content') || document.querySelector('.post-content');
if (!content) {
alert('Post content not found');
return;
}
var text = content.innerText.trim();
if (!text) return;
// Stop previous speech
if (window.speechSynthesis.speaking) {
window.speechSynthesis.cancel();
}
var utterance = new SpeechSynthesisUtterance(text);
utterance.lang = 'hi-IN';
utterance.rate = 1;
utterance.pitch = 1;
window.speechSynthesis.speak(utterance);
});
}
// Old Elementor may need small timeout for DOM
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initTTS);
} else {
setTimeout(initTTS, 300);
}
})();
Click to listen highlighted text!