Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
320 lines (287 loc) · 8.98 KB
/
Copy pathbackground.js
File metadata and controls
320 lines (287 loc) · 8.98 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/**
* 划词翻译 - 后台服务脚本
*/
// 安装时初始化
chrome.runtime.onInstalled.addListener(function(details) {
if (details.reason === 'install') {
// 首次安装,设置默认配置
chrome.storage.sync.set({
targetLang: 'zh',
sourceLang: 'auto',
triggerMode: 'auto',
showIcon: true,
autoPlay: false,
theme: 'light',
translationEngine: 'google',
enableHistory: true
});
// 创建右键菜单
createContextMenus();
// 打开欢迎页面或设置页面
chrome.tabs.create({
url: 'options.html'
});
} else if (details.reason === 'update') {
// 更新时重新创建菜单
createContextMenus();
}
});
/**
* 创建右键菜单
*/
function createContextMenus() {
// 清除现有菜单
chrome.contextMenus.removeAll(function() {
// 主菜单
chrome.contextMenus.create({
id: 'selection-translator',
title: '划词翻译',
contexts: ['selection']
});
// 翻译选中文本
chrome.contextMenus.create({
id: 'translate-selection',
parentId: 'selection-translator',
title: '翻译选中文本',
contexts: ['selection']
});
// 分隔线
chrome.contextMenus.create({
id: 'separator-1',
parentId: 'selection-translator',
type: 'separator',
contexts: ['selection']
});
// 复制翻译结果
chrome.contextMenus.create({
id: 'copy-translation',
parentId: 'selection-translator',
title: '复制翻译结果',
contexts: ['selection']
});
// 朗读选中文本
chrome.contextMenus.create({
id: 'speak-selection',
parentId: 'selection-translator',
title: '朗读选中文本',
contexts: ['selection']
});
// 分隔线
chrome.contextMenus.create({
id: 'separator-2',
parentId: 'selection-translator',
type: 'separator',
contexts: ['selection']
});
// 打开设置
chrome.contextMenus.create({
id: 'open-settings',
parentId: 'selection-translator',
title: '打开设置',
contexts: ['selection']
});
});
}
// 监听右键菜单点击
chrome.contextMenus.onClicked.addListener(function(info, tab) {
switch (info.menuItemId) {
case 'translate-selection':
chrome.tabs.sendMessage(tab.id, {
action: 'context-translate',
text: info.selectionText
});
break;
case 'copy-translation':
chrome.tabs.sendMessage(tab.id, {
action: 'copy-translation',
text: info.selectionText
});
break;
case 'speak-selection':
chrome.tabs.sendMessage(tab.id, {
action: 'speak-selection',
text: info.selectionText
});
break;
case 'open-settings':
chrome.runtime.openOptionsPage();
break;
}
});
// 监听来自内容脚本的消息
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
switch (request.action) {
case 'translate':
// 后台翻译逻辑
sendResponse({ success: true });
break;
case 'save-to-history':
// 保存到翻译历史
saveToHistory(request.data);
sendResponse({ success: true });
break;
case 'get-history':
// 获取翻译历史
getHistory(function(history) {
sendResponse({ history: history });
});
return true; // 异步响应
case 'clear-history':
// 清空历史
clearHistory();
sendResponse({ success: true });
break;
case 'proxyFetch': {
// 后台代发网络请求:持有 <all_urls> host 权限,可绕过内容脚本的 CORS 限制
const { url, options, timeoutMs } = request;
const controller = new AbortController();
const tid = setTimeout(() => controller.abort(), timeoutMs || 8000);
const opts = Object.assign({}, options || {});
delete opts.signal; // 信号不可序列化,后台自建
try {
fetch(url, Object.assign(opts, { signal: controller.signal }))
.then(async (res) => {
clearTimeout(tid);
const ct = res.headers && res.headers.get ? (res.headers.get('content-type') || '') : '';
let json = null, text = null;
try {
if (ct.indexOf('application/json') !== -1) json = await res.json();
else text = await res.text();
} catch (e) {
try { text = await res.text(); } catch (e2) { text = ''; }
}
sendResponse({ ok: res.ok, status: res.status, json, text });
})
.catch((err) => {
clearTimeout(tid);
sendResponse({
error: (err && err.name === 'AbortError') ? '请求超时' :
(err && err.message ? err.message : String(err))
});
});
} catch (e) {
clearTimeout(tid);
sendResponse({ error: e.message || String(e) });
}
return true; // 异步响应
}
case 'ocr': {
handleOcr(request, sendResponse);
return true; // 异步响应
}
}
return true;
});
/**
* 保存到翻译历史
*/
function saveToHistory(data) {
chrome.storage.local.get(['translationHistory'], function(result) {
let history = result.translationHistory || [];
// 添加新记录到开头
history.unshift({
id: Date.now(),
sourceText: data.sourceText,
translatedText: data.translatedText,
sourceLang: data.sourceLang,
targetLang: data.targetLang,
timestamp: new Date().toISOString()
});
// 只保留最近100条
if (history.length > 100) {
history = history.slice(0, 100);
}
chrome.storage.local.set({ translationHistory: history });
});
}
/**
* 获取翻译历史
*/
function getHistory(callback) {
chrome.storage.local.get(['translationHistory'], function(result) {
callback(result.translationHistory || []);
});
}
/**
* 清空历史
*/
function clearHistory() {
chrome.storage.local.remove(['translationHistory']);
}
// 添加快捷键命令监听
chrome.commands.onCommand.addListener(function(command) {
if (command === 'translate-selection') {
// 尊重设置页的「启用快捷键」开关
chrome.storage.sync.get({ enableShortcut: true }, function(result) {
if (!result.enableShortcut) return;
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
if (tabs[0]) {
chrome.tabs.sendMessage(tabs[0].id, { action: 'translate-selection' });
}
});
});
}
});
/**
* 截图翻译功能
*/
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.action === 'capture-screenshot') {
// 获取当前活动标签页
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
if (tabs[0]) {
// 截取当前标签页的屏幕
chrome.tabs.captureVisibleTab(tabs[0].windowId, { format: 'png' }, function(dataUrl) {
if (chrome.runtime.lastError) {
sendResponse({ error: chrome.runtime.lastError.message });
} else {
sendResponse({ screenshot: dataUrl });
}
});
} else {
sendResponse({ error: 'No active tab found' });
}
});
return true; // 异步响应
}
});
/**
* 免费 OCR:调用 OCR.space 的文字识别接口。
* 这里是「调用远程服务接口」,不是「加载远程代码」,
* 因此符合 MV3 的 CSP 约束(不会再触发 Insecure CSP value 报错)。
* 默认使用官方公开的免费体验 key(有频率与体积限制),
* 用户可在设置页填入自己申请的免费 key 以获得更高配额。
*/
async function handleOcr(request, sendResponse) {
try {
const stored = await chrome.storage.sync.get({ ocrKey: '' });
const apiKey = (stored.ocrKey && String(stored.ocrKey).trim()) || 'helloworld';
const body = new URLSearchParams();
body.set('apikey', apiKey);
body.set('base64Image', request.image);
body.set('language', request.lang || 'chs');
body.set('isOverlayRequired', 'false');
body.set('detectOrientation', 'true');
body.set('scale', 'true');
const res = await fetch('https://api.ocr.space/parse/image', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: body.toString()
});
if (!res.ok) throw new Error('OCR 服务返回 HTTP ' + res.status);
const data = await res.json();
if (data.IsErroredOnProcessing) {
const msg =
(data.ErrorMessage && data.ErrorMessage[0]) ||
(data.ParsedResults && data.ParsedResults[0] && data.ParsedResults[0].ErrorMessage) ||
'OCR 处理失败';
throw new Error(String(msg));
}
const results = data.ParsedResults || [];
const text = results.map(r => (r.ParsedText || '')).join('\n').trim();
sendResponse({ text });
} catch (e) {
sendResponse({ error: (e && e.message) ? e.message : 'OCR 识别失败' });
}
}
console.log('[Selection Translator] 后台服务已启动');