-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
357 lines (301 loc) · 10.3 KB
/
content.js
File metadata and controls
357 lines (301 loc) · 10.3 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
// ============================================================================
// Content Script for Romaji Translator
// Handles popup display and user interaction
// ============================================================================
let activePopup = null;
let currentTranslation = null;
// ============================================================================
// Message Listener
// ============================================================================
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "translate") {
handleTranslation(request.text);
sendResponse({ success: true });
}
return true;
});
// ============================================================================
// Translation Handler
// ============================================================================
async function handleTranslation(romajiText) {
// Remove any existing popup
removePopup();
// Get selection position for popup placement
const selection = window.getSelection();
let rect = null;
if (selection.rangeCount > 0) {
const range = selection.getRangeAt(0);
rect = range.getBoundingClientRect();
}
// Quick hiragana conversion first (doesn't need API)
let quickHiragana = "Converting...";
try {
// Use the tokenizer for quick hiragana (this is synchronous)
if (typeof tokenizeRomaji === 'function') {
const tokens = tokenizeRomaji(romajiText);
quickHiragana = tokens.map(t => t.hiragana).join('');
}
} catch (e) {
console.warn("Quick conversion failed:", e);
}
// Create and show popup with loading state (but show hiragana immediately)
showPopup({
romaji: romajiText,
hiragana: quickHiragana,
english: "Translating...",
loading: true
}, rect);
// Perform full translation (includes API call for English)
try {
const result = await translateRomaji(romajiText);
currentTranslation = result;
// Update popup with full translation
updatePopup(result);
} catch (error) {
console.error("Translation error:", error);
// Still show the hiragana even if English translation fails
updatePopup({
romaji: romajiText,
hiragana: quickHiragana !== "Converting..." ? quickHiragana : "Conversion failed",
english: "English translation unavailable - check your connection",
error: true,
confidence: 0.3
});
}
}
// ============================================================================
// Popup Display Functions
// ============================================================================
/**
* Create and display the translation popup
*/
function showPopup(data, rect) {
// Create popup container
const popup = document.createElement('div');
popup.className = 'romaji-translator-popup';
popup.id = 'romaji-translator-popup';
// Build popup content
popup.innerHTML = buildPopupHTML(data);
// Add to DOM first (needed for dimension calculations)
document.body.appendChild(popup);
// Position popup
positionPopup(popup, rect);
activePopup = popup;
// Add event listeners
setupPopupListeners(popup);
}
/**
* Build the popup HTML content
*/
function buildPopupHTML(data) {
const { romaji, hiragana, english, alternatives, confidence, loading, error } = data;
// Confidence indicator (if available)
let confidenceHTML = '';
if (typeof confidence === 'number' && confidence > 0 && !loading) {
const level = confidence > 0.8 ? 'high' : confidence > 0.5 ? 'medium' : 'low';
confidenceHTML = `<span class="romaji-confidence romaji-confidence-${level}" title="Confidence: ${Math.round(confidence * 100)}%"></span>`;
}
// Loading spinner
const loadingClass = loading ? 'romaji-loading' : '';
// Error styling
const errorClass = error ? 'romaji-error' : '';
// Alternatives section (multiple meanings)
let alternativesHTML = '';
if (alternatives && alternatives.length > 0) {
const altItems = alternatives.slice(0, 3).map(alt =>
`<div class="romaji-alt-item">• ${escapeHtml(alt)}</div>`
).join('');
alternativesHTML = `
<div class="romaji-alternatives">
<div class="romaji-alt-header">Other meanings:</div>
${altItems}
</div>
`;
}
return `
<div class="romaji-popup-content ${loadingClass} ${errorClass}">
<div class="romaji-header">
${confidenceHTML}
<div class="romaji-original">${escapeHtml(romaji)}</div>
</div>
<div class="romaji-hiragana">${escapeHtml(hiragana)}</div>
<div class="romaji-english">${escapeHtml(english)}</div>
${alternativesHTML}
</div>
<button class="romaji-close" aria-label="Close">×</button>
`;
}
/**
* Position the popup near the selection
*/
function positionPopup(popup, rect) {
if (!rect) {
// Fallback: center on screen
popup.style.position = 'fixed';
popup.style.top = '50%';
popup.style.left = '50%';
popup.style.transform = 'translate(-50%, -50%)';
return;
}
const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
const scrollLeft = window.pageXOffset || document.documentElement.scrollLeft;
// Start below the selection
let top = rect.bottom + scrollTop + 8;
let left = rect.left + scrollLeft;
// Set initial position
popup.style.top = `${top}px`;
popup.style.left = `${left}px`;
// Get popup dimensions after render
const popupRect = popup.getBoundingClientRect();
// Adjust if would go off right edge
if (popupRect.right > window.innerWidth - 10) {
left = Math.max(10, window.innerWidth - popupRect.width - 10 + scrollLeft);
popup.style.left = `${left}px`;
}
// Adjust if would go off bottom - show above selection instead
if (popupRect.bottom > window.innerHeight - 10) {
top = rect.top + scrollTop - popupRect.height - 8;
// Make sure we don't go off the top
if (top < scrollTop + 10) {
top = scrollTop + 10;
}
popup.style.top = `${top}px`;
}
// Adjust if would go off left edge
if (left < scrollLeft + 10) {
popup.style.left = `${scrollLeft + 10}px`;
}
}
/**
* Update popup content after translation completes
*/
function updatePopup(data) {
if (!activePopup) return;
const content = activePopup.querySelector('.romaji-popup-content');
if (!content) return;
// Remove loading state
content.classList.remove('romaji-loading');
// Update hiragana
const hiraganaDiv = activePopup.querySelector('.romaji-hiragana');
if (hiraganaDiv) {
hiraganaDiv.textContent = data.hiragana || '';
}
// Update English translation
const englishDiv = activePopup.querySelector('.romaji-english');
if (englishDiv) {
englishDiv.textContent = data.english || '';
if (data.error) {
englishDiv.classList.add('romaji-error-text');
}
}
// Add alternatives if present
if (data.alternatives && data.alternatives.length > 0) {
let altContainer = activePopup.querySelector('.romaji-alternatives');
if (!altContainer) {
altContainer = document.createElement('div');
altContainer.className = 'romaji-alternatives';
content.appendChild(altContainer);
}
const altItems = data.alternatives.slice(0, 3).map(alt =>
`<div class="romaji-alt-item">• ${escapeHtml(alt)}</div>`
).join('');
altContainer.innerHTML = `
<div class="romaji-alt-header">Other meanings:</div>
${altItems}
`;
}
// Update confidence indicator
if (typeof data.confidence === 'number') {
let confidenceEl = activePopup.querySelector('.romaji-confidence');
if (!confidenceEl) {
confidenceEl = document.createElement('span');
confidenceEl.className = 'romaji-confidence';
const header = activePopup.querySelector('.romaji-header');
// Insert at the beginning (before the romaji text)
if (header) header.insertBefore(confidenceEl, header.firstChild);
}
const level = data.confidence > 0.8 ? 'high' : data.confidence > 0.5 ? 'medium' : 'low';
confidenceEl.className = `romaji-confidence romaji-confidence-${level}`;
confidenceEl.title = `Confidence: ${Math.round(data.confidence * 100)}%`;
}
}
/**
* Setup event listeners for popup
*/
function setupPopupListeners(popup) {
// Close button
const closeBtn = popup.querySelector('.romaji-close');
if (closeBtn) {
closeBtn.addEventListener('click', (e) => {
e.stopPropagation();
removePopup();
});
}
// Prevent popup clicks from closing it
popup.addEventListener('click', (e) => {
e.stopPropagation();
});
// Close on outside click (with small delay to avoid immediate close)
setTimeout(() => {
document.addEventListener('click', handleOutsideClick);
document.addEventListener('keydown', handleEscapeKey);
}, 150);
}
/**
* Remove popup from page
*/
function removePopup() {
if (activePopup) {
activePopup.remove();
activePopup = null;
currentTranslation = null;
// Remove event listeners
document.removeEventListener('click', handleOutsideClick);
document.removeEventListener('keydown', handleEscapeKey);
}
}
/**
* Handle clicks outside popup
*/
function handleOutsideClick(event) {
if (activePopup && !activePopup.contains(event.target)) {
removePopup();
}
}
/**
* Handle Escape key press
*/
function handleEscapeKey(event) {
if (event.key === 'Escape') {
removePopup();
}
}
// ============================================================================
// Utility Functions
// ============================================================================
/**
* Escape HTML to prevent XSS
*/
function escapeHtml(text) {
if (!text) return '';
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
// Cleanup on page unload
window.addEventListener('beforeunload', () => {
removePopup();
});
// ============================================================================
// Debug Helper (can be removed in production)
// ============================================================================
// Expose for debugging in console
window.__romajiTranslator = {
getPopup: () => activePopup,
getTranslation: () => currentTranslation,
testTranslate: async (text) => {
const result = await translateRomaji(text);
console.log("Translation result:", result);
return result;
}
};