fix: retranslate loadmore text (issue #257)

This commit is contained in:
Gabe
2025-08-09 20:55:04 +08:00
parent 3a3f1fabe1
commit 45b294a121
2 changed files with 24 additions and 14 deletions

View File

@@ -18,7 +18,7 @@ import {
} from "../config"; } from "../config";
import Content from "../views/Content"; import Content from "../views/Content";
import { updateFetchPool, clearFetchPool } from "./fetch"; import { updateFetchPool, clearFetchPool } from "./fetch";
import { debounce, genEventName } from "./utils"; import { debounce, genEventName, getHtmlText } from "./utils";
import { runFixer } from "./webfix"; import { runFixer } from "./webfix";
import { apiTranslate } from "../apis"; import { apiTranslate } from "../apis";
import { sendBgMsg } from "./msg"; import { sendBgMsg } from "./msg";
@@ -475,26 +475,19 @@ export class Translator {
return; return;
} }
const preText = this._tranNodes.get(el); const preText = getHtmlText(this._tranNodes.get(el));
const curText = el.innerText.trim(); const curText = getHtmlText(el.innerHTML, APP_LCNAME);
// const traText = traEl.innerText.trim(); if (preText === curText) {
// todo
// 1. traText when loading
// 2. replace startsWith
if (curText.startsWith(preText)) {
return; return;
} }
traEl.remove(); traEl.remove();
} }
// 缓存已翻译元素
this._tranNodes.set(el, el.innerHTML);
let q = el.innerText.trim(); let q = el.innerText.trim();
if (this._rule.transOnly === "true") {
this._tranNodes.set(el, el.innerHTML);
} else {
this._tranNodes.set(el, q);
}
const keeps = []; const keeps = [];
// 翻译开始钩子函数 // 翻译开始钩子函数

View File

@@ -250,3 +250,20 @@ export const blobToBase64 = (blob) => {
reader.readAsDataURL(blob); reader.readAsDataURL(blob);
}); });
}; };
/**
* 获取html内的文本
* @param {*} htmlStr
* @param {*} skipTag
* @returns
*/
export const getHtmlText = (htmlStr, skipTag = "") => {
const parser = new DOMParser();
const doc = parser.parseFromString(htmlStr, "text/html");
if (skipTag) {
doc.querySelectorAll(skipTag).forEach((el) => el.remove());
}
return doc.body.innerText.trim();
};