From ac8c07deb4bf47cb52d975548e594b480b97694d Mon Sep 17 00:00:00 2001 From: Gabe Date: Fri, 24 Oct 2025 01:46:36 +0800 Subject: [PATCH] fix: keepselector for twitter --- src/config/rules.js | 3 ++- src/libs/translator.js | 47 +++++++++++++++++++++++------------------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/config/rules.js b/src/config/rules.js index fe26c64..7853840 100644 --- a/src/config/rules.js +++ b/src/config/rules.js @@ -211,7 +211,8 @@ const RULES_MAP = { }, "twitter.com, https://x.com": { selector: `[data-testid='tweetText']`, - keepSelector: `img, svg, span:has(a), div:has(a)`, + keepSelector: `img, svg, a, span:has(a), div:has(a)`, + ignoreSelector: `button, [data-testid='videoPlayer'], [role='group']`, autoScan: `false`, }, "www.youtube.com/live_chat": { diff --git a/src/libs/translator.js b/src/libs/translator.js index 00c30d4..efd10e5 100644 --- a/src/libs/translator.js +++ b/src/libs/translator.js @@ -206,6 +206,8 @@ export class Translator { // 14. 包含常见扩展名的文件名 (例如: document.pdf, image.jpeg) /^[^\s\\/:]+?\.[a-zA-Z0-9]{2,5}$/, + + // todo: 数字和特殊字符组成的字符串 ]; static DEFAULT_OPTIONS = DEFAULT_SETTING; // 默认配置 @@ -528,33 +530,36 @@ export class Translator { #createMutationObserver() { return new MutationObserver((mutations) => { for (const mutation of mutations) { - if (this.#skipMoNodes.has(mutation.target)) return; - if ( - mutation.type === "characterData" && - mutation.oldValue !== mutation.target.nodeValue + this.#skipMoNodes.has(mutation.target) || + mutation.nextSibling?.tagName === this.#translationTagName ) { - this.#queueForRescan(mutation.target.parentElement); - } else if (mutation.type === "childList") { - if (mutation.nextSibling?.tagName === this.#translationTagName) { - // 恢复原文时插入元素,忽略 - continue; - } + continue; + } + if (mutation.type === "characterData") { + if ( + mutation.oldValue !== mutation.target.nodeValue && + !this.#combinedSkipsRegex.test(mutation.target.nodeValue) + ) { + this.#queueForRescan(mutation.target.parentElement); + } + } else if (mutation.type === "childList") { let nodes = new Set(); let hasText = false; mutation.addedNodes.forEach((node) => { - if (this.#skipMoNodes.has(node)) return; + if ( + this.#skipMoNodes.has(node) || + node.nodeName === this.#translationTagName || + !node.innerText + ) { + return; + } - if (/\S/.test(node.nodeValue)) { - if (node.nodeType === Node.TEXT_NODE) { - hasText = true; - } else if ( - Translator.isElementOrFragment(node) && - node.nodeName !== this.#translationTagName - ) { - nodes.add(node); - } + if (node.nodeType === Node.TEXT_NODE) { + hasText = true; + } else if (Translator.isElementOrFragment(node)) { + nodes.add(node); } }); if (hasText) { @@ -772,7 +777,7 @@ export class Translator { #scanNode(rootNode) { if ( !Translator.isElementOrFragment(rootNode) || - rootNode.matches?.(this.#rule.keepSelector) || + // rootNode.matches?.(this.#rule.keepSelector) || rootNode.matches?.(this.#ignoreSelector) ) { return;