-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdictionary.html
More file actions
250 lines (204 loc) · 5.64 KB
/
dictionary.html
File metadata and controls
250 lines (204 loc) · 5.64 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Dictionary</title>
<style type="text/css">
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
img, video, canvas, svg { max-width: 100%; height: auto; }
@import url('https://fonts.googleapis.com/css2?family=Oswald:wght@200..700&display=swap');
h2 {
font-family: "Oswald", serif;
font-size: 1.5em;
line-height: 1.1em;
text-transform: uppercase;
letter-spacing: 0.05em;
margin-top: 0;
padding: 5px 10px;
margin-bottom: 15px;
position: relative;
}
/* Input Section */
#input-container {
margin-bottom: 20px;
}
label {
font-weight: bold;
display: block;
margin-bottom: 8px;
font-size: 14px;
color: #555;
}
input {
width: 100%;
padding: 10px;
font-size: 16px;
border: 1px solid #ddd;
}
input:focus {outline: none; /* Remove the default focus outline */
border-color: blue; /* Change the border color on focus */
box-shadow: 0 0 5px rgba(0, 0, 255, 0.5);}
button {
font-family: "Oswald", serif;
margin-top: 10px;
width: 100%;
background: #007bff; /* Blue button */
color: #fff;
font-size: 16px;
padding: 10px;
border: none;
border-radius: 4px;
cursor: pointer;
font-weight: bold;
transition: background 0.3s ease;
float:left;
}
button:hover {
background: #0056b3; /* Darker blue on hover */
}
/* Result Section */
#result {
font-family: "Oswald", serif;
margin-top: 20px;
padding-top:5px;
}
.word-header {
font-family: "Oswald", serif;
display: flex;
align-items: center;
justify-content: space-between;
border-bottom: 1px solid #ddd;
padding-bottom: 10px;
margin-bottom: 10px;
}
.word-header h2 {
font-family: "Oswald", serif;
font-size: 20px;
font-weight: bold;
}
.word-header .pronunciation {
font-family: "Oswald", serif;
font-size: 14px;
color: #000;
}
.definition, .example {
font-family: "Oswald", serif;
margin-bottom: 15px;
font-size: 16px;
}
.definition strong, .example strong {
color: #007bff;
}
.example {
}
/* Responsive Design */
@media (max-width: 600px) {
#app-container {
padding: 15px;
}
input {
font-size: 14px;
}
button {
font-family: "Lobster", serif;
font-size: 14px;
padding: 8px;
}
.word-header h2 {
font-size: 18px;
}
}
.word-type {
margin-bottom: 15px;
font-size: 16px;
color: #555; /* Subtle gray for secondary info */
}
.word-type strong {
color: #007bff; /* Accent color for labels */
}
#word-input {border-radius: 5px; /* Round the corners */
background-color: #f2f2f2;}
</style>
</head>
<body>
<h2>Dictionary</h2>
<div id="search-container">
<input type="text" id="word-input" placeholder="Enter a word" />
<button id="search" onclick="fetchDefinition()">SEARCH</button>
</div>
<div id="result"></div><div id="word-list-container">
<script type="text/javascript">
async function fetchDefinition() {
console.log("Search button clicked!");
const word = document.getElementById("word-input").value;
if (!word) {
alert("Please enter a word!");
return;
}
const apiKey = ""; // Replace with your actual Merriam-Webster API key
const url = `https://www.dictionaryapi.com/api/v3/references/collegiate/json/${word}?key=${apiKey}`;
const resultContainer = document.getElementById("result");
resultContainer.innerHTML = "<p>Loading...</p>";
try {
const response = await fetch(url);
// Attempt to parse the response as JSON
let data;
try {
data = await response.json();
} catch (error) {
console.error("Error parsing JSON:", error.message);
resultContainer.innerHTML = `<p>Error: Invalid response received from API.</p>`;
return;
}
console.log("API Response:", data);
if (data.length && typeof data[0] === "object") {
const wordData = data[0];
const definition = wordData.shortdef?.[0] || "No definition found.";
const pronunciation = wordData.hwi?.prs?.[0]?.mw || "No pronunciation available.";
const wordType = wordData.fl || "Unknown type";
// Extract example usage
let example = null;
if (wordData.def?.[0]?.sseq) {
const sseq = wordData.def[0].sseq;
for (const seq of sseq) {
const dt = seq[0]?.[1]?.dt;
if (dt) {
for (const entry of dt) {
if (entry[0] === "vis") {
example = entry[1][0]?.t || null;
break;
}
}
}
if (example) break;
}
}
if (example) {
example = example.replace(/{wi}/g, "<i>").replace(/{\/wi}/g, "</i>");
}
// Build the result HTML
let resultHTML = `
<div class="word-header">
<h2>${word}</h2>
<span class="pronunciation">${pronunciation}</span>
</div>
<p class="word-type"><strong>Type:</strong> ${wordType}</p>
<p class="definition"><strong>Definition:</strong> ${definition}</p>
`;
if (example) {
resultHTML += `<p class="example"><strong>Example:</strong> ${example}</p>`;
}
resultContainer.innerHTML = resultHTML;
} else {
resultContainer.innerHTML = "<p>Word not found!</p>";
}
} catch (error) {
console.error("Error fetching definition:", error.message);
resultContainer.innerHTML = `<p>Error fetching data: ${error.message}</p>`;
}
}
</script>
</body>
</html>