From a7b19f5b0a4b90868aa83782dd53c7399fafd2e1 Mon Sep 17 00:00:00 2001 From: ThinkStu <91378285+Bistutu@users.noreply.github.com> Date: Tue, 4 Jun 2024 21:56:14 +0800 Subject: [PATCH] =?UTF-8?q?opti:=20=E4=BC=98=E5=8C=96=E7=BF=BB=E8=AF=91?= =?UTF-8?q?=E8=A7=A6=E5=8F=91=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entrypoints/content.ts | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/entrypoints/content.ts b/entrypoints/content.ts index 3170177..a1a681b 100644 --- a/entrypoints/content.ts +++ b/entrypoints/content.ts @@ -12,24 +12,36 @@ export default defineContentScript({ cache.cleaner(); // 检测是否清理缓存 - // 鼠标移动事件监听 - const screen = {mouseX: 0, mouseY: 0, hotkeyPressed: false} - // 1、失去焦点时 hotkeyPressed = false - window.addEventListener('blur', () => screen.hotkeyPressed = false) - // 2、抬起快捷按键时 hotkeyPressed = false - window.addEventListener('keyup', event => { - if (config.hotkey === event.key) screen.hotkeyPressed = false; - }) + const screen = { mouseX: 0, mouseY: 0, hotkeyPressed: false, otherKeyPressed: false }; + + // 1. 失去焦点时 + window.addEventListener('blur', () => { + screen.hotkeyPressed = false; + screen.otherKeyPressed = false; + }); - // 3、按下快捷按键时 hotkeyPressed = true 并翻译节点 + // 2. 按下按键时 window.addEventListener('keydown', event => { if (config.hotkey === event.key) { screen.hotkeyPressed = true; - handleTranslation(screen.mouseX, screen.mouseY) + screen.otherKeyPressed = false; + } else if (screen.hotkeyPressed) { + screen.otherKeyPressed = true; + } + }); + + // 3. 抬起按键时 + window.addEventListener('keyup', event => { + if (config.hotkey === event.key) { + if (!screen.otherKeyPressed) { + handleTranslation(screen.mouseX, screen.mouseY); + } + screen.hotkeyPressed = false; + screen.otherKeyPressed = false; } - }) + }); - // 4、鼠标移动时更新位置,并根据 hotkeyPressed 决定是否触发翻译 + // 4. 鼠标移动时更新位置,并根据 hotkeyPressed 决定是否触发翻译 document.body.addEventListener('mousemove', event => { screen.mouseX = event.clientX; screen.mouseY = event.clientY;