Skip to content

Commit

Permalink
feature: 增加可靠的语言检测服务(同时请求模式,取最快返回)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bistutu committed Mar 3, 2024
1 parent f92cb98 commit 96dfd6a
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions FluentRead.js
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ const settingManager = {
mouseX = event.clientX;
mouseY = event.clientY;

handler(mouseX, mouseY, 20);
handler(mouseX, mouseY, 10);
});

// 检查是否需要拉取数据
Expand Down Expand Up @@ -799,7 +799,7 @@ function translate(node) {
if (!node.innerText.trim()) return; // 空文本,跳过

// 检测语言类型,如果是中文则不翻译
baiduDetectLang(node.innerText).then(lang => {
reliableDetectLang(node.innerText).then(lang => {
if (lang === langManager.getTo()) return; // 与目标语言相同,不翻译

// 如果是机器翻译,则翻译 outerHTML,否则递归获取文本
Expand Down Expand Up @@ -1394,6 +1394,26 @@ function detectLang(text) {
});
}

// 可靠的语言检测(竞速)
function reliableDetectLang(text) {
let fail = false;
return new Promise((resolve, reject) => {
detectLang(text)
.then(resolve)
.catch(() => {
if (fail) reject('Both methods failed');
else fail = true;
});

baiduDetectLang(text)
.then(resolve)
.catch(() => {
if (fail) reject('Both methods failed');
else fail = true;
});
});
}


// 延迟删除缓存
function delayRemoveCache(key, time = 250) {
Expand Down

0 comments on commit 96dfd6a

Please sign in to comment.