-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
72 lines (63 loc) · 2.36 KB
/
script.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
const url = "https://api.dictionaryapi.dev/api/v2/entries/en/";
const result = document.getElementById("result");
const sound = document.getElementById("sound");
const btn = document.getElementById("search-btn");
btn.addEventListener("click", async () => {
// Disable the button during the fetch operation
btn.disabled = true;
try {
let inpWord = document.getElementById("inp-word").value.trim();
if (inpWord) {
const response = await fetch(`${url}${inpWord}`);
if (!response.ok) {
throw new Error("Couldn't find the word");
}
const data = await response.json();
// Check if the data contains the necessary information
if (data[0] && data[0].phonetics && data[0].phonetics[0] && data[0].phonetics[0].audio) {
result.innerHTML = `
<div class="word">
<h3>${inpWord}</h3>
<button onclick="playSound()">
<i class="fas fa-volume-up"></i>
</button>
</div>
<div class="details">
<p>${data[0].meanings[0].partOfSpeech}</p>
<p>/${data[0].phonetic}/</p>
</div>
<p class="word-meaning">
${data[0].meanings[0].definitions[0].definition}
</p>
<p class="word-example">
${data[0].meanings[0].definitions[0].example || ""}
</p>`;
// Set the source URL for the audio element with the correct protocol
sound.setAttribute("src", `${data[0].phonetics[0].audio.replace(/^\/\//, '/')}`);
} else {
throw new Error("Audio source not found in the data");
}
// ... (remaining code)
} else {
result.innerHTML = `<h3 class="error">Please enter a word</h3>`;
}
} catch (error) {
console.error(error);
result.innerHTML = `<h3 class="error">Couldn't Find The Word</h3>`;
} finally {
// Enable the button, regardless of success or failure
btn.disabled = false;
}
});
async function playSound() {
try {
// Check if the audio element has a valid source
if (sound.src) {
await sound.play();
} else {
throw new Error("Audio source not set");
}
} catch (error) {
console.error("Error playing sound:", error);
}
}