-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
70 lines (62 loc) · 2.54 KB
/
Copy pathbackground.js
File metadata and controls
70 lines (62 loc) · 2.54 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
// Configuración por defecto
const defaultSettings = {
sourceLang: 'en',
targetLang: 'es',
ttsLang: 'en-US'
};
// Cargar configuración
chrome.storage.sync.get(defaultSettings, (settings) => {
globalThis.savedSettings = settings;
});
// Escuchar cambios en configuración
chrome.storage.onChanged.addListener((changes, namespace) => {
if (namespace === 'sync') {
globalThis.savedSettings = { ...globalThis.savedSettings, ...changes };
}
});
// --- NUEVO: Escuchar clic en el icono de la extensión ---
chrome.action.onClicked.addListener((tab) => {
// Enviar mensaje a la pestaña actual para que ejecute la traducción
chrome.tabs.sendMessage(tab.id, { action: 'translate-click' }, (response) => {
if (chrome.runtime.lastError) {
// Si no hay content script, abrir la URL directamente (fallback)
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs[0]) {
// Intentamos obtener el texto seleccionado de forma limitada o abrimos translate vacío
// Nota: Sin content script activo, no podemos obtener el texto fácilmente.
// Pero si el content script está activo, este mensaje llegará.
}
});
}
});
});
// Escuchar comandos de teclado (si logramos configurarlos)
chrome.commands.onCommand.addListener((command) => {
console.log('Comando recibido:', command);
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs[0]) {
chrome.tabs.sendMessage(tabs[0].id, {
action: command === 'translate-selection' ? 'translate' : 'speak'
});
}
});
});
// Escuchar mensajes de content script
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'openTranslateUrl') {
const url = `https://translate.google.com/?sl=${request.sourceLang}&tl=${request.targetLang}&text=${encodeURIComponent(request.text)}&op=translate`;
chrome.tabs.create({ url: url });
sendResponse({ success: true });
return true;
}
if (request.action === 'playAudio') {
const url = `https://translate.google.com/translate_tts?ie=UTF-8&total=1&idx=0&client=tw-ob&tl=${request.ttsLang}&q=${encodeURIComponent(request.text)}&textlen=${request.text.length}`;
// CAMBIO: Abrir en pestaña activa (active: true) y NO cerrar automáticamente
chrome.tabs.create({ url: url, active: true }, (tab) => {
// Ya no hay setTimeout para cerrar la pestaña
console.log('Audio playing in new tab:', tab.id);
});
sendResponse({ success: true });
return true;
}
});