1
0
Fork 0
EnglishPal/app/static/js/fillword.js

34 lines
800 B
JavaScript
Raw Normal View History

2022-06-14 16:24:05 +08:00
let isRead = true;
let isChoose = true;
let reader = window.speechSynthesis; // 全局定义朗读者,以便朗读和暂停
2022-06-14 16:24:05 +08:00
function getWord() {
return window.getSelection ? window.getSelection() : document.selection.createRange().text;
}
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-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-06-14 16:24:05 +08:00
function onReadClick() {
isRead = !isRead;
2022-06-14 16:24:05 +08:00
if (!isRead) {
reader.cancel();
}
}
2022-06-14 16:24:05 +08:00
function onChooseClick() {
isChoose = !isChoose;
}