forked from StructZ/CodeTranslateAI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
65 lines (56 loc) · 2.04 KB
/
content.js
File metadata and controls
65 lines (56 loc) · 2.04 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
import { enablePicker } from "./picker.js";
import { hashCode, getFromCache, saveToCache } from "./cache.js";
import { injectOrUpdateTranslations } from "./ui.js";
chrome.runtime.onMessage.addListener((message) => {
if (message.type === "ENABLE_PICKER") {
enablePicker(handleElementClick);
}
return true;
});
async function handleElementClick(e) {
e.preventDefault();
e.stopPropagation();
const clickedElement = e.target;
const selectedCode = clickedElement.textContent?.trim();
if (!selectedCode) return;
const cacheKey = `translation_${hashCode(selectedCode)}`;
const originalWidth = clickedElement.getBoundingClientRect().width;
const { targetLanguage, theme } = await chrome.storage.sync.get(["targetLanguage", "theme"]);
const lang = targetLanguage;
const cachedData = await getFromCache(cacheKey);
if (cachedData && cachedData[lang]) {
injectOrUpdateTranslations(cachedData, clickedElement, originalWidth,theme);
return;
}
const loadingDiv = document.createElement("div");
loadingDiv.className = "translator-loading";
loadingDiv.textContent = `Translating to ${lang}...`;
loadingDiv.style.width = `${originalWidth}px`;
clickedElement.parentNode.insertBefore(
loadingDiv,
clickedElement.nextSibling
);
chrome.runtime.sendMessage(
{ type: "TRANSLATE_CODE", code: selectedCode },
async (response) => {
loadingDiv.remove();
if (chrome.runtime.lastError || !response) {
alert(`Error: Could not connect to the translation service.`);
console.error(chrome.runtime.lastError?.message);
return;
}
if (response.error) {
alert(`Error: ${response.error}`);
} else if (response.translation) {
const cleaned = response.translation
.replace(/```[a-z]*\n/g, "")
.replace(/```/g, "")
.trim();
const newData = cachedData || {};
newData[lang] = cleaned;
await saveToCache(cacheKey, newData, 10);
injectOrUpdateTranslations(newData, clickedElement, originalWidth,theme);
}
}
);
}