fix: query tagname bug
This commit is contained in:
@@ -7,6 +7,7 @@ export const injectInlineJs = (code, id = "kiss-translator-inline-js") => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const el = document.createElement("script");
|
const el = document.createElement("script");
|
||||||
|
el.setAttribute("data-source", "kiss-inject injectInlineJs");
|
||||||
el.type = "text/javascript";
|
el.type = "text/javascript";
|
||||||
el.id = id;
|
el.id = id;
|
||||||
el.textContent = trustedTypesHelper.createScript(code);
|
el.textContent = trustedTypesHelper.createScript(code);
|
||||||
@@ -19,6 +20,7 @@ export const injectInlineJsBg = (code, id = "kiss-translator-inline-js") => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const el = document.createElement("script");
|
const el = document.createElement("script");
|
||||||
|
el.setAttribute("data-source", "kiss-inject injectInlineJsBg");
|
||||||
el.type = "text/javascript";
|
el.type = "text/javascript";
|
||||||
el.id = id;
|
el.id = id;
|
||||||
el.textContent = code;
|
el.textContent = code;
|
||||||
@@ -32,6 +34,7 @@ export const injectExternalJs = (src, id = "kiss-translator-external-js") => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const el = document.createElement("script");
|
const el = document.createElement("script");
|
||||||
|
el.setAttribute("data-source", "kiss-inject injectExternalJs");
|
||||||
el.type = "text/javascript";
|
el.type = "text/javascript";
|
||||||
el.id = id;
|
el.id = id;
|
||||||
el.src = trustedTypesHelper.createScriptURL(src);
|
el.src = trustedTypesHelper.createScriptURL(src);
|
||||||
|
|||||||
@@ -11,7 +11,10 @@ import { createLoadingSVG } from "./svg";
|
|||||||
import { logger } from "./log";
|
import { logger } from "./log";
|
||||||
|
|
||||||
function isInputNode(node) {
|
function isInputNode(node) {
|
||||||
return node.nodeName === "INPUT" || node.nodeName === "TEXTAREA";
|
return (
|
||||||
|
node.nodeName?.toUpperCase() === "INPUT" ||
|
||||||
|
node.nodeName?.toUpperCase() === "TEXTAREA"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function isEditAbleNode(node) {
|
function isEditAbleNode(node) {
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import {
|
import {
|
||||||
APP_UPNAME,
|
|
||||||
APP_LCNAME,
|
APP_LCNAME,
|
||||||
APP_CONSTS,
|
APP_CONSTS,
|
||||||
OPT_STYLE_FUZZY,
|
OPT_STYLE_FUZZY,
|
||||||
@@ -153,7 +152,7 @@ export class Translator {
|
|||||||
|
|
||||||
// 译文相关class
|
// 译文相关class
|
||||||
static KISS_CLASS = {
|
static KISS_CLASS = {
|
||||||
warpper: `${APP_LCNAME}-wrapper notranslate`,
|
warpper: `${APP_LCNAME}-wrapper`,
|
||||||
inner: `${APP_LCNAME}-inner`,
|
inner: `${APP_LCNAME}-inner`,
|
||||||
term: `${APP_LCNAME}-term`,
|
term: `${APP_LCNAME}-term`,
|
||||||
br: `${APP_LCNAME}-br`,
|
br: `${APP_LCNAME}-br`,
|
||||||
@@ -224,8 +223,8 @@ export class Translator {
|
|||||||
static isBlockNode(el) {
|
static isBlockNode(el) {
|
||||||
if (!Translator.isElementOrFragment(el)) return false;
|
if (!Translator.isElementOrFragment(el)) return false;
|
||||||
|
|
||||||
if (Translator.TAGS.INLINE.has(el.nodeName)) return false;
|
if (Translator.TAGS.INLINE.has(el.nodeName?.toUpperCase())) return false;
|
||||||
if (Translator.TAGS.BLOCK.has(el.nodeName)) return true;
|
if (Translator.TAGS.BLOCK.has(el.nodeName?.toUpperCase())) return true;
|
||||||
if (el.attributes?.display?.value?.includes("inline")) return false;
|
if (el.attributes?.display?.value?.includes("inline")) return false;
|
||||||
|
|
||||||
if (Translator.displayCache.has(el)) {
|
if (Translator.displayCache.has(el)) {
|
||||||
@@ -265,7 +264,7 @@ export class Translator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 内置忽略元素
|
// 内置忽略元素
|
||||||
static KISS_IGNORE_SELECTOR = `${APP_LCNAME}, .kiss-caption-container, .kiss-subtitle-controls
|
static KISS_IGNORE_SELECTOR = `.${Translator.KISS_CLASS.warpper}, .kiss-caption-container, .kiss-subtitle-controls
|
||||||
#${APP_CONSTS.fabID}, .${APP_CONSTS.fabID}_warpper,
|
#${APP_CONSTS.fabID}, .${APP_CONSTS.fabID}_warpper,
|
||||||
#${APP_CONSTS.boxID}, .${APP_CONSTS.boxID}_warpper,
|
#${APP_CONSTS.boxID}, .${APP_CONSTS.boxID}_warpper,
|
||||||
#${APP_CONSTS.popupID}, .${APP_CONSTS.popupID}_warpper`;
|
#${APP_CONSTS.popupID}, .${APP_CONSTS.popupID}_warpper`;
|
||||||
@@ -288,7 +287,7 @@ export class Translator {
|
|||||||
#combinedTermsRegex; // 专业术语正则表达式
|
#combinedTermsRegex; // 专业术语正则表达式
|
||||||
#combinedSkipsRegex; // 跳过文本正则表达式
|
#combinedSkipsRegex; // 跳过文本正则表达式
|
||||||
#placeholderRegex; // 恢复htnml正则表达式
|
#placeholderRegex; // 恢复htnml正则表达式
|
||||||
#translationTagName = APP_UPNAME; // 翻译容器的标签名
|
#translationTagName = APP_LCNAME; // 翻译容器的标签名
|
||||||
#eventName = ""; // 通信事件名称
|
#eventName = ""; // 通信事件名称
|
||||||
#docInfo = {}; // 网页信息
|
#docInfo = {}; // 网页信息
|
||||||
#glossary = {}; // AI词典
|
#glossary = {}; // AI词典
|
||||||
@@ -602,7 +601,8 @@ export class Translator {
|
|||||||
for (const mutation of mutations) {
|
for (const mutation of mutations) {
|
||||||
if (
|
if (
|
||||||
this.#skipMoNodes.has(mutation.target) ||
|
this.#skipMoNodes.has(mutation.target) ||
|
||||||
mutation.nextSibling?.tagName === this.#translationTagName
|
mutation.nextSibling?.tagName?.toLowerCase() ===
|
||||||
|
this.#translationTagName
|
||||||
) {
|
) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -620,7 +620,7 @@ export class Translator {
|
|||||||
mutation.addedNodes.forEach((node) => {
|
mutation.addedNodes.forEach((node) => {
|
||||||
if (
|
if (
|
||||||
this.#skipMoNodes.has(node) ||
|
this.#skipMoNodes.has(node) ||
|
||||||
node.nodeName === this.#translationTagName
|
node.nodeName?.toLowerCase() === this.#translationTagName
|
||||||
) {
|
) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -1056,7 +1056,10 @@ export class Translator {
|
|||||||
textLength += node.textContent.length;
|
textLength += node.textContent.length;
|
||||||
|
|
||||||
const isSentenceEnd = sentenceEndRegexForTest.test(node.textContent);
|
const isSentenceEnd = sentenceEndRegexForTest.test(node.textContent);
|
||||||
if (!isSentenceEnd || node.nextSibling?.nodeName === "BR") {
|
if (
|
||||||
|
!isSentenceEnd ||
|
||||||
|
node.nextSibling?.nodeName?.toUpperCase() === "BR"
|
||||||
|
) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1109,9 +1112,9 @@ export class Translator {
|
|||||||
if (node.matches(this.#rule.keepSelector)) return false;
|
if (node.matches(this.#rule.keepSelector)) return false;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
Translator.TAGS.BREAK_LINE.has(node.nodeName) ||
|
Translator.TAGS.BREAK_LINE.has(node.nodeName?.toUpperCase()) ||
|
||||||
node.matches?.(this.#ignoreSelector) ||
|
node.matches?.(this.#ignoreSelector) ||
|
||||||
node.nodeName === this.#translationTagName
|
node.nodeName?.toLowerCase() === this.#translationTagName
|
||||||
) {
|
) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -1194,11 +1197,10 @@ export class Translator {
|
|||||||
nodes,
|
nodes,
|
||||||
termsStyle
|
termsStyle
|
||||||
);
|
);
|
||||||
// console.log("processedString", processedString);
|
|
||||||
if (this.#isInvalidText(processedString)) return;
|
if (this.#isInvalidText(processedString)) return;
|
||||||
|
|
||||||
const wrapper = document.createElement(this.#translationTagName);
|
const wrapper = document.createElement(this.#translationTagName);
|
||||||
wrapper.className = Translator.KISS_CLASS.warpper;
|
wrapper.className = `${Translator.KISS_CLASS.warpper} notranslate`;
|
||||||
|
|
||||||
if (processedString.length > newlineLength) {
|
if (processedString.length > newlineLength) {
|
||||||
const br = document.createElement("br");
|
const br = document.createElement("br");
|
||||||
@@ -1443,7 +1445,9 @@ export class Translator {
|
|||||||
|
|
||||||
// 查找指定节点下所有译文节点
|
// 查找指定节点下所有译文节点
|
||||||
#findTranslationWrappers(parentNode) {
|
#findTranslationWrappers(parentNode) {
|
||||||
return parentNode.querySelectorAll(`:scope > ${APP_LCNAME}`);
|
return parentNode.querySelectorAll(
|
||||||
|
`:scope > .${Translator.KISS_CLASS.warpper}`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 清理所有插入的译文dom
|
// 清理所有插入的译文dom
|
||||||
@@ -1454,7 +1458,7 @@ export class Translator {
|
|||||||
// 清理节点下面所有译文dom
|
// 清理节点下面所有译文dom
|
||||||
#cleanupAllTranslations(root) {
|
#cleanupAllTranslations(root) {
|
||||||
root
|
root
|
||||||
.querySelectorAll(APP_LCNAME)
|
.querySelectorAll(`.${Translator.KISS_CLASS.warpper}`)
|
||||||
.forEach((el) => this.#removeTranslationElement(el));
|
.forEach((el) => this.#removeTranslationElement(el));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -252,6 +252,8 @@ export default class TranslatorManager {
|
|||||||
sendIframeMsg(action, args);
|
sendIframeMsg(action, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger.debug("process action:", action, args);
|
||||||
|
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case MSG_TRANS_TOGGLE:
|
case MSG_TRANS_TOGGLE:
|
||||||
this._translator.toggle();
|
this._translator.toggle();
|
||||||
|
|||||||
Reference in New Issue
Block a user