optimize some code

This commit is contained in:
Gabe Yuan
2023-08-03 10:23:40 +08:00
parent f31531e558
commit fbfd68fea2
11 changed files with 19 additions and 20 deletions

View File

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