@@ -704,37 +704,56 @@ function handler(mouseX, mouseY, time, noSkip = true) {
704
704
} , time ) ;
705
705
}
706
706
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" , // 列表
710
713
'yt-formatted-string' , // youtube 评论
711
714
] ) ;
712
715
713
716
// 特例适配
714
717
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
+ ] ,
720
722
] ) ;
721
723
722
724
// 返回最终应该翻译的父节点或 false
723
725
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
733
731
let fn = getTransNodeCompat . get ( url . host ) ;
734
732
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
+ }
736
756
// console.log('不翻译节点:', node);
737
-
738
757
return false
739
758
}
740
759
@@ -754,7 +773,6 @@ const detectChildMetaSet = new Set([
754
773
function detectChildMeta ( parent ) {
755
774
let child = parent . firstChild ;
756
775
while ( child ) {
757
- // 如果子元素不是 a、b、strong、span、p、img 标签,则返回 false
758
776
if ( child . nodeType === Node . ELEMENT_NODE && ! detectChildMetaSet . has ( child . nodeName . toLowerCase ( ) ) ) {
759
777
return false ;
760
778
}
@@ -973,7 +991,7 @@ function parseJwt(token) {
973
991
974
992
// endregion
975
993
976
- // region DeepL
994
+ // region deepL
977
995
// native 判断是否为本地部署模型(native 不支持 html,暂保留)
978
996
function deepL ( origin , native = false ) {
979
997
return new Promise ( ( resolve , reject ) => {
@@ -1400,15 +1418,15 @@ function reliableDetectLang(text) {
1400
1418
return new Promise ( ( resolve , reject ) => {
1401
1419
detectLang ( text )
1402
1420
. then ( resolve )
1403
- . catch ( ( ) => {
1404
- if ( fail ) reject ( 'Both methods failed' ) ;
1421
+ . catch ( error => {
1422
+ if ( fail ) reject ( 'Both methods failed' + error ) ;
1405
1423
else fail = true ;
1406
1424
} ) ;
1407
1425
1408
1426
baiduDetectLang ( text )
1409
1427
. then ( resolve )
1410
- . catch ( ( ) => {
1411
- if ( fail ) reject ( 'Both methods failed' ) ;
1428
+ . catch ( error => {
1429
+ if ( fail ) reject ( 'Both methods failed' + error ) ;
1412
1430
else fail = true ;
1413
1431
} ) ;
1414
1432
} ) ;
0 commit comments