2022-06-14 16:24:05 +08:00
|
|
|
let isRead = true;
|
|
|
|
let isChoose = true;
|
|
|
|
let reader = window.speechSynthesis; // 全局定义朗读者,以便朗读和暂停
|
2022-01-26 21:10:09 +08:00
|
|
|
|
2022-06-14 16:24:05 +08:00
|
|
|
function getWord() {
|
|
|
|
return window.getSelection ? window.getSelection() : document.selection.createRange().text;
|
2022-01-26 21:10:09 +08:00
|
|
|
}
|
2022-06-14 16:24:05 +08:00
|
|
|
|
|
|
|
function fillInWord() {
|
|
|
|
let word = getWord();
|
|
|
|
if (isRead) read(word);
|
|
|
|
if (!isChoose) return;
|
|
|
|
const element = document.getElementById("selected-words");
|
|
|
|
element.value = element.value + " " + word;
|
2022-01-26 21:10:09 +08:00
|
|
|
}
|
2022-06-14 16:24:05 +08:00
|
|
|
|
|
|
|
document.getElementById("text-content").addEventListener("click", fillInWord, false);
|
|
|
|
|
|
|
|
function read(s) {
|
|
|
|
let msg = new SpeechSynthesisUtterance(s);
|
|
|
|
reader.speak(msg);
|
2022-01-26 21:10:09 +08:00
|
|
|
}
|
2022-06-14 16:24:05 +08:00
|
|
|
|
|
|
|
|
|
|
|
function onReadClick() {
|
2022-01-26 21:10:09 +08:00
|
|
|
isRead = !isRead;
|
2022-06-14 16:24:05 +08:00
|
|
|
if (!isRead) {
|
|
|
|
reader.cancel();
|
2022-01-26 21:10:09 +08:00
|
|
|
}
|
|
|
|
}
|
2022-06-14 16:24:05 +08:00
|
|
|
|
|
|
|
function onChooseClick() {
|
2022-01-26 21:10:09 +08:00
|
|
|
isChoose = !isChoose;
|
|
|
|
}
|