Upload background.js with huggingface_hub
Browse files- background.js +31 -0
background.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
chrome.runtime.onInstalled.addListener(() => {
|
| 2 |
+
chrome.contextMenus.create({
|
| 3 |
+
id: "traduzir-area",
|
| 4 |
+
title: "Traduzir área selecionada",
|
| 5 |
+
contexts: ["all"]
|
| 6 |
+
});
|
| 7 |
+
});
|
| 8 |
+
|
| 9 |
+
chrome.contextMenus.onClicked.addListener((info, tab) => {
|
| 10 |
+
if (info.menuItemId === "traduzir-area") {
|
| 11 |
+
chrome.tabs.sendMessage(tab.id, { action: "capturar-area" });
|
| 12 |
+
}
|
| 13 |
+
});
|
| 14 |
+
|
| 15 |
+
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
| 16 |
+
if (request.action === "traduzir-texto") {
|
| 17 |
+
traduzirTexto(request.texto).then(sendResponse);
|
| 18 |
+
return true;
|
| 19 |
+
}
|
| 20 |
+
});
|
| 21 |
+
|
| 22 |
+
async function traduzirTexto(texto) {
|
| 23 |
+
try {
|
| 24 |
+
const url = `https://translate.googleapis.com/translate_a/single?client=gtx&sl=zh-CN&tl=pt&dt=t&q=${encodeURIComponent(texto)}`;
|
| 25 |
+
const response = await fetch(url);
|
| 26 |
+
const data = await response.json();
|
| 27 |
+
return data[0].map(item => item[0]).join('');
|
| 28 |
+
} catch (error) {
|
| 29 |
+
return texto;
|
| 30 |
+
}
|
| 31 |
+
}
|