Skip to content

Commit 3f20121

Browse files
committed
opti: 优化翻译节点选中逻辑
1 parent 96dfd6a commit 3f20121

File tree

2 files changed

+46
-28
lines changed

2 files changed

+46
-28
lines changed

FluentRead.js

+43-25
Original file line numberDiff line numberDiff line change
@@ -704,37 +704,56 @@ function handler(mouseX, mouseY, time, noSkip = true) {
704704
}, time);
705705
}
706706

707-
const getTransNodeSet = new Set([
708-
'span', 'p', // 日常
709-
'h1', 'h2', 'h3', 'h4', 'h5', 'h6', // 标题
707+
const getTransNodeSet = new Set(['span']);
708+
// 特例集合
709+
const specialSet = new Set([
710+
'h1', 'h2', 'h3', 'h4', 'h5', 'h6', // 标题
711+
'p', // 段落(p 标签通常代表一句完整的话)
712+
"li", // 列表
710713
'yt-formatted-string', // youtube 评论
711714
]);
712715

713716
// 特例适配
714717
const getTransNodeCompat = new Map([
715-
["mvnrepository.com", function (node) {
716-
if (node.tagName.toLowerCase() === 'div' && node.classList.contains('im-description')) {
717-
return true
718-
}
719-
}],
718+
["mvnrepository.com", node => {
719+
if (node.tagName.toLowerCase() === 'div' && node.classList.contains('im-description')) return true
720+
},
721+
],
720722
]);
721723

722724
// 返回最终应该翻译的父节点或 false
723725
function getTransNode(node) {
724-
// 全局节点与空节点、class="notranslate" 的节点不翻译
725-
if (!node || node === document.body || node === document.documentElement || node.classList.contains('notranslate')) return false;
726-
727-
// 检测当前节点是否满足翻译条件
728-
if (getTransNodeSet.has(node.tagName.toLowerCase()) || detectChildMeta(node)) {
729-
return getTransNode(node.parentNode) || node; // 返回应该翻译的节点
730-
}
731-
732-
// 特例适配 是否应该翻译
726+
// 1、全局节点与空节点、class="notranslate" 的节点不翻译
727+
if (!node || node === document.body || node.tagName.toLowerCase() === "iframe" || node === document.documentElement || node.classList.contains('notranslate')) return false;
728+
// 2、特例适配标签,遇到这些标签则直接返回节点
729+
if (specialSet.has(node.tagName.toLowerCase())) return node;
730+
// 3、特例适配函数,根据 host 适配、且支持匹配 class
733731
let fn = getTransNodeCompat.get(url.host);
734732
if (fn && fn(node)) return node;
735-
733+
// 4、检测当前节点是否满足翻译条件
734+
if (getTransNodeSet.has(node.tagName.toLowerCase()) || detectChildMeta(node)) {
735+
return getTransNode(node.parentNode) || node; // 如果当前节点满足翻译条件,则向上寻找最终符合的父节点
736+
}
737+
// 5、如果节点是div并且不符合一般翻译条件,可翻译首行文本
738+
let model = util.getValue('model')
739+
if (node.tagName.toLowerCase() === 'div' && !detectChildMeta(node)) {
740+
// 遍历子节点,寻找首个文本节点或 a 标签节点
741+
let child = node.firstChild;
742+
while (child) {
743+
if (child.nodeType === Node.TEXT_NODE && child.textContent.trim() !== '') {
744+
transModelFn[model](child.textContent).then(text => {
745+
child.textContent = text;
746+
})
747+
break; // 只处理首行文本
748+
}
749+
else if (child.nodeType === Node.ELEMENT_NODE && child.tagName.toLowerCase() === 'a') {
750+
translate(child);
751+
break
752+
}
753+
child = child.nextSibling;
754+
}
755+
}
736756
// console.log('不翻译节点:', node);
737-
738757
return false
739758
}
740759

@@ -754,7 +773,6 @@ const detectChildMetaSet = new Set([
754773
function detectChildMeta(parent) {
755774
let child = parent.firstChild;
756775
while (child) {
757-
// 如果子元素不是 a、b、strong、span、p、img 标签,则返回 false
758776
if (child.nodeType === Node.ELEMENT_NODE && !detectChildMetaSet.has(child.nodeName.toLowerCase())) {
759777
return false;
760778
}
@@ -973,7 +991,7 @@ function parseJwt(token) {
973991

974992
// endregion
975993

976-
// region DeepL
994+
// region deepL
977995
// native 判断是否为本地部署模型(native 不支持 html,暂保留)
978996
function deepL(origin, native = false) {
979997
return new Promise((resolve, reject) => {
@@ -1400,15 +1418,15 @@ function reliableDetectLang(text) {
14001418
return new Promise((resolve, reject) => {
14011419
detectLang(text)
14021420
.then(resolve)
1403-
.catch(() => {
1404-
if (fail) reject('Both methods failed');
1421+
.catch(error => {
1422+
if (fail) reject('Both methods failed' + error);
14051423
else fail = true;
14061424
});
14071425

14081426
baiduDetectLang(text)
14091427
.then(resolve)
1410-
.catch(() => {
1411-
if (fail) reject('Both methods failed');
1428+
.catch(error => {
1429+
if (fail) reject('Both methods failed' + error);
14121430
else fail = true;
14131431
});
14141432
});

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@
7070
# 版本更新记录
7171

7272
- 2024-03-04:1.30版本更新
73-
- 增加 DeepL 翻译模型
74-
- 兼容移动设备,实现“三指触摸”翻译
75-
- 优化缓存逻辑
73+
1. 增加 DeepL 翻译模型
74+
2. 兼容移动设备,实现“三指触摸”
75+
3. 优化缓存逻辑、减少请求次数
7676
- 2024-02-18:1.0版本发布。
7777
1. 接入微软机器翻译
7878
2. 接入 OpenAI、智谱清言、文心一言、通义千问、Gemini、moonshot 人工智能引擎

0 commit comments

Comments
 (0)