-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
303 lines (251 loc) · 12.3 KB
/
content.js
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
294
295
296
297
298
299
300
301
302
// Listen for messages from the background script
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
handleMessageAction(message);
});
// Function to handle the action based on the message
function handleMessageAction(message) {
switch (message.action) {
case "showOptions":
showOptionsPopup(message.options);
break;
case "showMeaning":
showMeaningPopup(message.word, message.meaning, true, false, message.cognates, message.language);
break;
case "noMeaning":
showMeaningPopup(message.word, message.meaning, true, true);
break;
default:
console.error("Unknown action:", message.action);
break;
}
}
// Variable to store the last popup position
let lastPopupPosition = { top: 0, left: 0 };
// Function to show the popup with options to choose from
function showOptionsPopup(options) {
const popup = createPopup();
// Create header "Choose Option"
const header = document.createElement('div');
header.innerText = "Choose Option";
header.style.fontWeight = 'bold';
header.style.fontSize = '16px'; // Medium size
header.style.color = 'black'; // Black color
header.style.marginBottom = '10px';
header.style.textAlign = 'left'; // Left align header
popup.appendChild(header); // Add header to the popup
// Add each option as a button or clickable element
options.forEach(option => {
const optionButton = document.createElement('div');
optionButton.innerText = option;
optionButton.classList.add('option');
optionButton.style.marginBottom = '8px';
optionButton.style.cursor = 'pointer';
optionButton.style.textAlign = 'left'; // Left align options
// Add click event listener for each option
optionButton.addEventListener('click', () => {
chrome.runtime.sendMessage({ action: "getMeaning", word: option }, (response) => {
showMeaningPopup(option, response.meaning, false); // Show the meaning after choosing an option
popup.remove(); // Close the list of options
});
});
popup.appendChild(optionButton);
});
// Position the popup next to the selected text
const selection = window.getSelection();
if (selection.rangeCount > 0) {
const range = selection.getRangeAt(0);
const rect = range.getBoundingClientRect();
lastPopupPosition.top = rect.bottom + window.scrollY; // Save the top position
lastPopupPosition.left = rect.left + window.scrollX; // Save the left position
popup.style.top = `${lastPopupPosition.top}px`; // Position below the selected text
popup.style.left = `${lastPopupPosition.left}px`; // Align left with the selected text
}
// Add event listener to close the popup if clicked outside
document.addEventListener('click', (event) => {
if (!popup.contains(event.target)) {
popup.remove();
}
}, { once: true }); // Ensure the event listener only runs once
}
// Function to show the cognates popup
function showCognatesPopup(cognates) {
const cognatesPopup = createPopup(false);
// Create header "Cognates"
const cognatesHeader = document.createElement('div');
cognatesHeader.innerText = "Cognates";
cognatesHeader.style.fontWeight = 'bold';
cognatesHeader.style.fontSize = '16px'; // Medium size
cognatesHeader.style.color = 'black'; // Black color
cognatesHeader.style.marginBottom = '10px';
cognatesHeader.style.textAlign = 'left'; // Left align header
cognatesPopup.appendChild(cognatesHeader); // Add header to the popup
// Split the cognates string into an array
const cognateArray = cognates.split(',').map(word => word.trim());
// Add each cognate as a clickable element
cognateArray.forEach(cognate => {
const cognateButton = document.createElement('div');
cognateButton.innerText = cognate;
cognateButton.classList.add('cognate');
cognateButton.style.marginBottom = '8px';
cognateButton.style.cursor = 'pointer';
cognateButton.style.textAlign = 'left'; // Left align cognate options
// Add click event listener for each cognate
cognateButton.addEventListener('click', () => {
chrome.runtime.sendMessage({ action: "getMeaning", word: cognate }, (response) => {
handleMessageAction(response);
});
});
cognatesPopup.appendChild(cognateButton);
});
// Position the cognates popup to the right of the meaning popup
cognatesPopup.style.position = 'fixed';
cognatesPopup.style.top = `${lastPopupPosition.top}px`; // Align with the top of the meaning popup
cognatesPopup.style.left = `${lastPopupPosition.left + 380}px`; // Position it to the right (considering width of meaning popup)
// Add event listener to close the popup if clicked outside
document.addEventListener('click', (event) => {
if (!cognatesPopup.contains(event.target)) {
cognatesPopup.remove();
}
}, { once: true });
}
// Function to show the popup with the meaning
function showMeaningPopup(word, meaning, afterListSelection = false, noMeaning = false, cognates = "", language = "") {
const popup = createPopup();
// Create a mapping for language codes to full names
const languageMap = {
'E': 'English',
'L': 'Latin',
'G': 'Greek',
'A': 'Arabic'
};
// Get the full language name from the languageMap
const fullLanguage = languageMap[language] || language; // Use the code itself if not found
// Add the word as the header with the specified color
const header = document.createElement('div');
header.style.fontSize = '24px';
header.style.color = '#e78753'; // Color you specified for the header
header.style.fontWeight = 'bold';
header.style.textAlign = 'left'; // Left align header
// Add the word text
const wordText = document.createElement('span');
wordText.innerText = word;
// Add the full language name in small black letters, inside parentheses
const languageText = document.createElement('span');
languageText.innerText = fullLanguage ? ` (${fullLanguage})` : '';
languageText.style.fontSize = '12px'; // Smaller font size for the language
languageText.style.color = 'black'; // Black color for language text
// Append both the word and language to the header
header.appendChild(wordText);
header.appendChild(languageText);
popup.appendChild(header);
// Add a spacer for better readability
const spacer = document.createElement('div');
spacer.style.marginTop = '10px'; // Space between the header and meaning text
popup.appendChild(spacer);
// Add meaning text
const meaningText = document.createElement('div');
if (noMeaning === true) {
// If noMeaning is true, display the message directly
const noMeaningText = document.createElement('div');
noMeaningText.innerText = "The word does not exist in the dictionary."; // Direct message
noMeaningText.style.textAlign = 'left'; // Left align text
noMeaningText.style.direction = 'ltr'; // Ensure text direction is left-to-right
meaningText.appendChild(noMeaningText);
} else {
// Split the meaning string to extract parts
const firstQuoteIndex = meaning.indexOf('"');
const secondQuoteIndex = meaning.indexOf('"', firstQuoteIndex + 1);
let firstPart = "";
let restOfString = meaning;
if (firstQuoteIndex !== -1 && secondQuoteIndex !== -1) {
firstPart = meaning.substring(firstQuoteIndex + 1, secondQuoteIndex); // Content between the first two quotation marks
restOfString = meaning.slice(secondQuoteIndex + 1).trim(); // The rest of the string after the second quotation mark
}
// Add meaning header
const meaningHeader = document.createElement('div');
meaningHeader.innerText = "Meaning";
meaningHeader.style.fontWeight = 'bold';
meaningHeader.style.textAlign = 'left'; // Left align meaning header
meaningHeader.style.direction = 'ltr'; // Ensure text direction is left-to-right
meaningText.appendChild(meaningHeader);
// Add the first part
const firstPartText = document.createElement('div');
firstPartText.innerText = firstPart;
firstPartText.style.textAlign = 'left'; // Left align first part text
firstPartText.style.direction = 'ltr'; // Ensure text direction is left-to-right
meaningText.appendChild(firstPartText);
// Add a spacer for better readability between meaning and etymology
const meaningSpacer = document.createElement('div');
meaningSpacer.style.marginTop = '10px'; // One line gap
meaningText.appendChild(meaningSpacer);
// Add etymology header
const etymologyHeader = document.createElement('div');
etymologyHeader.innerText = "Etymology";
etymologyHeader.style.fontWeight = 'bold';
etymologyHeader.style.textAlign = 'left'; // Left align etymology header
etymologyHeader.style.direction = 'ltr'; // Ensure text direction is left-to-right
meaningText.appendChild(etymologyHeader);
// Add the rest of the string
const restOfStringText = document.createElement('div');
restOfStringText.innerText = restOfString;
restOfStringText.style.textAlign = 'left'; // Left align rest of string text
restOfStringText.style.direction = 'ltr'; // Ensure text direction is left-to-right
meaningText.appendChild(restOfStringText);
}
meaningText.style.marginTop = '10px'; // Space above the text
meaningText.style.maxHeight = '200px'; // Limiting height for scrolling if long
meaningText.style.overflowY = 'auto';
meaningText.style.textAlign = 'left'; // Ensure all text is left aligned
meaningText.style.direction = 'ltr'; // Ensure text direction is left-to-right
meaningText.style.display = 'block'; // Ensure block display for alignment
meaningText.style.whiteSpace = 'pre-wrap'; // Preserve white space
meaningText.style.wordWrap = 'break-word'; // Wrap long words
popup.appendChild(meaningText);
if (afterListSelection === true) {
// Position the popup next to the selected text
const selection = window.getSelection();
if (selection.rangeCount > 0) {
const range = selection.getRangeAt(0);
const rect = range.getBoundingClientRect();
lastPopupPosition.top = rect.bottom + window.scrollY; // Save the top position
lastPopupPosition.left = rect.left + window.scrollX; // Save the left position
popup.style.top = `${lastPopupPosition.top}px`; // Position below the selected text
popup.style.left = `${lastPopupPosition.left}px`; // Align left with the selected text
}
} else {
// Position the popup using the last saved position
popup.style.top = `${lastPopupPosition.top}px`; // Position below the selected text
popup.style.left = `${lastPopupPosition.left}px`; // Align left with the selected text
}
if (cognates) {
showCognatesPopup(cognates); // Call the function to show cognates popup
}
// Add event listener to close the popup if clicked outside
document.addEventListener('click', (event) => {
if (!popup.contains(event.target)) {
popup.remove();
}
}, { once: true });
}
// Helper function to create and show a popup
function createPopup(remove = true) {
let popup = document.querySelector('.etymology-popup');
if (popup && remove) {
popup.remove(); // Remove any existing popup
}
// Create a new popup element
popup = document.createElement('div');
popup.classList.add('etymology-popup');
popup.style.position = 'fixed';
popup.style.zIndex = '9999';
popup.style.width = '350px'; // Fixed width for the popup
popup.style.height = 'auto';
popup.style.padding = '10px';
popup.style.backgroundColor = '#fff';
popup.style.border = '2px solid black';
popup.style.borderRadius = '5px';
popup.style.boxShadow = '0px 4px 8px rgba(0, 0, 0, 0.2)';
popup.style.textAlign = 'left'; // Left align the entire popup
document.body.appendChild(popup);
return popup;
}