-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontentScript.js
More file actions
293 lines (266 loc) · 9.1 KB
/
contentScript.js
File metadata and controls
293 lines (266 loc) · 9.1 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// contentScript.js
/**
* @fileoverview
* Content script for BabbelSky extension.
* Overrides the default behavior of the translation button on Bluesky posts.
* Extracts the text from the active main post and sends it for translation.
*/
(function () {
/**
* Labels used to identify the translation button in different languages.
* Extend this array if Bluesky supports more languages.
* @type {string[]}
*/
const translationButtonLabels = [
"Translate",
"Traducir",
"Tradueix",
"Übersetzen",
"Käännä",
"Traduire",
"Aistrigh",
"अनुवाद करें",
"Lefordítás",
"Terjemahkan",
"Traduci",
"翻訳",
"번역",
"Vertalen",
"Przetłumacz",
"Traduzir",
"Перевести",
"Çevir",
"Перекласти",
"Dịch",
"翻译",
"翻譯",
"翻譯",
]; // Add more as needed (Currentlty Thai button is still "Translate")
let detectedLanguage = null; // Placeholder for detected language
/**
* Selects the "Translate" button based on predefined labels.
* @returns {HTMLElement|null} - The "Translate" button element or null if not found.
*/
function getTranslateButton() {
for (const label of translationButtonLabels) {
const button = document.querySelector(`a[aria-label="${label}"]`);
if (button) {
detectedLanguage = label; // Store the detected language
return button;
}
}
return null;
}
/**
* Promisified version of chrome.storage.sync.get.
* @param {string[]|Object} keys - Keys to retrieve.
* @returns {Promise<Object>} - Promise resolving to retrieved items.
*/
function getStorage(keys) {
return new Promise((resolve, reject) => {
if (!chrome.storage || !chrome.runtime) {
reject(new Error("Chrome storage or runtime is not available."));
return;
}
chrome.storage.sync.get(keys, (items) => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
resolve(items);
}
});
});
}
/**
* Extracts text from the post containing the provided "Translate" button.
* @param {HTMLElement} translateButton - The "Translate" button element.
* @returns {string|null} - The extracted post text or null if not found.
*/
function extractPostText(translateButton) {
if (!translateButton) {
console.warn('BabbelSky: "Translate" button not found.');
return null;
}
// Traverse up the DOM to find the parent post element
const postElement = translateButton.closest(
'[data-testid^="postThreadItem-by-"]',
);
if (!postElement) {
console.warn("BabbelSky: Could not locate the parent post element.");
return null;
}
// Extract the text from the designated div within the post
const textElement = postElement.querySelector('[data-word-wrap="1"]');
if (textElement) {
const postText = textElement.textContent.trim();
return postText;
}
console.warn("BabbelSky: No text element found within the post.");
return null;
}
/**
* Handles the click event on the "Translate" button.
* Prevents the default action, extracts post text, and sends it for translation.
* @param {Event} event - The click event object.
*/
function handleTranslateButtonClick(event) {
// Extract the "Translate" button that was clicked
const translateButton = event.currentTarget;
// Prevent the default translation behavior immediately
event.preventDefault();
event.stopImmediatePropagation();
// Check if a translation service is selected
getStorage(["translationService"]).then((result) => {
const selectedService = result.translationService;
if (!selectedService) {
// Allow default Bluesky behavior to proceed
if (translateButton) {
translateButton.removeEventListener(
"click",
handleTranslateButtonClick,
true,
);
translateButton.click();
translateButton.addEventListener(
"click",
handleTranslateButtonClick,
true,
);
}
return;
}
let postText = null;
try {
// Extract the post text from the associated post
postText = extractPostText(translateButton);
if (!postText) {
console.warn("BabbelSky: No post text found to translate.");
return;
}
// Send the post text to the background script for translation
chrome.runtime.sendMessage(
{ action: "translatePost", post: postText },
(response) => {
if (response && response.translatedPost) {
// Inject the translated text into the DOM
addTranslatedText(translateButton, response.translatedPost);
} else if (response && response.error) {
console.error("BabbelSky: Translation Error:", response.error);
alert(`BabbelSky Translation Error: ${response.error}`);
} else {
console.warn(
"BabbelSky: Unexpected response from background script.",
);
}
},
);
} catch (error) {
console.error("BabbelSky: Unexpected error occurred.", error);
}
});
}
/**
* Adds the translated text below the original post text.
* @param {HTMLElement} translateButton - The "Translate" button element.
* @param {string} translatedText - The translated text to display.
*/
function addTranslatedText(translateButton, translatedText) {
if (!translateButton) {
console.warn('BabbelSky: "Translate" button not provided.');
return;
}
// Traverse up the DOM to find the parent post element
const postElement = translateButton.closest(
'[data-testid^="postThreadItem-by-"]',
);
if (!postElement) {
console.warn("BabbelSky: Could not locate the parent post element.");
return;
}
// Extract the text element within the post
const textElement = postElement.querySelector('[data-word-wrap="1"]');
if (!textElement) {
console.warn("BabbelSky: No text element found within the post.");
return;
}
// Create a new paragraph element for the translated text
const translatedTextElement = document.createElement("p");
translatedTextElement.className = "babbelsky-translated-text";
// Set the translated text
translatedTextElement.textContent = translatedText;
// Insert the translated text element after the original text element
textElement.parentNode.insertBefore(
translatedTextElement,
textElement.nextSibling,
);
}
/**
* Initializes the content script by attaching the click event listener to the "Translate" button.
*/
function initialize() {
const translateButton = getTranslateButton();
if (translateButton) {
// Attach event listener to the "Translate" button
translateButton.addEventListener(
"click",
handleTranslateButtonClick,
true,
);
// Mark the button to prevent duplicate listeners
translateButton.setAttribute("data-babbelsky-listener", "true");
} else {
console.warn(
'BabbelSky: "Translate" button not found on initialization.',
);
}
}
/**
* Observes the DOM for changes to dynamically attach event listeners to the "Translate" button.
*/
function observeTranslationButton() {
// Create a MutationObserver to watch for changes in the DOM
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
mutation.addedNodes.forEach((node) => {
if (node.nodeType === 1) {
// ELEMENT_NODE
let translateButton = null;
// Check if the node or its descendants match the detected translation button label
translateButton =
node.querySelector(`a[aria-label="${detectedLanguage}"]`) ||
(node.matches(`a[aria-label="${detectedLanguage}"]`)
? node
: null);
if (
translateButton &&
!translateButton.hasAttribute("data-babbelsky-listener")
) {
// Mark the button to prevent duplicate listeners
translateButton.setAttribute("data-babbelsky-listener", "true");
// Attach the event listener
translateButton.addEventListener(
"click",
handleTranslateButtonClick,
true,
);
}
}
});
});
});
// Start observing the document body for added nodes and subtree modifications
observer.observe(document.body, { childList: true, subtree: true });
}
// Initialize the script immediately if the DOM is ready
if (
document.readyState === "complete" ||
document.readyState === "interactive"
) {
initialize();
} else {
// Initialize the script when the DOM is fully loaded
document.addEventListener("DOMContentLoaded", initialize);
}
// Start observing for dynamically added "Translate" buttons
observeTranslationButton();
})();