-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
402 lines (338 loc) · 12.5 KB
/
content.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
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
console.log('Content script loaded');
// Constants
const ICON_SIZE = 20;
const QUICK_ICON_ID = 'translation-quick-icon';
// Utility functions
const getConfig = () => {
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage({action: "getConfig"}, (response) => {
chrome.runtime.lastError ? reject(chrome.runtime.lastError) : resolve(response);
});
});
};
const mapLanguageCode = (code, service) => {
const mapping = {
// English
'en': { google: 'en', deepl: 'EN', mymemory: 'eng' },
// Chinese (Simplified)
'zh': { google: 'zh-CN', deepl: 'ZH', mymemory: 'chi' },
'zh-CN': { google: 'zh-CN', deepl: 'ZH', mymemory: 'chi' },
// Chinese (Traditional)
'zh-TW': { google: 'zh-TW', deepl: 'ZH', mymemory: 'cht' },
// Japanese
'ja': { google: 'ja', deepl: 'JA', mymemory: 'jpn' },
};
if (mapping[code] && mapping[code][service]) {
return mapping[code][service];
}
// If no specific mapping is found, return the original code
return code;
};
// UI-related functions
const createQuickIcon = (iconURL) => {
const quickIcon = document.createElement('div');
quickIcon.id = QUICK_ICON_ID;
if (iconURL) {
quickIcon.innerHTML = `<img src="${iconURL}" width="${ICON_SIZE}" height="${ICON_SIZE}">`;
} else {
quickIcon.textContent = '🌐';
}
quickIcon.style.cssText = `
position: absolute !important;
z-index: 2147483647 !important;
cursor: pointer !important;
background: white !important;
border: 1px solid #ccc !important;
border-radius: 50% !important;
width: 25px !important;
height: 25px !important;
text-align: center !important;
line-height: 25px !important;
font-size: 16px !important;
box-shadow: 0 2px 5px rgba(0,0,0,0.2) !important;
display: none !important;
`;
document.body.appendChild(quickIcon);
};
const showQuickIcon = (event) => {
const quickIcon = document.getElementById(QUICK_ICON_ID);
if (!quickIcon) {
return;
}
const selection = window.getSelection();
if (selection.toString().trim().length > 0) {
const range = selection.getRangeAt(0);
const rect = range.getBoundingClientRect();
// Position the icon to the right of the selection
quickIcon.style.left = `${rect.right + 5}px`;
quickIcon.style.top = `${rect.top + window.scrollY}px`;
quickIcon.style.setProperty('display', 'block', 'important');
// Force a repaint
quickIcon.offsetHeight;
} else {
quickIcon.style.setProperty('display', 'none', 'important');
}
};
// Translation-related functions
const detectLanguage = (text) => {
const apiUrl = `https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=en&dt=t&q=${encodeURIComponent(text)}`;
return fetch(apiUrl)
.then(response => response.json())
.then(data => {
if (data && data[2]) {
return data[2];
}
throw new Error('Unable to detect language');
});
};
const translateText = async (text, targetLang = 'en') => {
const quickIcon = document.getElementById(QUICK_ICON_ID);
if (quickIcon) quickIcon.style.display = 'none !important';
// Reset translations object for new translation
const translations = {};
// First, detect the language
const detectedLang = await detectLanguage(text);
console.log('Detected language:', detectedLang);
// Now proceed with translations using the detected language
await Promise.all([
translateWithGoogle(text, detectedLang, targetLang, translations),
translateWithDeepL(text, detectedLang, targetLang, translations),
translateWithMyMemory(text, detectedLang, targetLang, translations)
]);
showTranslation(translations, targetLang, text, detectedLang);
};
const translateWithGoogle = (text, sourceLang, targetLang, translations) => {
const mappedSourceLang = mapLanguageCode(sourceLang, 'google');
const mappedTargetLang = mapLanguageCode(targetLang, 'google');
return fetch(`https://translate.googleapis.com/translate_a/single?client=gtx&sl=${mappedSourceLang}&tl=${mappedTargetLang}&dt=t&q=${encodeURIComponent(text)}`)
.then(response => response.json())
.then(data => {
translations['google'] = data[0][0][0];
})
.catch(error => {
console.error('Google translation error:', error);
translations['google'] = 'Error: ' + error.message;
});
};
const translateWithDeepL = async (text, sourceLang, targetLang, translations) => {
const mappedSourceLang = mapLanguageCode(sourceLang, 'deepl');
const mappedTargetLang = mapLanguageCode(targetLang, 'deepl');
const config = await getConfig();
const deeplApiKeys = config.DEEPL_API_KEYS;
const apiKey = getNextDeeplApiKey(deeplApiKeys);
if (!apiKey) {
translations['deepl'] = 'Error: All API keys exhausted';
return;
}
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage({
action: 'translateWithDeepL',
text,
sourceLang: mappedSourceLang,
targetLang: mappedTargetLang,
apiKey
}, response => {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
translations['deepl'] = 'Error: ' + chrome.runtime.lastError.message;
reject(chrome.runtime.lastError);
} else if (response.success) {
translations['deepl'] = response.translation;
resolve();
} else {
translations['deepl'] = 'Error: ' + response.error;
reject(new Error(response.error));
}
});
});
};
const translateWithMyMemory = (text, sourceLang, targetLang, translations) => {
const mappedSourceLang = mapLanguageCode(sourceLang, 'mymemory');
const mappedTargetLang = mapLanguageCode(targetLang, 'mymemory');
const apiUrl = `https://api.mymemory.translated.net/get?q=${encodeURIComponent(text)}&langpair=${mappedSourceLang}|${mappedTargetLang}`;
return fetch(apiUrl)
.then(response => response.json())
.then(data => {
if (data.responseData && data.responseData.translatedText) {
translations['mymemory'] = data.responseData.translatedText;
} else {
throw new Error('No translation found in the response');
}
})
.catch(error => {
// console.error('MyMemory translation error:', error);
translations['mymemory'] = 'Error: ' + error.message;
});
};
// Audio-related functions
const convertToKatakana = async (text) => {
const config = await getConfig();
const apiKey = config.GOO_LABS_API_KEY;
const apiUrl = 'https://labs.goo.ne.jp/api/hiragana';
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
app_id: apiKey,
sentence: text,
output_type: 'katakana',
}),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data.converted;
};
const playAudio = (text, lang) => {
const utterance = new SpeechSynthesisUtterance(text);
utterance.lang = lang;
// Find a voice that matches the language
const voices = speechSynthesis.getVoices();
const voice = voices.find(voice => voice.lang.startsWith(lang)) || voices[0];
if (voice) {
utterance.voice = voice;
}
speechSynthesis.speak(utterance);
};
// UI components
const createTooltip = (translations, currentTargetLang, originalText, sourceLang) => {
const tooltip = document.createElement('div');
tooltip.className = 'translation-tooltip';
tooltip.id = 'my-extension-tooltip';
tooltip.innerHTML = `
<select class="lang-select">
<option value="en" ${currentTargetLang === 'en' ? 'selected' : ''}>English</option>
<option value="zh-CN" ${currentTargetLang === 'zh-CN' ? 'selected' : ''}>Chinese (Simplified)</option>
<option value="ja" ${currentTargetLang === 'ja' ? 'selected' : ''}>Japanese</option>
</select>
<div class="original-container">
<div class="original-text">${originalText}
<button class="audio-btn" data-text="${originalText}" data-lang="${sourceLang}">🔊</button></div>
${sourceLang === 'ja' ? `<div class="katakana-text">Converting to katakana...</div>` : ''}
</div>
<div class="translations">
${Object.entries(translations).map(([source, translation]) => `
<div class="translation-item">
<h4>${source}</h4>
<p>
${translation}
<button class="audio-btn" data-text="${translation}" data-lang="${currentTargetLang}">🔊</button>
</p>
</div>
`).join('')}
</div>
`;
// Add event listeners for audio buttons
const audioButtons = tooltip.querySelectorAll('.audio-btn');
audioButtons.forEach(button => {
button.addEventListener('click', () => {
const text = button.getAttribute('data-text');
const lang = button.getAttribute('data-lang');
playAudio(text, lang);
});
});
const langSelect = tooltip.querySelector('.lang-select');
langSelect.addEventListener('change', (event) => {
const newTargetLang = event.target.value;
translateText(originalText, newTargetLang);
});
if (sourceLang === 'ja') {
const katakanaDiv = tooltip.querySelector('.katakana-text');
convertToKatakana(originalText).then(katakana => {
katakanaDiv.textContent = katakana;
}).catch(error => {
console.error('Error converting to katakana:', error);
katakanaDiv.textContent = 'Error converting to katakana';
});
}
return tooltip;
};
const showTranslation = (translations, currentTargetLang, originalText, sourceLang) => {
// Remove any existing tooltips
const existingTooltips = document.querySelectorAll('.translation-tooltip');
existingTooltips.forEach(tooltip => tooltip.remove());
const tooltip = createTooltip(translations, currentTargetLang, originalText, sourceLang);
document.body.appendChild(tooltip);
// Position the tooltip near the selected text
const selection = window.getSelection();
if (selection.rangeCount > 0) {
const range = selection.getRangeAt(0);
const rect = range.getBoundingClientRect();
const scrollX = window.pageXOffset || document.documentElement.scrollLeft;
const scrollY = window.pageYOffset || document.documentElement.scrollTop;
tooltip.style.left = `${rect.left + scrollX}px`;
tooltip.style.top = `${rect.bottom + scrollY + 5}px`;
// Adjust position if tooltip goes off-screen
const tooltipRect = tooltip.getBoundingClientRect();
if (tooltipRect.right > window.innerWidth) {
tooltip.style.left = `${window.innerWidth - tooltipRect.width - 5}px`;
}
if (tooltipRect.bottom > window.innerHeight) {
tooltip.style.top = `${rect.top + scrollY - tooltipRect.height - 5}px`;
}
} else {
// Fallback to center of the viewport if no selection
tooltip.style.left = '50%';
tooltip.style.top = '50%';
tooltip.style.transform = 'translate(-50%, -50%)';
}
// Remove the tooltip after 30 seconds
// setTimeout(() => {
// tooltip.remove();
// }, 30000);
// Remove the tooltip when clicking outside of it
document.addEventListener('click', function removeTooltip(e) {
if (!tooltip.contains(e.target)) {
tooltip.remove();
document.removeEventListener('click', removeTooltip);
}
});
};
// DeepL API key management
let currentDeeplKeyIndex = 0;
const getNextDeeplApiKey = (deeplApiKeys) => {
if (currentDeeplKeyIndex < deeplApiKeys.length) {
const apiKey = deeplApiKeys[currentDeeplKeyIndex];
currentDeeplKeyIndex++;
return apiKey;
}
return null;
};
// Event listeners and initialization
document.addEventListener('mouseup', showQuickIcon);
document.addEventListener('click', (event) => {
const quickIcon = document.getElementById(QUICK_ICON_ID);
if (quickIcon && event.target.closest(`#${QUICK_ICON_ID}`)) {
const selectedText = window.getSelection().toString();
translateText(selectedText);
}
});
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'translate') {
console.log('Translating:', request.text);
translateText(request.text, request.targetLang)
.then(() => sendResponse({ success: true }))
.catch(error => {
console.error('Translation error:', error);
sendResponse({ success: false, error: error.message });
});
return true;
}
});
// Initialization
(async () => {
if (!('speechSynthesis' in window)) {
alert("Web Speech API not supported :-(");
return;
}
const { iconURL } = await new Promise(resolve => {
chrome.runtime.sendMessage({action: "getIconURL"}, resolve);
});
createQuickIcon(iconURL);
speechSynthesis.onvoiceschanged = () => {
speechSynthesis.getVoices();
};
})();