Skip to content

Commit

Permalink
opti:优化翻译体验
Browse files Browse the repository at this point in the history
  • Loading branch information
Bistutu committed Mar 3, 2024
1 parent fe0c13c commit 2cfad35
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
18 changes: 13 additions & 5 deletions FluentRead.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ const localStorageManager = {
const lastSessionTimestamp = localStorage.getItem('lastSessionTimestamp');
const currentTime = new Date().getTime();

// 超过 30 分钟时清空localStorage
if (!lastSessionTimestamp || currentTime - parseInt(lastSessionTimestamp) > 1800000) {
// 超过 24 小时清空 localStorage
if (!lastSessionTimestamp || currentTime - parseInt(lastSessionTimestamp) > 24 * 3600000) {
// if (!lastSessionTimestamp || currentTime - parseInt(lastSessionTimestamp) > 20000) {
localStorage.clear();
}
Expand Down Expand Up @@ -651,6 +651,14 @@ const settingManager = {
handleDOMUpdate(document.body);
});

// F2 清空当前页面所有翻译缓存
document.addEventListener('keydown', function (event) {
if (event.key === 'F2') {
localStorage.clear()
toast.fire({icon: 'success', title: '当前页面翻译缓存清空成功!'});
}
});

// 快捷键 F2,清空所有缓存
// document.addEventListener('keydown', function (event) {
// if (event.key === 'F2') {
Expand All @@ -659,6 +667,7 @@ const settingManager = {
// console.log('Cache cleared!');
// }
// });

})();

// 监听事件处理器,参数:鼠标坐标、计时器
Expand All @@ -670,7 +679,7 @@ function handler(mouseX, mouseY, time, noSkip = true) {
let node = getTransNode(document.elementFromPoint(mouseX, mouseY)); // 获取最终需要翻译的节点
if (!node) return; // 如果不需要翻译,则跳过

// console.log('翻译节点:', node);
console.log('翻译节点:', node);

if (hasLoadingSpinner(node)) return; // 如果已经在翻译,则跳过

Expand Down Expand Up @@ -723,7 +732,6 @@ const getTransNodeCompat = new Map([

// 返回最终应该翻译的父节点或 false
function getTransNode(node) {
console.log('当前节点长度:', node.textContent.length)
// 1、全局节点与空节点、文字过多的节点、class="notranslate" 的节点不翻译
if (!node || [document.documentElement, document.body].includes(node)
|| node.tagName.toLowerCase() === "iframe" || node.classList.contains('notranslate')
Expand Down Expand Up @@ -753,7 +761,7 @@ function getTransNode(node) {
child = child.nextSibling;
}
}
// console.log('不翻译节点:', node);
console.log('不翻译节点:', node);
return false
}

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@
- 2024-03-04:1.30版本更新
1. 增加 DeepL 翻译模型
2. 兼容移动设备,实现“三指触摸”
3. 优化缓存逻辑、减少请求次数
3. 优化缓存逻辑,默认翻译缓存 24h
4. 新增翻译缓存删除快捷键 `F2`
- 2024-02-18:1.0版本发布。
1. 接入微软机器翻译
2. 接入 OpenAI、智谱清言、文心一言、通义千问、Gemini、moonshot 人工智能引擎
Expand Down

0 comments on commit 2cfad35

Please sign in to comment.