forked from mrlan/EnglishPal
Compare commits
No commits in common. "Bug407-JinHaoLin" and "master" have entirely different histories.
Bug407-Jin
...
master
|
@ -1,36 +1,29 @@
|
||||||
var isRead = true;
|
isRead = true;
|
||||||
var isChoose = true;
|
isChoose = true;
|
||||||
const reader = window.speechSynthesis; // 全局定义朗读者,以便朗读和暂停
|
var reader = window.speechSynthesis; // 全局定义朗读者,以便朗读和暂停
|
||||||
//获取字符
|
|
||||||
function getWord(){
|
function getWord(){
|
||||||
return window.getSelection?window.getSelection():document.selection.createRange().text;
|
var word = window.getSelection?window.getSelection():document.selection.createRange().text;
|
||||||
|
return word;
|
||||||
}
|
}
|
||||||
//填充字符
|
function fillinWord(){
|
||||||
function fillingWord(){
|
var word = getWord();
|
||||||
let word = getWord();
|
|
||||||
if (isRead) read(word);
|
if (isRead) read(word);
|
||||||
if (!isChoose) return;
|
if (!isChoose) return;
|
||||||
let element = document.getElementById("selected-words");
|
var element = document.getElementById("selected-words");
|
||||||
element.value = element.value + " " + word;
|
element.value = element.value + " " + word;
|
||||||
}
|
}
|
||||||
document.getElementById("text-content").addEventListener("click", fillingWord, false);
|
document.getElementById("text-content").addEventListener("click", fillinWord, false);
|
||||||
//朗读单词
|
function read(s){
|
||||||
//@word 要朗读的单词
|
var msg = new SpeechSynthesisUtterance(s);
|
||||||
function read(word){
|
|
||||||
//关闭当前正在读的单词
|
|
||||||
reader.cancel()
|
|
||||||
//创建新的朗读任务
|
|
||||||
let msg = new SpeechSynthesisUtterance(word);
|
|
||||||
reader.speak(msg);
|
reader.speak(msg);
|
||||||
}
|
}
|
||||||
//关闭正在读的单词
|
|
||||||
function onReadClick(){
|
function onReadClick(){
|
||||||
let isRead = !isRead;
|
isRead = !isRead;
|
||||||
if(!isRead){
|
if(!isRead){
|
||||||
reader.cancel();
|
reader.cancel();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//取消当前选择
|
|
||||||
function onChooseClick(){
|
function onChooseClick(){
|
||||||
isChoose = !isChoose;
|
isChoose = !isChoose;
|
||||||
}
|
}
|
|
@ -29,10 +29,9 @@ function highLight() {
|
||||||
if (sel_word1 != null) {
|
if (sel_word1 != null) {
|
||||||
var list = sel_word1.value.split(" ");
|
var list = sel_word1.value.split(" ");
|
||||||
for (var i = 0; i < list.length; ++i) {
|
for (var i = 0; i < list.length; ++i) {
|
||||||
list[i] = list[i].replace(/(^\s*)|(\s*$)/g, "");//消除字符串两边空字符
|
list[i] = list[i].replace(/(^\s*)|(\s*$)/g, "");
|
||||||
if (list[i] != "" && "<mark>".indexOf(list[i]) == -1 && "</mark>".indexOf(list[i]) == -1) {
|
if (list[i] != "" && "<mark>".indexOf(list[i]) == -1 && "</mark>".indexOf(list[i]) == -1) {
|
||||||
|
txt = txt.replace(new RegExp(list[i], "g"), "<mark>" + list[i] + "</mark>");
|
||||||
txt = txt.replace(new RegExp("\\s"+list[i]+"\\s", "g"), " <mark>" + list[i] + "</mark> ");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,7 +40,7 @@ function highLight() {
|
||||||
for (var i = 0; i < list2.length; ++i) {
|
for (var i = 0; i < list2.length; ++i) {
|
||||||
list2[i] = list2[i].replace(/(^\s*)|(\s*$)/g, "");
|
list2[i] = list2[i].replace(/(^\s*)|(\s*$)/g, "");
|
||||||
if (list2[i] != "" && "<mark>".indexOf(list2[i]) == -1 && "</mark>".indexOf(list2[i]) == -1) {
|
if (list2[i] != "" && "<mark>".indexOf(list2[i]) == -1 && "</mark>".indexOf(list2[i]) == -1) {
|
||||||
txt = txt.replace(new RegExp("\\s"+list2[i]+"\\s", "g"), " <mark>" + list2[i] + "</mark> ");
|
txt = txt.replace(new RegExp(list2[i], "g"), "<mark>" + list2[i] + "</mark>");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue