-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
96 lines (89 loc) · 2.73 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
console.log("background.js 已加载");
// 创建右键菜单
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: "translateText",
title: "deepseek翻译选中文本",
contexts: ["selection"]
}, () => {
if (chrome.runtime.lastError) {
console.error("右键菜单创建失败:", chrome.runtime.lastError.message || chrome.runtime.lastError);
} else {
console.log("右键菜单创建成功!");
}
});
});
// 监听右键菜单点击事件
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "translateText" && info.selectionText) {
console.log("选中的文本:", info.selectionText);
const selectedText = info.selectionText;
// 获取保存的 API Key
chrome.storage.sync.get('apiKey', function (data) {
console.log("获取的 API Key:", data.apiKey);
if (data.apiKey) {
// 在用户手势的上下文中打开侧边栏
chrome.sidePanel.open({ tabId: tab.id });
// 调用 DeepSeek API 进行翻译
translateText(data.apiKey, selectedText)
.then((translation) => {
// 将翻译结果发送到侧边栏
chrome.runtime.sendMessage({
action: "showTranslation",
translation: translation,
}, (response) => {
if (chrome.runtime.lastError) {
console.error("消息发送失败:", chrome.runtime.lastError.message || chrome.runtime.lastError);
} else {
console.log("消息发送成功");
}
});
})
.catch((error) => {
console.error("翻译失败:", error);
});
} else {
console.error("API Key 未设置");
}
});
} else {
console.error("未选中文本或右键菜单点击失败");
}
});
// 翻译逻辑
function translateText(apiKey, text) {
const url = 'https://api.deepseek.com/chat/completions';
const prompt = `请将以下内容翻译成中文:${text}`;
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify({
model: 'deepseek-chat',
messages: [
{
role: 'user',
content: prompt
}
],
max_tokens: 1000
})
};
return fetch(url, options)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(data => {
const translation = data.choices[0].message.content;
return translation;
})
.catch(error => {
console.error('Error:', error);
throw error;
});
}