Confucius did not know that "he is a Heraclite" once said: "All flows"!
*&*
(Code 📚 read text English or Vietnamese)
['vi.VN'] ['eng.ENG']
*&*
<!-- THANH CÔNG CỤ ĐỌC BÀI VIẾT TỰ ĐỘNG -->
<div style="background-color: #f4f6f9; border-left: 4px solid #007bff; padding: 12px 16px; border-radius: 4px; display: flex; align-items: center; margin-bottom: 20px;">
<!-- Nút Bấm Đọc / Dừng -->
<button id="play-tts-btn" onclick="toggleSpeech()" style="background-color: #007bff; color: white; border: none; border-radius: 50%; width: 40px; height: 40px; cursor: pointer; display: flex; align-items: center; justify-content: center; margin-right: 12px; font-size: 18px; font-weight: bold;">
▶
</button>
<!-- Trạng thái hiển thị -->
<div>
<span id="tts-status" style="font-weight: bold; color: #333; display: block; font-size: 14px;">Nghe bài viết này</span>
<span style="font-size: 12px; color: #666;">Giọng đọc tự động hệ thống</span>
</div>
</div>
<!-- ĐẦU VÙNG ĐỌC: Chứa toàn bộ nội dung bài viết của bạn -->
<div id="post-content">
<!-- Bạn dán phần hình ảnh và toàn bộ đoạn văn bản màu xanh của bạn ở đây -->
<p style="color: blue;">___ Xin chào. Tôi quan tâm về quy chế xét duyệt rồi trao giải thưởng Nobel Hòa Bình...</p>
</div>
<!-- CUỐI VÙNG ĐỌC -->
<!-- ĐOẠN CODE XỬ LÝ LỆNH ĐỌC (BẮT BUỘC PHẢI CÓ) -->
<script>
var speechUtterance = null;
var isSpeaking = false;
function toggleSpeech() {
var btn = document.getElementById('play-tts-btn');
var statusText = document.getElementById('tts-status');
// Nếu đang đọc thì bấm vào sẽ dừng lại
if (isSpeaking) {
window.speechSynthesis.cancel();
btn.innerHTML = "▶";
statusText.innerHTML = "Nghe bài viết này";
isSpeaking = false;
return;
}
// Lấy toàn bộ văn bản trong vùng post-content
var textToRead = document.getElementById('post-content').innerText;
if (!textToRead || textToRead.trim() === "") {
alert("Không tìm thấy nội dung để đọc!");
return;
}
// Khởi tạo giọng đọc
speechUtterance = new SpeechSynthesisUtterance(textToRead);
speechUtterance.lang = 'vi-VN'; // Thiết lập đọc tiếng Việt
// Khi đọc xong tự động chuyển nút về ban đầu
speechUtterance.onend = function() {
btn.innerHTML = "▶";
statusText.innerHTML = "Nghe bài viết này";
isSpeaking = false;
};
// Khi có lỗi xảy ra
speechUtterance.onerror = function() {
btn.innerHTML = "▶";
statusText.innerHTML = "Nghe bài viết này";
isSpeaking = false;
};
// Bắt đầu đọc
window.speechSynthesis.cancel(); // Xóa các lệnh đọc cũ xếp hàng nếu có
window.speechSynthesis.speak(speechUtterance);
// Đổi giao diện nút thành Pause
btn.innerHTML = "⏸";
statusText.innerHTML = "Đang đọc bài viết...";
isSpeaking = true;
}
</script>