-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathcontent.js
More file actions
167 lines (144 loc) · 6.27 KB
/
Copy pathcontent.js
File metadata and controls
167 lines (144 loc) · 6.27 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Listen for messages from popup or background script
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "translatePage") {
translatePageContent();
}
});
// Function to translate the entire page
async function translatePageContent() {
// Create progress bar container
const progressContainer = document.createElement('div');
progressContainer.style.position = 'fixed';
progressContainer.style.top = '10px';
progressContainer.style.right = '10px';
progressContainer.style.padding = '10px';
progressContainer.style.backgroundColor = '#1a73e8';
progressContainer.style.color = 'white';
progressContainer.style.borderRadius = '4px';
progressContainer.style.zIndex = '9999';
progressContainer.style.minWidth = '200px';
progressContainer.style.fontFamily = 'Arial, sans-serif';
// Create progress text
const progressText = document.createElement('div');
progressText.textContent = 'Translating page: 0%';
progressText.style.marginBottom = '8px';
// Create progress bar
const progressBar = document.createElement('div');
progressBar.style.backgroundColor = 'rgba(255, 255, 255, 0.3)';
progressBar.style.height = '8px';
progressBar.style.borderRadius = '4px';
progressBar.style.overflow = 'hidden';
const progressFill = document.createElement('div');
progressFill.style.width = '0%';
progressFill.style.height = '100%';
progressFill.style.backgroundColor = '#ffffff';
progressFill.style.transition = 'width 0.3s ease-in-out';
progressBar.appendChild(progressFill);
progressContainer.appendChild(progressText);
progressContainer.appendChild(progressBar);
document.body.appendChild(progressContainer);
try {
// Get user preference for translation mode
const { translationMode } = await chrome.storage.local.get(['translationMode']);
const mode = translationMode || 'paragraphs';
let translatedCount = 0;
let totalElements = 0;
if (mode === 'paragraphs') {
translatedCount = await translateParagraphs(progressText, progressFill);
totalElements = document.querySelectorAll('p, h1, h2, h3, h4, h5, h6, li, span, div').length;
} else {
translatedCount = await translateAllText(progressText, progressFill);
const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, null, false);
while (walker.nextNode()) {
if (walker.currentNode.textContent.trim() &&
walker.currentNode.parentElement &&
!walker.currentNode.parentElement.classList.contains('hinglish-translated')) {
totalElements++;
}
}
}
progressText.textContent = 'Translation complete!';
progressContainer.style.backgroundColor = '#0b8043';
progressFill.style.width = '100%';
setTimeout(() => progressContainer.remove(), 2000);
} catch (error) {
console.error('Translation error:', error);
progressText.textContent = 'Translation failed!';
progressContainer.style.backgroundColor = '#d93025';
setTimeout(() => progressContainer.remove(), 2000);
}
}
// Translate paragraph by paragraph
async function translateParagraphs(progressText, progressFill) {
const paragraphs = document.querySelectorAll('p, h1, h2, h3, h4, h5, h6, li, span, div');
let translatedCount = 0;
const totalElements = paragraphs.length;
for (const element of paragraphs) {
if (element.textContent.trim() &&
element.childNodes.length === 1 &&
element.childNodes[0].nodeType === Node.TEXT_NODE &&
!element.classList.contains('hinglish-translated')) {
const originalText = element.textContent;
try {
const response = await chrome.runtime.sendMessage({
action: "translateText",
text: originalText
});
if (response && response !== "Please configure your API key first") {
element.textContent = response;
element.classList.add('hinglish-translated');
translatedCount++;
const progress = Math.round((translatedCount / totalElements) * 100);
progressText.textContent = `Translating page: ${progress}%`;
progressFill.style.width = `${progress}%`;
}
} catch (error) {
console.error('Translation error:', error);
}
}
}
return translatedCount;
}
// Translate all text nodes (more aggressive approach)
async function translateAllText(progressText, progressFill) {
const walker = document.createTreeWalker(
document.body,
NodeFilter.SHOW_TEXT,
null,
false
);
let node;
const textNodes = [];
while (node = walker.nextNode()) {
if (node.textContent.trim() &&
node.parentElement &&
!node.parentElement.classList.contains('hinglish-translated')) {
textNodes.push(node);
}
}
let translatedCount = 0;
const totalElements = textNodes.length;
for (let i = 0; i < textNodes.length; i++) {
const node = textNodes[i];
const originalText = node.textContent;
try {
const response = await chrome.runtime.sendMessage({
action: "translateText",
text: originalText
});
if (response && response !== "Please configure your API key first") {
node.textContent = response;
if (node.parentElement) {
node.parentElement.classList.add('hinglish-translated');
}
translatedCount++;
const progress = Math.round((translatedCount / totalElements) * 100);
progressText.textContent = `Translating page: ${progress}%`;
progressFill.style.width = `${progress}%`;
}
} catch (error) {
console.error('Translation error:', error);
}
}
return translatedCount;
}