-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbackground.js
More file actions
42 lines (40 loc) · 1.2 KB
/
background.js
File metadata and controls
42 lines (40 loc) · 1.2 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
chrome.runtime.onMessage.addListener((request, _, sendResponse) => {
if (request.type === "TRANSLATE_CODE") {
const BACKEND_URL = process.env.BACKEND_URL;
chrome.storage.sync.get(["targetLanguage"], (result) => {
const targetLanguage = result.targetLanguage || "Java";
fetch(BACKEND_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
code: request.code,
targetLanguage: targetLanguage,
}),
})
.then((response) => {
if (!response.ok) {
throw new Error(
`Network response was not ok: ${response.statusText}`
);
}
return response.json();
})
.then((data) => {
if (data.error) {
sendResponse({ error: data.error });
} else {
sendResponse({ translation: data.translation });
}
})
.catch((error) => {
console.error("Error calling backend:", error);
sendResponse({
error: `Failed to connect to the translation service: ${error.message}`,
});
});
});
return true;
}
});