-
Notifications
You must be signed in to change notification settings - Fork 0
/
translationService.js
113 lines (101 loc) · 3.68 KB
/
translationService.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// 翻译服务类
class TranslationService {
constructor() {
this.API_URL = 'https://translation.googleapis.com/language/translate/v2';
}
// 获取当前 API Key
async getApiKey() {
await window.configManager.init();
const apiKey = window.configManager.getApiKey();
if (!apiKey) {
throw new Error('请先配置 API Key');
}
return apiKey;
}
// 单个文本翻译
async translateText(text, targetLang = 'zh') {
try {
const apiKey = await this.getApiKey();
const response = await fetch(`${this.API_URL}?key=${apiKey}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
q: text,
target: targetLang
})
});
const data = await response.json();
return data.data.translations[0].translatedText;
} catch (error) {
console.error('翻译错误:', error);
throw error;
}
}
// 批量翻译
async batchTranslate(texts, targetLang = 'zh', batchSize = 50) {
const apiKey = await this.getApiKey();
const results = [];
for (let i = 0; i < texts.length; i += batchSize) {
const batch = texts.slice(i, i + batchSize);
const batchPromises = [];
// 将每个批次分成更小的并发请求(每5个一组)
const concurrentBatchSize = 5;
for (let j = 0; j < batch.length; j += concurrentBatchSize) {
const concurrentBatch = batch.slice(j, j + concurrentBatchSize);
const promise = fetch(`${this.API_URL}?key=${apiKey}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
q: concurrentBatch,
target: targetLang
})
}).then(response => response.json());
batchPromises.push(promise);
}
const batchResults = await Promise.all(batchPromises);
batchResults.forEach(data => {
results.push(...data.data.translations.map(t => t.translatedText));
});
// 更新进度
if (window.updateProgress) {
window.updateProgress(Math.min(i + batchSize, texts.length), texts.length);
}
// 添加延时避免请求过快
await new Promise(resolve => setTimeout(resolve, 100));
}
return results;
}
// 更新字符统计
async updateCharacterCount(characters) {
try {
const apiKey = await this.getApiKey();
await chrome.runtime.sendMessage({
action: 'updateCharacterCount',
count: characters,
apiKey: apiKey
});
} catch (error) {
console.warn('更新字符统计失败:', error);
}
}
// 检查字符限制
async checkCharacterLimit(chars) {
try {
const apiKey = await this.getApiKey();
return await chrome.runtime.sendMessage({
action: 'checkCharacterLimit',
count: chars,
apiKey: apiKey
});
} catch (error) {
console.error('检查字符限制失败:', error);
throw error;
}
}
}
// 使用全局变量导出
window.translationService = new TranslationService();