增加了部分优化代码的注释

refactor-huangzirui
黄子睿 2023-06-05 08:37:31 +08:00
parent 9816596cf8
commit 616d35a92f
3 changed files with 8 additions and 5 deletions

View File

@ -31,6 +31,7 @@ function onChooseClick() {
} }
// 如果网页刷新,停止播放声音 // 如果网页刷新,停止播放声音
// performance.navigation.type 以弃用改为了performance.getEntriesByType("navigation")[0].type
if (performance.getEntriesByType("navigation")[0].type == "reload") { if (performance.getEntriesByType("navigation")[0].type == "reload") {
Reader.stopRead(); Reader.stopRead();
} }

View File

@ -22,19 +22,19 @@ function getWord() {
function highLight() { function highLight() {
if (!isHighlight) return; if (!isHighlight) return;
let articleContent = document.getElementById("article").innerHTML; // innerHTML保留HTML标签来保持部分格式且适配不同的浏览器 let articleContent = document.getElementById("article").innerHTML; // 相比innerTextinnerHTML保留HTML标签来保持文章原先的格式且适配不同的浏览器。
let pickedWords = document.getElementById("selected-words"); // words picked to the text area let pickedWords = document.getElementById("selected-words"); // words picked to the text area
let dictionaryWords = document.getElementById("selected-words2"); // words appearing in the user's new words list let dictionaryWords = document.getElementById("selected-words2"); // words appearing in the user's new words list
let allWords = dictionaryWords === null ? pickedWords.value + " " : pickedWords.value + " " + dictionaryWords.value; let allWords = dictionaryWords === null ? pickedWords.value + " " : pickedWords.value + " " + dictionaryWords.value;
const list = allWords.split(" "); // 将所有的生词放入一个list中 const list = allWords.split(" "); // 将所有的生词放入一个list中
let totalSet = new Set(); let totalSet = new Set(); // 通过集合存储所有单词,通过判断重复单词,以及最后只遍历一次的方法,提高运行效率。
for (let i = 0; i < list.length; ++i) { for (let i = 0; i < list.length; ++i) {
list[i] = list[i].replace(/(^\W*)|(\W*$)/g, ""); // 消除单词两边的非单词字符 list[i] = list[i].replace(/(^\W*)|(\W*$)/g, ""); // 消除单词两边的非单词字符
if (list[i] != "" && !totalSet.has(list[i])) { if (list[i] != "" && !totalSet.has(list[i])) {
// 返回所有匹配单词的集合, 正则表达式RegExp()中, "\b"匹配一个单词的边界, g 表示全局匹配, i 表示对大小写不敏感。 // 返回所有匹配单词的集合, 正则表达式RegExp()中, "\b"匹配一个单词的边界, g 表示全局匹配, i 表示对大小写不敏感。
let matches = new Set(articleContent.match(new RegExp("\\b" + list[i] + "\\b", "gi"))); let matches = new Set(articleContent.match(new RegExp("\\b" + list[i] + "\\b", "gi")));
if (matches.has("mark")) { if (matches.has("mark")) {
// 优先处理单词为 "mark" 的情况 // 优先处理单词为 "mark" 的情况,防止和标签<mark>产生冲突。
totalSet = new Set(["mark", ...totalSet]); totalSet = new Set(["mark", ...totalSet]);
} }
totalSet = new Set([...totalSet, ...matches]); totalSet = new Set([...totalSet, ...matches]);
@ -51,6 +51,7 @@ function highLight() {
} }
function cancelHighlighting() { function cancelHighlighting() {
// 通过innerHTML获取文章后去掉<mark>标签即可实现取消高亮的功能,不需要遍历单词逐个修改。
let articleContent = document.getElementById("article").innerHTML; let articleContent = document.getElementById("article").innerHTML;
articleContent = articleContent.replace(/<(mark)[^>]*>/gi, ""); articleContent = articleContent.replace(/<(mark)[^>]*>/gi, "");
articleContent = articleContent.replace(/<(\/mark)[^>]*>/gi, ""); articleContent = articleContent.replace(/<(\/mark)[^>]*>/gi, "");

View File

@ -164,6 +164,7 @@ function elementFromString(string) {
* 当first大于second时返回1 * 当first大于second时返回1
*/ */
function compareWord(first, second) { function compareWord(first, second) {
// 优化了代码行数
if (first.freq !== second.freq) { if (first.freq !== second.freq) {
return first.freq < second.freq ? -1 : 1; return first.freq < second.freq ? -1 : 1;
} }