Skip to content

Instantly share code, notes, and snippets.

@LewisGet
Created May 1, 2023 14:00
Show Gist options
  • Save LewisGet/fc368e5a1d5d0d6f36d268f7d4cd1001 to your computer and use it in GitHub Desktop.
Save LewisGet/fc368e5a1d5d0d6f36d268f7d4cd1001 to your computer and use it in GitHub Desktop.
// 取得頁面上的初始文字
var targetDom = document.querySelector("main");
var initialText = targetDom.innerText;
// 設定一個定時器,定期檢查頁面上的文字是否有變化
setInterval(function() {
// 取得目前的文字
var currentText = targetDom.innerText;
// 比較目前的文字和初始文字是否有差異
if (currentText !== initialText) {
// 如果有差異,則找出新增的文字
var addedText = currentText.replace(initialText, '');
// 更新初始文字
initialText = currentText;
// 將新增的文字傳給 WebKit 的 Text-to-Speech 引擎
var synth = window.speechSynthesis;
var utterance = new SpeechSynthesisUtterance(addedText);
synth.speak(utterance);
}
}, 1000); // 每秒檢查一次
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment