|
1 |
| -// The file for highlighting the word and making it show up in the browser |
2 |
| -document.addEventListener('mouseup', () => { |
3 |
| - const highlightedText = window.getSelection().toString().trim(); |
4 |
| - |
5 |
| - if (highlightedText.length > 0) { |
6 |
| - console.log("The word highlighted by the user is: ", highlightedText); |
7 |
| - fetch('http://localhost:3000/translate', { |
8 |
| - method: 'POST', |
9 |
| - headers: { |
10 |
| - 'Content-Type': 'application/json', |
11 |
| - }, |
12 |
| - body: JSON.stringify({ |
13 |
| - text: highlightedText, // The text to translate |
14 |
| - targetLanguage: 'es' // Use the selected language |
15 |
| - }), |
16 |
| - }) |
17 |
| - .then(response => { |
18 |
| - if (!response.ok) { |
19 |
| - throw new Error('Network response was not ok: ' + response.statusText); |
| 1 | +//The file for highlighting the word and making it show up in the browser 4 |
| 2 | + |
| 3 | +document.addEventListener('mouseup', (event) => { |
| 4 | + const selectedText = window.getSelection().toString().trim(); |
| 5 | + |
| 6 | + if (selectedText.length > 0) { |
| 7 | + const selection = window.getSelection(); |
| 8 | + if (selection.rangeCount > 0) { |
| 9 | + const range = selection.getRangeAt(0); |
| 10 | + |
| 11 | + // Get the bounding box of the selected text |
| 12 | + const rect = range.getBoundingClientRect(); |
| 13 | + //This is how the tooltip gets the highlighted text and translated text |
| 14 | + function fetchTranslation(text) { |
| 15 | + fetch('http://localhost:3000/translate', { |
| 16 | + method: 'POST', |
| 17 | + headers: { |
| 18 | + 'Content-Type': 'application/json', |
| 19 | + }, |
| 20 | + body: JSON.stringify({ |
| 21 | + text: text, // Text to translate |
| 22 | + targetLanguage: 'es' |
| 23 | + }), |
| 24 | + }) |
| 25 | + .then(response => response.json()) |
| 26 | + .then(data => { |
| 27 | + console.log('Translation:', data.translation); |
| 28 | + |
| 29 | + // Update the tooltip with the translated text |
| 30 | + let tooltip = document.getElementById('tooltip'); |
| 31 | + if (tooltip) { |
| 32 | + tooltip.innerHTML = ` Selected Text: ${selectedText} <br> Translation: ${data.translation} <br> |
| 33 | + <button id="saveButton">Save</button> `; |
| 34 | + |
| 35 | + } |
| 36 | + return data.translation; |
| 37 | + }) |
| 38 | + .catch(error => { |
| 39 | + console.error('Error fetching translation:', error); |
| 40 | + }); |
20 | 41 | }
|
21 |
| - return response.json(); |
22 |
| - }) |
23 |
| - .then(data => { |
24 |
| - console.log('Translated text: ', data.translation); |
25 |
| - }) |
26 |
| - .catch(error => { |
27 |
| - console.error('Error fetching translation:', error); |
28 |
| - }); |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | + // Call your translation API |
| 46 | + translation = fetchTranslation(selectedText); |
| 47 | + |
| 48 | + // Show the tooltip with translation (keeping the blue highlight) |
| 49 | + showTooltip(event, translation, selectedText, rect); |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | + } |
| 54 | + } else { |
| 55 | + // Hide tooltip if no text is selected |
| 56 | + hideTooltip(); |
29 | 57 | }
|
30 | 58 | });
|
31 | 59 |
|
32 |
| -// Instructions to run this properly: |
33 |
| -/* |
34 |
| -1. Install necessary packages: |
35 |
| - npm install dotenv cors express |
| 60 | +// Tooltip logic |
| 61 | +function showTooltip(event, translation, text, rect) { |
| 62 | + let tooltip = document.getElementById('tooltip'); |
| 63 | + |
| 64 | + if (!tooltip) { |
| 65 | + tooltip = document.createElement('div'); |
| 66 | + tooltip.id = 'tooltip'; |
| 67 | + tooltip.style.position = 'absolute'; |
| 68 | + tooltip.style.backgroundColor = '#fff'; |
| 69 | + tooltip.style.border = '1px solid #ccc'; |
| 70 | + tooltip.style.padding = '50px'; |
| 71 | + tooltip.style.zIndex = '1000'; |
| 72 | + tooltip.style.visibility = 'hidden'; |
| 73 | + document.body.appendChild(tooltip); |
| 74 | + } |
| 75 | + |
| 76 | + //tooltip.textContent = `The phrase highlighted is: ${text} Translation: ${translation}`; |
| 77 | + |
| 78 | + |
| 79 | + tooltip.style.left = `${rect.left + window.scrollX + rect.width / 2}px`; // Align tooltip horizontally |
| 80 | + tooltip.style.top = `${rect.top + window.scrollY - 40}px`; // Position tooltip above the selected text |
| 81 | + tooltip.style.visibility = 'visible'; |
| 82 | + |
| 83 | +} |
36 | 84 |
|
37 |
| -2. Go to translate.js for more npm installations: |
38 |
| - npm install @google-cloud/translate |
39 |
| - npm install @google-cloud/text-to-speech |
40 |
| - npm install @google-cloud/storage |
41 |
| - npm install fs |
| 85 | +function hideTooltip() { |
| 86 | + const tooltip = document.getElementById('tooltip'); |
| 87 | + if (tooltip) { |
| 88 | + tooltip.style.visibility = 'hidden'; |
| 89 | + } |
| 90 | +} |
42 | 91 |
|
43 |
| -3. Start the server: |
44 |
| - node translate.js (to host the server for our base URL) |
| 92 | +//ignore this |
| 93 | +/*function updateTooltip(translation, text) { |
| 94 | + const tooltip = document.getElementById('tooltip'); |
| 95 | + if (tooltip) { |
| 96 | + // Update the tooltip content with the actual translation |
| 97 | + tooltip.textContent = `Selected Text: ${text}\nTranslation: ${translation}`; |
| 98 | + } |
| 99 | +}*/ |
| 100 | +/* |
| 101 | +// Translation logic |
| 102 | +function fetchTranslation(text) { |
| 103 | + fetch('http://localhost:3000/translate', { |
| 104 | + method: 'POST', |
| 105 | + headers: { |
| 106 | + 'Content-Type': 'application/json', |
| 107 | + }, |
| 108 | + body: JSON.stringify({ |
| 109 | + text: text, // Text to translate |
| 110 | + targetLanguage: 'es' |
| 111 | + }), |
| 112 | + }) |
| 113 | + .then(response => response.json()) |
| 114 | + .then(data => { |
| 115 | + console.log('Translation:', data.translation); |
45 | 116 |
|
46 |
| -4. Make sure to stop the server (control + C in terminal) when done to prevent unnecessary API calls. |
| 117 | + // Update the tooltip with the translated text |
| 118 | + let tooltip = document.getElementById('tooltip'); |
| 119 | + if (tooltip) { |
| 120 | + tooltip.textContent = ` Translation: ${data.translation}`; |
| 121 | + } |
| 122 | + return data.translation; |
| 123 | + }) |
| 124 | + .catch(error => { |
| 125 | + console.error('Error fetching translation:', error); |
| 126 | + }); |
| 127 | +} |
47 | 128 | */
|
| 129 | +// Hide the tooltip when the selection is cleared (mouse is clicked outside the selected text) |
| 130 | +document.addEventListener('mousedown', hideTooltip); |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | + //to run this properly: |
| 136 | + /* |
| 137 | + npm install dotenv |
| 138 | + npm install cors |
| 139 | + npm install express |
| 140 | + node translate.js (to host the server for our base url) |
| 141 | + When done make sure to control c in terminal or else whenever you highlight stuff on your laptop the api will be called |
| 142 | + and we will run out of requests |
| 143 | + */ |
| 144 | + |
0 commit comments