fix: keepselector for twitter

This commit is contained in:
Gabe
2025-10-24 01:46:36 +08:00
parent ca48ab639e
commit ac8c07deb4
2 changed files with 28 additions and 22 deletions

View File

@@ -211,7 +211,8 @@ const RULES_MAP = {
}, },
"twitter.com, https://x.com": { "twitter.com, https://x.com": {
selector: `[data-testid='tweetText']`, 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`, autoScan: `false`,
}, },
"www.youtube.com/live_chat": { "www.youtube.com/live_chat": {

View File

@@ -206,6 +206,8 @@ export class Translator {
// 14. 包含常见扩展名的文件名 (例如: document.pdf, image.jpeg) // 14. 包含常见扩展名的文件名 (例如: document.pdf, image.jpeg)
/^[^\s\\/:]+?\.[a-zA-Z0-9]{2,5}$/, /^[^\s\\/:]+?\.[a-zA-Z0-9]{2,5}$/,
// todo: 数字和特殊字符组成的字符串
]; ];
static DEFAULT_OPTIONS = DEFAULT_SETTING; // 默认配置 static DEFAULT_OPTIONS = DEFAULT_SETTING; // 默认配置
@@ -528,34 +530,37 @@ export class Translator {
#createMutationObserver() { #createMutationObserver() {
return new MutationObserver((mutations) => { return new MutationObserver((mutations) => {
for (const mutation of mutations) { for (const mutation of mutations) {
if (this.#skipMoNodes.has(mutation.target)) return;
if ( if (
mutation.type === "characterData" && this.#skipMoNodes.has(mutation.target) ||
mutation.oldValue !== mutation.target.nodeValue 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 nodes = new Set();
let hasText = false; let hasText = false;
mutation.addedNodes.forEach((node) => { 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) { if (node.nodeType === Node.TEXT_NODE) {
hasText = true; hasText = true;
} else if ( } else if (Translator.isElementOrFragment(node)) {
Translator.isElementOrFragment(node) &&
node.nodeName !== this.#translationTagName
) {
nodes.add(node); nodes.add(node);
} }
}
}); });
if (hasText) { if (hasText) {
this.#queueForRescan(mutation.target); this.#queueForRescan(mutation.target);
@@ -772,7 +777,7 @@ export class Translator {
#scanNode(rootNode) { #scanNode(rootNode) {
if ( if (
!Translator.isElementOrFragment(rootNode) || !Translator.isElementOrFragment(rootNode) ||
rootNode.matches?.(this.#rule.keepSelector) || // rootNode.matches?.(this.#rule.keepSelector) ||
rootNode.matches?.(this.#ignoreSelector) rootNode.matches?.(this.#ignoreSelector)
) { ) {
return; return;