-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
49 lines (41 loc) · 1.92 KB
/
scripts.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
async function fetchQuote() {
try {
const response = await fetch('https://api.quotable.io/random'); // Fetch the data from the API
const data = await response.json(); // Parse the JSON data from the response
return data.content; // Return the "content" field
} catch (error) {
console.error('Error fetching the quote:', error);
return null; // Return null or handle the error appropriately
}
}
async function fetchTranslation(sentence, targetLang) {
const url = `https://api.mymemory.translated.net/get?q=${encodeURIComponent(sentence)}&langpair=en|${encodeURIComponent(targetLang)}`;
try {
const response = await fetch(url); // Fetch the translation
const data = await response.json(); // Parse the JSON response
return data.responseData.translatedText; // Return the translated text
} catch (error) {
console.error('Error fetching the translation:', error);
return "Failed to fetch translation."; // Handle the error and provide feedback
}
}
function clean(){
document.getElementById('english').innerText = '';
document.getElementById('german').innerText = '';
document.getElementById('slovak').innerText = '';
document.getElementById('russian').innerText = '';
}
document.getElementById('refreshButton').addEventListener('click',
async () => {
clean();
const quote = await fetchQuote(); // Fetch the quote
document.getElementById('english').innerText = quote; // Display the quote in the div
let translation;
translation = await fetchTranslation(quote, 'de'); // Fetch the translation
document.getElementById('german').innerText = translation; // Display the translation in the german div
translation = await fetchTranslation(quote, 'sk');
document.getElementById('slovak').innerText = translation;
translation = await fetchTranslation(quote, 'ru');
document.getElementById('russian').innerText = translation;
}
);