| chrome.runtime.onInstalled.addListener(() => { | |
| chrome.contextMenus.create({ | |
| id: "traduzir-area", | |
| title: "Traduzir área selecionada", | |
| contexts: ["all"] | |
| }); | |
| }); | |
| chrome.contextMenus.onClicked.addListener((info, tab) => { | |
| if (info.menuItemId === "traduzir-area") { | |
| chrome.tabs.sendMessage(tab.id, { action: "capturar-area" }); | |
| } | |
| }); | |
| chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { | |
| if (request.action === "traduzir-texto") { | |
| traduzirTexto(request.texto).then(sendResponse); | |
| return true; | |
| } | |
| }); | |
| async function traduzirTexto(texto) { | |
| try { | |
| const url = `https://translate.googleapis.com/translate_a/single?client=gtx&sl=zh-CN&tl=pt&dt=t&q=${encodeURIComponent(texto)}`; | |
| const response = await fetch(url); | |
| const data = await response.json(); | |
| return data[0].map(item => item[0]).join(''); | |
| } catch (error) { | |
| return texto; | |
| } | |
| } | |