-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
64 lines (51 loc) · 1.75 KB
/
app.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
alert("Use desktop mode for best experience!");
let input = document.querySelector('#search-bar');
let button = document.querySelector('#button');
let KEY = 'be1eb80c-7d92-49f8-9545-2817d19056a0';
let notFound = document.querySelector('.not__found');
let defBox = document.querySelector('.def');
let audioBox = document.querySelector('.audio');
let loading = document.querySelector('.load');
button.addEventListener('click',(e) => handleClick(e));
function handleClick(e){
e.preventDefault();
defBox.innerText = '';
audioBox.innerText = '';
notFound.innerText = '';
let query = input.value;
if(query===''){
alert('Please enter a keyword to Search!');
return;
}
fetchData(query);
}
async function fetchData(query){
loading.style.display = 'block';
const response = await fetch(`https://www.dictionaryapi.com/api/v3/references/learners/json/${query}?key=${KEY}`);
const data = await response.json();
if (data.length==0){
loading.style.display = 'none';
audioBox.style.display = 'none';
notFound.style.display = 'block';
notFound.innerHTML = 'Not Found';
return;
}
audioBox.style.display = 'block';
loading.style.display = 'none';
console.log(data);
let defination = data[0].shortdef[0];
defBox.innerText = defination;
const soundData = data[0].hwi.prs[0].sound.audio;
if(soundData) {
fetchSound(soundData);
}
console.log(data);
}
function fetchSound(soundData){
let subfolder = soundData.charAt(0);
let soundSrc = `https://media.merriam-webster.com/soundc11/${subfolder}/${soundData}.wav?key=${KEY}`;
let aud = document.createElement('audio');
aud.src = soundSrc;
aud.controls = true;
audioBox.appendChild(aud);
}