rollback to innerText

This commit is contained in:
Gabe Yuan
2023-08-03 15:29:54 +08:00
parent d735ee977c
commit 5aca06fb9c
8 changed files with 14 additions and 36 deletions

View File

@@ -5,7 +5,6 @@ import {
MSG_TRANS_TOGGLE,
MSG_TRANS_GETRULE,
MSG_TRANS_PUTRULE,
OPT_TRANS_OPENAI,
TRANS_MIN_LENGTH,
TRANS_MAX_LENGTH,
} from "./config";
@@ -104,10 +103,7 @@ class Translator {
}
// 除openai外保留code和a标签
const q =
this._rule.translator === OPT_TRANS_OPENAI
? el.innerText.trim()
: el.innerHTML.replace(/<(?!\/?(code|a))[^>]+>/gi, "").trim();
const q = this._rule.translator === el.innerText.trim();
if (!q || q.length < TRANS_MIN_LENGTH || q.length > TRANS_MAX_LENGTH) {
// 太长或太短不翻译
return;

View File

@@ -7,7 +7,6 @@ import {
OPT_STYLE_WAVYLINE,
OPT_STYLE_FUZZY,
OPT_STYLE_HIGHTLIGHT,
OPT_TRANS_OPENAI,
} from "../../config";
import { useTranslate } from "../../hooks/Translate";
@@ -77,30 +76,13 @@ export default function Content({ q, rule }) {
return (
<>
{q.length > 40 ? <br /> : " "}
{rule.translator === OPT_TRANS_OPENAI ? (
<span
style={style}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
>
{text}
</span>
) : (
<span
style={style}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
dangerouslySetInnerHTML={{
__html: text.replace(
/<code>(.*?)<\/code>/gi,
(_match, p1) =>
`<code>${p1
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")}</code>`
),
}}
/>
)}
<span
style={style}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
>
{text}
</span>
</>
);
}