fix: retranslate loadmore text (issue #257)
This commit is contained in:
@@ -18,7 +18,7 @@ import {
|
||||
} from "../config";
|
||||
import Content from "../views/Content";
|
||||
import { updateFetchPool, clearFetchPool } from "./fetch";
|
||||
import { debounce, genEventName } from "./utils";
|
||||
import { debounce, genEventName, getHtmlText } from "./utils";
|
||||
import { runFixer } from "./webfix";
|
||||
import { apiTranslate } from "../apis";
|
||||
import { sendBgMsg } from "./msg";
|
||||
@@ -475,26 +475,19 @@ export class Translator {
|
||||
return;
|
||||
}
|
||||
|
||||
const preText = this._tranNodes.get(el);
|
||||
const curText = el.innerText.trim();
|
||||
// const traText = traEl.innerText.trim();
|
||||
|
||||
// todo
|
||||
// 1. traText when loading
|
||||
// 2. replace startsWith
|
||||
if (curText.startsWith(preText)) {
|
||||
const preText = getHtmlText(this._tranNodes.get(el));
|
||||
const curText = getHtmlText(el.innerHTML, APP_LCNAME);
|
||||
if (preText === curText) {
|
||||
return;
|
||||
}
|
||||
|
||||
traEl.remove();
|
||||
}
|
||||
|
||||
// 缓存已翻译元素
|
||||
this._tranNodes.set(el, el.innerHTML);
|
||||
|
||||
let q = el.innerText.trim();
|
||||
if (this._rule.transOnly === "true") {
|
||||
this._tranNodes.set(el, el.innerHTML);
|
||||
} else {
|
||||
this._tranNodes.set(el, q);
|
||||
}
|
||||
const keeps = [];
|
||||
|
||||
// 翻译开始钩子函数
|
||||
|
||||
@@ -250,3 +250,20 @@ export const blobToBase64 = (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();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user