Skip to content

Commit

Permalink
Fix/fix word highlight issue (#34)
Browse files Browse the repository at this point in the history
* 🚧 Fix floating panel out of boundary.

* 🐛 Fix can not mark word bug.

* 🐛 Fix word highlight issue.

---------
  • Loading branch information
tolerious authored Aug 30, 2024
1 parent c4ccab4 commit 968545d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 83 deletions.
7 changes: 2 additions & 5 deletions src/plugins/general/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
logger,
waitEventFromBackgroundScriptInContentScript,
} from "../utils/utils";
import { listenEventFromBackgroundScript, logger } from "../utils/utils";
import {
customizeGeneralEvent,
getWordList,
Expand All @@ -11,7 +8,7 @@ import {

export async function initializeGeneralWebSite() {
logger("initializeGeneralWebSite");
waitEventFromBackgroundScriptInContentScript();
listenEventFromBackgroundScript();
await injectTranslationFloatingPanelToShadowDom();
customizeGeneralEvent();
goThroughDomAndGenerateCustomElement(await getWordList());
Expand Down
64 changes: 23 additions & 41 deletions src/plugins/general/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,8 @@ function removeUnMarkedWord(word) {
markedNodeList.forEach((node) => {
// 说明取消的是这个节点
if (
node.textContent
.trim()
.toLocaleLowerCase()
.replace(".", "")
.replace(",", "")
.replace('"', "")
.replace("(", "")
.replace(")", "")
.replace(":", "") ===
word
.trim()
.toLocaleLowerCase()
.replace(".", "")
.replace(",", "")
.replace('"', "")
.replace("(", "")
.replace(")", "")
.replace(":", "")
convertStringToLowerCaseAndRemoveSpecialCharacter(node.textContent) ===
convertStringToLowerCaseAndRemoveSpecialCharacter(word)
) {
const targetParentNode = node.parentNode;
// 重新把被自定义span标签包裹的文本系欸但替换回来
Expand All @@ -97,26 +81,12 @@ function convertCurrentTextNodeContent(textNode, targetWordList) {
// 判断并找出当前文本节点中包含的目标单词
const textContent = textNode.textContent;
const targetWordSet = new Set(targetWordList);
const splitedTextContentStringList = textContent.split(" ");
const splittedTextContentStringList = textContent.split(" ");
// 存放的是目标单词在splittedTextContentStringList中的索引
const indexList = [];
splitedTextContentStringList.filter((s, index) => {
/**
* FIXME: 这里没有处理的一点是如果是被特殊服务包裹起来的字符,
* 例如"hello, world",这里world"会被当成一个单词,所以使用set中的has
* 函数就无法判断出来,这里需要单独处理下这种情况
*
* */
splittedTextContentStringList.filter((s, index) => {
if (
targetWordSet.has(
s
.toLowerCase()
.replace(".", "")
.replace(",", "")
.replace('"', "")
.replace("(", "")
.replace(")", "")
.replace(":", "")
)
targetWordSet.has(convertStringToLowerCaseAndRemoveSpecialCharacter(s))
) {
indexList.push(index);
}
Expand All @@ -126,7 +96,7 @@ function convertCurrentTextNodeContent(textNode, targetWordList) {
const newNodeList = [];
// 遍历indexList,重新构造#text节点
if (indexList.length > 0) {
splitedTextContentStringList.forEach((s, index) => {
splittedTextContentStringList.forEach((s, index) => {
// 这段文本不包含目标单词
const stt = s + " ";
if (indexList.indexOf(index) > -1) {
Expand Down Expand Up @@ -158,7 +128,7 @@ function convertCurrentTextNodeContent(textNode, targetWordList) {
export function customizeGeneralEvent() {
addSelectionChangeEvent();
addMouseDownEvent();
listenFromFloatingPanelEvent();
listenEventFromFloatingPanelEvent();
}

/**
Expand Down Expand Up @@ -376,7 +346,7 @@ function hideTranslationFloatingPanel() {
}
}

async function createTranslationFloatingPanel(x = 0, y = 0) {
async function createTranslationFloatingPanelToShadowDom(x = 0, y = 0) {
const shadowRoot = document.createElement("div");
shadowRoot.id = translationFloatingPanelShadowRootId;
shadowRoot.style.display = "none";
Expand Down Expand Up @@ -438,7 +408,7 @@ function injectTranslationFloatingPanelCss() {
});
}

export function listenFromFloatingPanelEvent() {
export function listenEventFromFloatingPanelEvent() {
document.addEventListener("floatingPanelEvent", async (event) => {
const detail = JSON.parse(event.detail);
switch (detail.type) {
Expand Down Expand Up @@ -505,5 +475,17 @@ export async function injectTranslationFloatingPanelToShadowDom() {
if (checkIfTranslationFloatingPanelExist()) {
return;
}
await createTranslationFloatingPanel();
await createTranslationFloatingPanelToShadowDom();
}

function convertStringToLowerCaseAndRemoveSpecialCharacter(s) {
return s
.trim()
.toLowerCase()
.replaceAll(".", "")
.replaceAll(",", "")
.replaceAll('"', "")
.replaceAll("(", "")
.replaceAll(")", "")
.replaceAll(":", "");
}
2 changes: 1 addition & 1 deletion src/plugins/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ export function sendMessageFromContentScriptToBackgroundScript(
browser.runtime.sendMessage({ type, message });
}

export function waitEventFromBackgroundScriptInContentScript() {
export function listenEventFromBackgroundScript() {
browser.runtime.onMessage.addListener((message) => {
switch (message.type) {
case "youtube":
Expand Down
52 changes: 16 additions & 36 deletions src/webPage/translationFloatingPanel/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,7 @@ function getYoutubeId() {
async function addWord() {
if (isLiked.value) {
const t = await customPost(GET_WORD_ID, {
en: currentWord.value
.trim()
.toLocaleLowerCase()
.replace(',', '')
.replace('.', '')
.replace('"', '')
.replace('(', '')
.replace(')', '')
.replace(':', '')
en: convertStringToLowerCaseAndRemoveSpecialCharacter(currentWord.value.trim())
});
const r = await customPost(DELETE_WORD, { id: t.data.data._id, groupId: t.data.data.groupID });
if (r.data.code === 200) {
Expand All @@ -95,15 +87,7 @@ async function addWord() {
} else {
let t;
t = await customPost(SAVE_WORD, {
en: currentWord.value
.trim()
.toLocaleLowerCase()
.replace(',', '')
.replace('.', '')
.replace('"', '')
.replace('(', '')
.replace(')', '')
.replace(':', ''),
en: convertStringToLowerCaseAndRemoveSpecialCharacter(currentWord.value.trim()),
groupId: groupId.value
});
if (t.data.code === 200) {
Expand Down Expand Up @@ -144,15 +128,7 @@ watch(currentWord, async (newVal) => {
return;
}
const t = await customPost(SEARCH_WORD, {
en: newVal
.trim()
.toLowerCase()
.replace(',', '')
.replace('.', '')
.replace('"', '')
.replace('(', '')
.replace(')', '')
.replace(':', '')
en: convertStringToLowerCaseAndRemoveSpecialCharacter(newVal.trim())
});
isLiked.value = t.data.data.isLiked;
});
Expand All @@ -176,15 +152,7 @@ function listenEventFromGeneralScript() {
} else {
isPlayAudioIconVisible.value = true;
}
getTranslationFromYouDao(
data.word
.replace('.', '')
.replace(',', '')
.replace('"', '')
.replace('(', '')
.replace(')', '')
.replace(':', '')
);
getTranslationFromYouDao(convertStringToLowerCaseAndRemoveSpecialCharacter(data.word));
break;
case 'play':
if (isPlayAudioIconVisible.value) {
Expand Down Expand Up @@ -271,6 +239,18 @@ function sendMessageToGeneralScript(message: any) {
});
document.dispatchEvent(event);
}
function convertStringToLowerCaseAndRemoveSpecialCharacter(s: string) {
return s
.trim()
.toLowerCase()
.replace(/\./g, '')
.replace(/,/g, '')
.replace(/"/g, '')
.replace(/\(/g, '')
.replace(/\)/g, '')
.replace(/:/g, '');
}
</script>

<style scoped>
Expand Down

0 comments on commit 968545d

Please sign in to comment.