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 3a69956 commit ec917c3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
16 changes: 9 additions & 7 deletions entrypoints/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Config} from "./utils/model";
import {cssInject} from "./main/css";
import {handler} from "./main/dom";
import {cache} from "./utils/cache";
import {DoubleClick} from "@/entrypoints/utils/constant";
import {constants} from "@/entrypoints/utils/constant";

export default defineContentScript({
matches: ['<all_urls>'], // 匹配所有页面
Expand Down Expand Up @@ -54,15 +54,17 @@ export default defineContentScript({
}
});

// 6、双击鼠标翻译事件
document.body.addEventListener('dblclick', event => {
if (config.hotkey == DoubleClick) {
// 通过双击事件获取鼠标位置
// 7、长按鼠标翻译事件
let timer:number; // 计时器变量用于设置延时
document.body.addEventListener('mouseup', () => clearTimeout(timer));
document.body.addEventListener('mousedown', event => {
clearTimeout(timer); // 清除之前的计时器
timer = setTimeout(() => {
console.log('长按事件触发')
let mouseX = event.clientX;
let mouseY = event.clientY;
// 调用 handler 函数进行翻译
handler(config, mouseX, mouseY);
}
}, 500) as unknown as number;
});


Expand Down
6 changes: 5 additions & 1 deletion entrypoints/utils/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ export const urls = {

export const method = {POST: "POST", GET: "GET",};

export const DoubleClick = "DoubleClick";
export const constants = {
DoubleClick : "DoubleClick",
LongPress : "LongPress",
MiddleClick : "MiddleClick",
}
5 changes: 5 additions & 0 deletions entrypoints/utils/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ export const options = {
value: 'DoubleClick',
label: "鼠标双击"
},
// 鼠标长按
{
value: 'LongPress',
label: "鼠标长按"
},
],
services: [
{
Expand Down

0 comments on commit ec917c3

Please sign in to comment.