Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
402 lines (336 loc) · 13.4 KB
/
Copy pathbackground.js
File metadata and controls
402 lines (336 loc) · 13.4 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// background.js - The Networker
const GOOGLE_TRANSLATE_API = 'https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=en&dt=t&q=';
const GOOGLE_OFFICIAL_API = 'https://translation.googleapis.com/language/translate/v2';
const HOURLY_LIMIT = 99; // Max requests per hour (Updated per user request)
const ONE_HOUR_MS = 60 * 60 * 1000;
const FREE_TIER_LIMIT = 495000; // 500k limit with 5k safety buffer
let requestQueue = [];
let isProcessing = false;
let lastRequestTime = 0;
let requestTimestamps = []; // Store timestamps of successful requests
let googleApiKey = '';
let gtxDailyLimit = 1000;
let officialDailyLimit = 50000;
let officialMonthlyLimit = 500000;
let gtxRpm = 12;
let minRequestInterval = 5000;
// New Settings
let priority1 = 'gtx';
let priority2 = 'official';
let priority3 = 'local';
let localUrl = 'http://localhost:1234/v1/chat/completions';
let localModel = 'local-model';
let localPrompt = 'You are a professional translator. Translate the following text to English. Return only the translated text, no explanations.';
// Load Settings from storage
chrome.storage.sync.get([
'googleApiKey', 'gtxDailyLimit', 'officialDailyLimit', 'officialMonthlyLimit', 'gtxRpm',
'priority1', 'priority2', 'priority3',
'localUrl', 'localModel', 'localPrompt'
], (result) => {
if (result.googleApiKey) googleApiKey = result.googleApiKey;
if (result.gtxDailyLimit) gtxDailyLimit = result.gtxDailyLimit;
if (result.officialDailyLimit) officialDailyLimit = result.officialDailyLimit;
if (result.officialMonthlyLimit) officialMonthlyLimit = result.officialMonthlyLimit;
if (result.gtxRpm) {
gtxRpm = result.gtxRpm;
updateInterval();
}
// Load Priorities
if (result.priority1) priority1 = result.priority1;
if (result.priority2) priority2 = result.priority2;
if (result.priority3) priority3 = result.priority3;
// Load Local LLM Settings
if (result.localUrl) localUrl = result.localUrl;
if (result.localModel) localModel = result.localModel;
if (result.localPrompt) localPrompt = result.localPrompt;
});
// Listen for Settings changes
chrome.storage.onChanged.addListener((changes, namespace) => {
if (namespace === 'sync') {
if (changes.googleApiKey) googleApiKey = changes.googleApiKey.newValue;
if (changes.gtxDailyLimit) gtxDailyLimit = changes.gtxDailyLimit.newValue;
if (changes.officialDailyLimit) officialDailyLimit = changes.officialDailyLimit.newValue;
if (changes.officialMonthlyLimit) officialMonthlyLimit = changes.officialMonthlyLimit.newValue;
if (changes.gtxRpm) {
gtxRpm = changes.gtxRpm.newValue;
updateInterval();
}
// Update Priorities
if (changes.priority1) priority1 = changes.priority1.newValue;
if (changes.priority2) priority2 = changes.priority2.newValue;
if (changes.priority3) priority3 = changes.priority3.newValue;
// Update Local LLM Settings
if (changes.localUrl) localUrl = changes.localUrl.newValue;
if (changes.localModel) localModel = changes.localModel.newValue;
if (changes.localPrompt) localPrompt = changes.localPrompt.newValue;
}
});
function updateInterval() {
// Safe bounds check: max 120 RPM (500ms), min 1 RPM (60s)
const safeRpm = Math.max(1, Math.min(gtxRpm, 120));
minRequestInterval = 60000 / safeRpm;
console.log(`GTX Request Interval updated to: ${minRequestInterval.toFixed(0)}ms (${safeRpm} RPM)`);
}
// Listener for messages from content script
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.type === 'translate') {
// We need to return true to indicate we will send a response asynchronously
handleTranslationRequest(request.text, sendResponse);
return true;
}
});
// Listener for extension icon click
chrome.action.onClicked.addListener((tab) => {
// Check if it's a Twitch page (requires host permissions for tab.url)
// Or if we can't see the URL, just try sending it anyway (content script won't answer if not there)
const isTwitch = tab.url && (tab.url.includes('twitch.tv') || tab.url.includes('twitch.com'));
if (isTwitch || !tab.url) {
chrome.tabs.sendMessage(tab.id, { type: 'toggle_settings' }, (response) => {
// Ignore errors if the content script isn't ready or listening
if (chrome.runtime.lastError) {
console.log('Toggle settings failed (content script might not be ready):', chrome.runtime.lastError.message);
}
});
}
});
function handleTranslationRequest(text, sendResponse) {
requestQueue.push({ text, sendResponse });
processQueue();
}
async function processQueue() {
if (isProcessing || requestQueue.length === 0) return;
isProcessing = true;
// Check/Reset Quotas
await checkQuotaReset(); // Monthly Official Reset
await checkOfficialDailyReset(); // Daily Official Reset
await checkGtxQuotaReset(); // Daily GTX Reset
// Clean up old timestamps for GTX Hourly limit
const now = Date.now();
requestTimestamps = requestTimestamps.filter(ts => now - ts < ONE_HOUR_MS);
const { text, sendResponse } = requestQueue.shift();
const priorities = [priority1, priority2, priority3];
let translationResult = null;
let lastError = null;
for (const engine of priorities) {
// Check Engine Availability
const availability = await checkEngineAvailability(engine, text.length);
if (!availability.available) {
console.log(`Skipping engine ${engine}: ${availability.reason}`);
lastError = availability.reason;
continue; // Try next engine
}
try {
// Pre-request logic (e.g., rate limiting for GTX)
if (engine === 'gtx') {
const timeSinceLastRequest = Date.now() - lastRequestTime;
if (timeSinceLastRequest < minRequestInterval) {
const delay = minRequestInterval - timeSinceLastRequest;
await new Promise(resolve => setTimeout(resolve, delay));
}
lastRequestTime = Date.now();
}
console.log(`Attempting translation with ${engine}...`);
let translatedText;
if (engine === 'gtx') {
translatedText = await fetchTranslationGTX(text);
// Post-request success logic for GTX
requestTimestamps.push(Date.now());
await updateGtxStats(1);
} else if (engine === 'official') {
translatedText = await fetchTranslationOfficial(text);
// Post-request logic for Official is handled inside fetchTranslationOfficial (quota update)
} else if (engine === 'local') {
translatedText = await fetchTranslationLocal(text);
}
translationResult = translatedText;
break; // Success! Exit loop
} catch (error) {
console.warn(`Translation failed with ${engine}:`, error);
lastError = error.message;
// Continue to next engine
}
}
if (translationResult) {
sendResponse({ success: true, translation: translationResult });
} else {
sendResponse({ success: false, error: lastError || 'All translation attempts failed.' });
}
isProcessing = false;
if (requestQueue.length > 0) processQueue();
}
async function checkEngineAvailability(engine, textLength) {
if (engine === 'gtx') {
const gtxStats = await getGtxStats();
if (gtxStats.dailyUsage >= gtxDailyLimit) return { available: false, reason: 'Daily GTX Limit Reached' };
if (requestTimestamps.length >= HOURLY_LIMIT) return { available: false, reason: 'Hourly GTX Limit Reached' };
return { available: true };
}
if (engine === 'official') {
if (!googleApiKey) return { available: false, reason: 'No API Key' };
const stats = await getStats();
if ((stats.monthlyUsageChars + textLength) > officialMonthlyLimit) return { available: false, reason: 'Monthly Official Limit Exceeded' };
if ((stats.dailyUsageChars + textLength) > officialDailyLimit) return { available: false, reason: 'Daily Official Limit Exceeded' };
return { available: true };
}
if (engine === 'local') {
if (!localUrl) return { available: false, reason: 'No Local URL Configured' };
return { available: true }; // No limits for local
}
return { available: false, reason: 'Unknown Engine' };
}
async function fetchTranslationGTX(text) {
const url = GOOGLE_TRANSLATE_API + encodeURIComponent(text);
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
// Parse Google's nested JSON format
if (Array.isArray(data) && Array.isArray(data[0])) {
return data[0].map(item => item[0]).join('');
}
throw new Error('Invalid response format');
}
async function fetchTranslationOfficial(text) {
// 1. Quota checks are already done in checkEngineAvailability, but we double check here to be safe and because we update stats here?
// Actually, checkEngineAvailability is a "soft" check.
// The official API function logic handles the "hard" check and update.
const stats = await getStats();
// 2. Perform Request
const response = await fetch(`${GOOGLE_OFFICIAL_API}?key=${googleApiKey}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
q: text,
target: 'en',
format: 'text'
})
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error ? errorData.error.message : `API Error ${response.status}`);
}
const data = await response.json();
if (data.data && data.data.translations && data.data.translations.length > 0) {
const translation = data.data.translations[0].translatedText;
// 3. Update Quota
await updateStats(text.length);
// Decode HTML entities
return translation
.replace(/"/g, '"')
.replace(/'/g, "'")
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>');
}
throw new Error('Invalid Official API response');
}
async function fetchTranslationLocal(text) {
const payload = {
model: localModel,
messages: [
{ role: "system", content: localPrompt },
{ role: "user", content: text }
],
temperature: 0.3
};
const response = await fetch(localUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Local LLM Error ${response.status}: ${errorText}`);
}
const data = await response.json();
if (data.choices && data.choices.length > 0 && data.choices[0].message) {
return data.choices[0].message.content.trim();
}
throw new Error('Invalid Local LLM response format');
}
// Quota Management Helpers
async function getStats() {
return new Promise((resolve) => {
chrome.storage.sync.get(['stats'], (result) => {
// Initialize with defaults if missing
const stats = result.stats || {};
stats.monthlyUsageChars = stats.monthlyUsageChars || 0;
stats.dailyUsageChars = stats.dailyUsageChars || 0;
stats.lastResetDate = stats.lastResetDate || null;
stats.lastDailyResetDate = stats.lastDailyResetDate || null;
resolve(stats);
});
});
}
async function updateStats(charsAdded) {
const stats = await getStats();
stats.monthlyUsageChars += charsAdded;
stats.dailyUsageChars += charsAdded;
return new Promise((resolve) => {
chrome.storage.sync.set({ stats }, resolve);
});
}
async function checkQuotaReset() {
const stats = await getStats();
const now = new Date();
// Convert to Pacific Time for Billing Cycle
const pacificTime = new Date(now.toLocaleString("en-US", {timeZone: "America/Los_Angeles"}));
const currentMonth = pacificTime.getMonth();
const currentYear = pacificTime.getFullYear();
const currentPeriod = `${currentMonth}-${currentYear}`;
if (stats.lastResetPeriod !== currentPeriod) {
console.log(`New billing month detected (${currentPeriod}). Resetting official quota.`);
stats.monthlyUsageChars = 0;
stats.lastResetPeriod = currentPeriod;
stats.lastResetDate = now.toISOString();
await new Promise((resolve) => {
chrome.storage.sync.set({ stats }, resolve);
});
}
}
async function checkOfficialDailyReset() {
const stats = await getStats();
const now = new Date();
const today = now.toDateString(); // Local Time Reset
if (stats.lastDailyResetDate !== today) {
console.log(`New day detected (${today}). Resetting Official API daily quota.`);
stats.dailyUsageChars = 0;
stats.lastDailyResetDate = today;
await new Promise((resolve) => {
chrome.storage.sync.set({ stats }, resolve);
});
}
}
// GTX Daily Quota Helpers
async function getGtxStats() {
return new Promise((resolve) => {
chrome.storage.sync.get(['gtxStats'], (result) => {
resolve(result.gtxStats || { dailyUsage: 0, lastResetDay: null });
});
});
}
async function updateGtxStats(count) {
const stats = await getGtxStats();
stats.dailyUsage += count;
return new Promise((resolve) => {
chrome.storage.sync.set({ gtxStats: stats }, resolve);
});
}
async function checkGtxQuotaReset() {
const stats = await getGtxStats();
const now = new Date();
const today = now.toDateString(); // "Mon Dec 15 2025" - local time reset seems appropriate for user preference
if (stats.lastResetDay !== today) {
console.log(`New day detected (${today}). Resetting GTX daily quota.`);
stats.dailyUsage = 0;
stats.lastResetDay = today;
await new Promise((resolve) => {
chrome.storage.sync.set({ gtxStats: stats }, resolve);
});
}
}