EnglishPal/app/static/js/fillword.js

34 lines
800 B
JavaScript

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