forked from mrlan/EnglishPal
				
			Fix bug 579
							parent
							
								
									220e108ee1
								
							
						
					
					
						commit
						eb3268a139
					
				| 
						 | 
					@ -15,6 +15,42 @@ function showBtnHandler() {
 | 
				
			||||||
    document.getElementById("text-content").addEventListener("touchstart", fillInWord, false);
 | 
					    document.getElementById("text-content").addEventListener("touchstart", fillInWord, false);
 | 
				
			||||||
    highLight();
 | 
					    highLight();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					function replaceWords(str, word) {
 | 
				
			||||||
 | 
					  let count = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const regex = new RegExp(`(^|\\s)${word}(?=\\s|$)`, 'g');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  let result = str.replace(regex, (match, p1) => {
 | 
				
			||||||
 | 
					    count++;
 | 
				
			||||||
 | 
					    // p1 保留前导空格(如果有),仅第一个匹配保留,后续匹配替换为空字符串
 | 
				
			||||||
 | 
					    return count === 1 ? match : p1;
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  return result;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function countWords(str, word) {
 | 
				
			||||||
 | 
					  // 使用正则表达式匹配目标单词的整个单词边界情况,包括前后空格、行首和行尾
 | 
				
			||||||
 | 
					  const regex = new RegExp(`(^|\\s)${word}(?=\\s|$)`, 'g');
 | 
				
			||||||
 | 
					  let match;
 | 
				
			||||||
 | 
					  let count = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  // 迭代匹配所有符合条件的单词
 | 
				
			||||||
 | 
					  while ((match = regex.exec(str)) !== null) {
 | 
				
			||||||
 | 
					    count++;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  return count;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					//用于替换单词
 | 
				
			||||||
 | 
					function replaceAllWords(str, word, replacement) {
 | 
				
			||||||
 | 
					  const regex = new RegExp(`(^|\\s)${word}(?=\\s|$)`, 'gi');
 | 
				
			||||||
 | 
					  let result = str.replace(regex, (match, p1) => {
 | 
				
			||||||
 | 
					    return p1 + replacement;
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  return result;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function getWord() {
 | 
					function getWord() {
 | 
				
			||||||
    return window.getSelection ? window.getSelection() : document.selection.createRange().text;
 | 
					    return window.getSelection ? window.getSelection() : document.selection.createRange().text;
 | 
				
			||||||
| 
						 | 
					@ -33,8 +69,22 @@ function highLight() {
 | 
				
			||||||
    if(word !== null && word !== "" && word !== " "){
 | 
					    if(word !== null && word !== "" && word !== " "){
 | 
				
			||||||
        let articleContent_fb2 = articleContent;
 | 
					        let articleContent_fb2 = articleContent;
 | 
				
			||||||
        if(localStorage.getItem("nowWords").indexOf(word) !== -1 || localStorage.getItem("nowWords").indexOf(word.toLowerCase()) !== -1){
 | 
					        if(localStorage.getItem("nowWords").indexOf(word) !== -1 || localStorage.getItem("nowWords").indexOf(word.toLowerCase()) !== -1){
 | 
				
			||||||
            articleContent = articleContent.replace(new RegExp('<span class="highlighted">' + word + '</span>', "gi"), word);
 | 
					            articleContent = articleContent.replace(new RegExp('<span class="highlighted">' + word + '</span>', "g"), word);
 | 
				
			||||||
            pickedWords.value = localStorage.getItem("nowWords").replace(word,"");
 | 
					
 | 
				
			||||||
 | 
					    let count=countWords(pickedWords.value,word)
 | 
				
			||||||
 | 
					    let currentWords=localStorage.getItem("nowWords")+" "+word
 | 
				
			||||||
 | 
					    localStorage.setItem("nowWords",currentWords)
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					    if(count>0){
 | 
				
			||||||
 | 
					        if(count==1){
 | 
				
			||||||
 | 
					            localStorage.setItem("nowWords",replaceWords(currentWords,word))
 | 
				
			||||||
 | 
					        }else{
 | 
				
			||||||
 | 
					            localStorage.setItem("nowWords",replaceAllWords(currentWords,word,""))
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    pickedWords.value = localStorage.getItem("nowWords")
 | 
				
			||||||
            document.getElementById("article").innerHTML = articleContent;
 | 
					            document.getElementById("article").innerHTML = articleContent;
 | 
				
			||||||
            return;
 | 
					            return;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue