-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent_bridge.js
More file actions
64 lines (55 loc) · 2.41 KB
/
content_bridge.js
File metadata and controls
64 lines (55 loc) · 2.41 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
const fontMap = {
arabic: ["ar", "ur", "fa", "ku", "ps", "sd", "ug", "yi"],
hindi: ["hi", "mr", "ne", "sa", "bh"],
latin: ["en", "es", "de", "fr", "it", "pt", "nl", "af", "sq", "ca", "da", "fi", "sv", "no", "is", "ga", "cy", "eu", "gl", "mt", "ro", "hu", "pl", "cs", "sk", "sl", "hr", "bs", "sr-Latn", "et", "lv", "lt", "tr", "az", "uz", "tk", "sw", "yo", "zu", "xh", "st", "tn", "ts", "ve", "ss", "nr", "id", "ms", "tl", "ceb", "la"]
};
function getFontClass(langCode) {
if (fontMap.arabic.includes(langCode)) return 'font-arabic';
if (fontMap.hindi.includes(langCode)) return 'font-hindi';
return 'font-latin';
}
let tooltip = null;
const handleDblClick = async (e) => {
try {
if (!chrome.runtime?.id) {
document.removeEventListener('dblclick', handleDblClick);
return;
}
const selection = window.getSelection().toString().trim();
if (!selection || selection.split(' ').length > 1) return;
const res = await chrome.storage.sync.get(['tooltipEnabled', 'targetLang']);
if (res.tooltipEnabled === false) return;
const lang = res.targetLang || 'en';
const translation = await chrome.runtime.sendMessage({
action: 'translateWord',
text: selection,
lang: lang
});
if (translation) {
showTooltip(e.clientX, e.clientY, translation, lang);
}
} catch (error) {
if (error.message.includes('Extension context invalidated')) {
console.log("Translate In Sidepanel: Context was invalidated. The event listener on this page has been removed. Please refresh the page to re-enable tooltips.");
document.removeEventListener('dblclick', handleDblClick);
} else {
console.error("An unexpected error occurred in content_bridge.js:", error);
}
}
};
document.addEventListener('dblclick', handleDblClick);
function showTooltip(x, y, text, lang) {
if (!tooltip) {
tooltip = document.createElement('div');
tooltip.id = 'trans-tooltip';
document.body.appendChild(tooltip);
}
tooltip.className = '';
tooltip.classList.add(getFontClass(lang), 'show');
tooltip.textContent = text;
tooltip.style.left = (x + window.scrollX) + 'px';
tooltip.style.top = (y + window.scrollY - 40) + 'px';
setTimeout(() => {
if (tooltip) tooltip.classList.remove('show');
}, 5000);
}