Skip to content

Commit

Permalink
feature: 增加鼠标双击翻译
Browse files Browse the repository at this point in the history
  • Loading branch information
Bistutu committed Apr 26, 2024
1 parent 7f426a8 commit 3a69956
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
7 changes: 6 additions & 1 deletion entrypoints/compat/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type SelectFunction = (url: any) => any;
interface ReplaceCompatFn {
[domain: string]: ReplaceFunction;
}

interface SelectCompatFn {
[domain: string]: SelectFunction;
}
Expand Down Expand Up @@ -41,5 +42,9 @@ export const selectCompatFn: SelectCompatFn = {
['www.webtrees.net']: (node: any) => {
// class='kmsg'
if (node.tagName.toLowerCase() === 'div' && node.classList.contains('kmsg')) return true
}
},
['webtrees.net']: (node: any) => {
// class='kmsg'
if (node.tagName.toLowerCase() === 'div' && node.classList.contains('kmsg')) return true
},
}
13 changes: 12 additions & 1 deletion entrypoints/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +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";

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

// todo 2024.4.26 双击翻译)开关控制
// 6、双击鼠标翻译事件
document.body.addEventListener('dblclick', event => {
if (config.hotkey == DoubleClick) {
// 通过双击事件获取鼠标位置
let mouseX = event.clientX;
let mouseY = event.clientY;
// 调用 handler 函数进行翻译
handler(config, mouseX, mouseY);
}
});


// background.ts
browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
Expand Down
4 changes: 3 additions & 1 deletion entrypoints/utils/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ export const urls = {
[services.baidu]: "https://fanyi-api.baidu.com/api/trans/vip/translate",
}

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

export const DoubleClick = "DoubleClick";
5 changes: 5 additions & 0 deletions entrypoints/utils/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ export const options = {
value: "`",
label: "波浪号键"
},
// 鼠标双击左键
{
value: 'DoubleClick',
label: "鼠标双击"
},
],
services: [
{
Expand Down

0 comments on commit 3a69956

Please sign in to comment.