Skip to content

Commit

Permalink
优化体验:长按鼠标翻译事件
Browse files Browse the repository at this point in the history
  • Loading branch information
Bistutu committed Apr 27, 2024
1 parent dd4484f commit 333291c
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions entrypoints/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,38 @@ export default defineContentScript({
}
});

// 7、长按鼠标翻译事件
let timer:number; // 计时器变量用于设置延时
// 7、长按鼠标翻译事件(长按事件时鼠标不能移动)
let timer: number;
let startPos = {x: 0, y: 0}; // startPos 记录鼠标按下时的位置
document.body.addEventListener('mouseup', () => clearTimeout(timer));
document.body.addEventListener('mousedown', event => {
clearTimeout(timer); // 清除之前的计时器
clearTimeout(timer); // 清除之前的计时器
startPos.x = event.clientX; // 记录鼠标按下时的初始位置
startPos.y = event.clientY;
timer = setTimeout(() => {
console.log('长按事件触发')
let mouseX = event.clientX;
let mouseY = event.clientY;
handler(config, mouseX, mouseY);
}, 500) as unknown as number;
}, 500) as unknown as number;
});
document.body.addEventListener('mousemove', event => {
// 如果鼠标移动超过10像素,取消长按事件
if (Math.abs(event.clientX - startPos.x) > 10 || Math.abs(event.clientY - startPos.y) > 10) {
clearTimeout(timer);
}
});

document.body.addEventListener('mousemove', event => {
// 检测鼠标是否移动
if (Math.abs(event.clientX - startPos.x) > 10 || Math.abs(event.clientY - startPos.y) > 10) {
clearTimeout(timer); // 如果鼠标移动超过10像素,取消长按事件
}
});


// 8、鼠标中键翻译事件
document.body.addEventListener('click', event => {
if (config.hotkey===constants.MiddleClick) {
if (config.hotkey === constants.MiddleClick) {
if (event.button === 1) {
let mouseX = event.clientX;
let mouseY = event.clientY;
Expand Down

0 comments on commit 333291c

Please sign in to comment.