Spaces:
Running
Running
Update script.js
Browse files
script.js
CHANGED
|
@@ -1,16 +1,32 @@
|
|
| 1 |
-
|
| 2 |
-
const translator = require("open-google-translator");
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const translator = require("open-google-translator");
|
|
|
|
| 2 |
|
| 3 |
+
document.addEventListener("DOMContentLoaded", () => {
|
| 4 |
+
const translateButton = document.querySelector(".button");
|
| 5 |
+
const textInput = document.querySelectorAll(".input")[0];
|
| 6 |
+
const fromInput = document.querySelectorAll(".input")[1];
|
| 7 |
+
const toInput = document.querySelectorAll(".input")[2];
|
| 8 |
+
const resultDiv = document.querySelector(".card:last-of-type");
|
| 9 |
|
| 10 |
+
translateButton.addEventListener("click", () => {
|
| 11 |
+
const text = textInput.value;
|
| 12 |
+
const from = fromInput.value || "auto";
|
| 13 |
+
const to = toInput.value || "vi";
|
| 14 |
+
|
| 15 |
+
if (!text) {
|
| 16 |
+
resultDiv.textContent = "Please enter text to translate.";
|
| 17 |
+
return;
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
translator.TranslateLanguageData({
|
| 21 |
+
listOfWordsToTranslate: [text],
|
| 22 |
+
fromLanguage: from,
|
| 23 |
+
toLanguage: to,
|
| 24 |
+
})
|
| 25 |
+
.then((data) => {
|
| 26 |
+
resultDiv.textContent = data.translatedWords[0] || "Translation failed.";
|
| 27 |
+
})
|
| 28 |
+
.catch((error) => {
|
| 29 |
+
resultDiv.textContent = "Error: " + error.message;
|
| 30 |
+
});
|
| 31 |
+
});
|
| 32 |
+
});
|