Created
May 1, 2023 14:00
-
-
Save LewisGet/fc368e5a1d5d0d6f36d268f7d4cd1001 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 取得頁面上的初始文字 | |
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