-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElegantDictionary.html
More file actions
252 lines (215 loc) · 7.79 KB
/
ElegantDictionary.html
File metadata and controls
252 lines (215 loc) · 7.79 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Elegant Dictionary</title>
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" />
<!-- Font Awesome for icons -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" />
<!-- Google Fonts for better typography -->
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500&family=Nunito:wght@400;600&display=swap"
rel="stylesheet" />
<style>
body {
font-family: 'Nunito', sans-serif;
background: linear-gradient(135deg, #D1A7E0, #F4C4D1);
/* Soft purple and pink gradient */
color: #333;
height: 100vh;
margin: 0;
display: flex;
justify-content: center;
align-items: center;
}
.card {
border-radius: 1.5rem;
box-shadow: 0 6px 20px rgba(0, 0, 0, 0.1);
background: #fff;
padding: 2rem;
width: 100%;
max-width: 800px;
transition: transform 0.3s ease;
}
.card:hover {
transform: scale(1.05);
}
.btn-primary {
background-color: #7FDBB6;
/* Mint green */
border-color: #7FDBB6;
}
.btn-primary:hover {
background-color: #66D1A1;
/* Darker mint */
border-color: #66D1A1;
}
.btn-light {
background-color: #F4C4D1;
/* Soft pink */
border-color: #F4C4D1;
color: #fff;
}
.btn-light:hover {
background-color: #F1A0B2;
/* Darker soft pink */
border-color: #F1A0B2;
}
.input-group input {
border-radius: 0.75rem;
border: 2px solid #ddd;
padding: 0.75rem;
font-size: 1rem;
}
.input-group button {
border-radius: 0.75rem;
padding: 0.75rem 1.5rem;
}
.text-muted {
color: #777;
}
.text-dark {
color: #333;
}
.result {
display: none;
margin-top: 1.5rem;
}
.result.active {
display: block;
}
.italic {
font-style: italic;
}
.mt-3 {
margin-top: 1rem;
}
.search-wrapper {
position: relative;
}
.search-wrapper input {
border-radius: 1rem;
box-shadow: none;
border: 2px solid #7FDBB6;
/* Mint green border */
}
.search-wrapper button {
border-radius: 1rem;
background-color: #7FDBB6;
/* Mint green button */
border-color: #7FDBB6;
color: #fff;
}
.search-wrapper button:hover {
background-color: #66D1A1;
border-color: #66D1A1;
}
.result h3 {
font-weight: 600;
}
.result p {
font-size: 1.1rem;
line-height: 1.5;
}
.result button {
background-color: #7FDBB6;
color: #fff;
}
.result button:hover {
background-color: #66D1A1;
}
.loading {
display: none;
font-size: 1.25rem;
}
.loading.active {
display: block;
}
</style>
</head>
<body>
<div class="card shadow-lg">
<div class="search-wrapper mb-4">
<div class="input-group">
<input type="text" class="form-control" id="inp-word" placeholder="Search for a word..." />
<button class="btn btn-primary" id="search-btn">
<i class="fas fa-search"></i> Search
</button>
<button class="btn btn-light" id="clear-btn">
<i class="fas fa-times"></i> Clear
</button>
</div>
</div>
<div id="loading" class="text-center loading">
<i class="fas fa-spinner fa-spin"></i> Loading...
</div>
<div class="result" id="result"></div>
</div>
<!-- Script -->
<script>
const url = "https://api.dictionaryapi.dev/api/v2/entries/en/";
const result = document.getElementById("result");
const loading = document.getElementById("loading");
const sound = document.createElement("audio");
document.getElementById("search-btn").addEventListener("click", () => {
let inpWord = document.getElementById("inp-word").value.trim();
if (!inpWord) {
alert("Please enter a word.");
return;
}
loading.classList.add("active");
result.classList.remove("active");
fetch(`${url}${inpWord}`)
.then((response) => response.json())
.then((data) => {
loading.classList.remove("active");
if (!data[0].phonetics || !data[0].phonetics[0].audio) {
result.innerHTML = `
<h3 class="text-center">${inpWord}</h3>
<p class="text-dark">Audio pronunciation is not available.</p>
`;
} else {
const wordData = data[0];
const phonetic = wordData.phonetic || "N/A";
const partOfSpeech = wordData.meanings[0].partOfSpeech || "N/A";
const definition = wordData.meanings[0].definitions[0].definition || "No definition available.";
const example = wordData.meanings[0].definitions[0].example || "No example available.";
const audioUrl = wordData.phonetics[0]?.audio;
result.innerHTML = `
<h3 class="text-center">${inpWord}</h3>
<div class="d-flex justify-content-between mb-3">
<p class="text-muted">${partOfSpeech}</p>
<p class="text-muted">/${phonetic}/</p>
</div>
<p class="text-dark">${definition}</p>
<p class="text-muted italic mt-3">${example}</p>
${audioUrl ? `<button class="btn btn-light mt-3" onclick="playSound('${audioUrl}')"><i class="fas fa-volume-up"></i> Pronounce</button>` : ""}
`;
}
result.classList.add("active");
})
.catch(() => {
loading.classList.remove("active");
result.innerHTML = `<h4 class="text-danger text-center">Couldn't find the word</h4>`;
result.classList.add("active");
});
});
function playSound(url) {
if (url) {
sound.setAttribute("src", url);
sound.play().catch((error) => {
alert("Error playing sound: " + error.message);
});
} else {
alert("Audio pronunciation not available.");
}
}
document.getElementById("clear-btn").addEventListener("click", () => {
document.getElementById("inp-word").value = ''; // Clears the input field
result.classList.remove("active"); // Hide the result
});
</script>
<!-- Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>