From 8575a0dc33672c53fccf3fc383950c92f22ef0b0 Mon Sep 17 00:00:00 2001 From: "1994836463@qq.com" Date: Mon, 20 May 2024 15:25:34 +0800 Subject: [PATCH] fix --- app/static/js/fillword.js | 6 +++++- app/static/js/highlight.js | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/app/static/js/fillword.js b/app/static/js/fillword.js index b967633..d851123 100644 --- a/app/static/js/fillword.js +++ b/app/static/js/fillword.js @@ -10,7 +10,11 @@ function fillInWord() { if (isRead) Reader.read(word, inputSlider.value); if (!isChoose) return; const element = document.getElementById("selected-words"); - element.value = element.value + " " + word; + let index = (String)(element.value).indexOf(word); + localStorage.setItem("nowWord",element.value); + if(index === -1){ + element.value = element.value + " " + word; + } } document.getElementById("text-content").addEventListener("click", fillInWord, false); diff --git a/app/static/js/highlight.js b/app/static/js/highlight.js index 0cea31a..ccd6c95 100644 --- a/app/static/js/highlight.js +++ b/app/static/js/highlight.js @@ -33,6 +33,21 @@ function highLight() { allWords = pickedWords.value + " "; } const list = allWords.split(" ");//将所有的生词放入一个list中,用于后续处理 + if(word !== null && word !== "" && word !== " "){ + let articleContent_fb2 = articleContent; + if(localStorage.getItem("nowWord").indexOf(word) !== -1){ + while(articleContent_fb2.toLowerCase().indexOf(word.toLowerCase()) !== -1){ + // 找到副本中和list[i]匹配的第一个单词(第一种匹配情况),并赋值给list[i]。 + const index = articleContent_fb2.toLowerCase().indexOf(word.toLowerCase()); + word = articleContent_fb2.substring(index, index + word.length); + articleContent_fb2 = articleContent_fb2.substring(index + word.length); // 使用副本中list[i]之后的子串替换掉副本 + articleContent = articleContent.replace(new RegExp(""+word+"", "g"), word) + } + pickedWords.value = localStorage.getItem("nowWord").replace(word,""); + document.getElementById("article").innerHTML = articleContent; + return; + } + } for (let i = 0; i < list.length; ++i) { list[i] = list[i].replace(/(^\s*)|(\s*$)/g, ""); //消除单词两边的空字符 list[i] = list[i].replace('|', "");