Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
150 lines (129 loc) · 3.95 KB
/
Copy pathindex.html
File metadata and controls
150 lines (129 loc) · 3.95 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>API is gonna work for me!</title>
<style>
body {
margin: 0;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
background-color: #f4f6f8;
}
.app {
background: white;
padding: 24px;
border-radius: 8px;
width: 360px;
text-align: center;
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.08);
}
.controls {
margin-top: 20px;
display: flex;
gap: 8px;
}
input {
flex: 1;
padding: 8px;
font-size: 14px;
border: 1px solid #ccc;
border-radius: 6px;
}
button {
border: none;
border-radius: 6px;
background-color: #111;
padding: 8px 12px;
color: white;
font-size: 14px;
cursor: pointer;
}
h1 {
margin: 0, 0, 16px 0;
font-size: 20px;
font-weight: 600;
color: #222;
}
.img {
width: 100%;
border-radius: 6px;
background-color: #eaeaea;
}
button:hover {
background-color: #333;
}
button:active {
transform: scale(0.97);
}
input:focus {
outline: none;
border-color: #111;
}
button:focus {
outline: none;
}
.status {
margin-top: 12px;
font-size: 14px;
color: #555;
min-height: 18px;
}
</style>
</head>
<body>
<div class="app">
<h1>GIFs for your mood, or for a thing in your mind.</h1>
<img src="" alt="My Gifs" width="300">
<p id="status" class="status"></p>
<div class="controls">
<input type="text" id="searchGif" placeholder="Search GIF">
<button id="newGifBtn">New GIF</button>
</div>
</div>
<script>
const image = document.querySelector('img');
const newGifBtn = document.getElementById('newGifBtn');
const searchQuery = document.getElementById('searchGif');
const statusText = document.querySelector("#status");
async function newGif() {
try {
statusText.textContent = "Loading GIF...";
newGifBtn.disabled = true;
const query = searchQuery.value.trim() || "cats";
const apiUrl = `https://api.giphy.com/v1/gifs/translate?api_key=YOUR_API_KEY_HERE&s=${query}`;
const response = await fetch(`${apiUrl}`);
if (!response.ok) {
throw new Error(`HTTP Error: ${response.status}`);
}
const data = await response.json();
if (!data.data || !data.data.images) {
throw new Error("No GIF found");
}
image.src = data.data.images.original.url;
statusText.textContent = "";
}
catch (error) {
console.log(error);
}
finally {
newGifBtn.disabled = false;
}
}
newGif();
newGifBtn.addEventListener("click", newGif);
searchQuery.addEventListener("focus", () => {
searchQuery.select();
});
searchQuery.addEventListener("keydown", (event) => {
if (event.key === "Enter" && searchQuery.value.trim()) {
newGif();
}
});
</script>
</body>
</html>