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