-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgiphySearchEngine.html
145 lines (128 loc) · 4.59 KB
/
giphySearchEngine.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Giphy</title>
<!-- the css stylesheet -->
<style>
body {
width: 80%;
max-width: 1024px;
margin: 0 auto;
background-color: rgba(42, 42, 43, 0.925);
}
.container-padding50 {
padding-top: 40px;
}
.js-userinput {
width: 80%;
display: inline-block;
padding: 20px;
font-size: 16px;
font-family: Helvetica, sans-serif;
background-color: black;
color: aliceblue;
}
.js-go {
width: 10%;
display: inline-block;
padding: 20px;
font-size: 16px;
font-family: Helvetica, sans-serif;
background-color: black;
background: url("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSAEOYAC7tc-hlCIvODyfP-XEY3wG-aNUsOYQ&usqp=CAU");
background-position: center;
background-size: 70px;
border-radius: 30px;
}
</style>
<!-- the names after the container word in
class field of div, input, button is just a
group name to define css,
every place with this class name will have the same style-->
</head>
<body>
<!-- makeing the input field and the search button -->
<!-- class name is js-userinput -->
<div class="container container-padding50">
<!-- taking text type user input, and a default value -->
<input type="text" class="js-userinput" value="" />
<!-- a button with container-button type style defined in css file -->
<button class="js-go">.</button>
</div>
<!-- the container for the output -->
<div class="container container-padding50 js-container"></div>
<!-- the js script -->
<script>
// grabbing the boxes from html file and reading the input
let input = document.querySelector(".js-userinput").value;
let outContainer = document.querySelector(".js-container");
// presenting the trending gifs on first entry
dataParse(
"https://api.giphy.com/v1/gifs/trending?api_key=dhGatsWyo2dHB0vT8oyybsYlNGYfCQoD"
);
//readding the data from input field
function inputRead() {
input = document.querySelector(".js-userinput").value;
if (input == "") {
return "https://api.giphy.com/v1/gifs/trending?api_key=dhGatsWyo2dHB0vT8oyybsYlNGYfCQoD";
} else {
return (
"https://api.giphy.com/v1/gifs/search?q=" +
input +
"&api_key=dhGatsWyo2dHB0vT8oyybsYlNGYfCQoD"
);
}
}
// getting the data from remote url
function dataParse(url) {
// AJAX(element reload, not the whole page) Request
let GiphyAJAXCall = new XMLHttpRequest();
GiphyAJAXCall.open("GET", url);
GiphyAJAXCall.send();
// parsing the data ionce it is loaded
GiphyAJAXCall.addEventListener("load", function (gifdata) {
let parsedData = JSON.parse(gifdata.target.response);
output(parsedData.data);
});
}
// showing the result
function output(gifUrls) {
outContainer.innerHTML = "";
// iterating through 25 gifs
for (let i = 0; i < 25; i++) {
// or use gifUrls.forEach(function(){}));
// += operator same as in c or c++
outContainer.innerHTML +=
'<img src="' +
gifUrls[i].images.fixed_height_downsampled.url +
'">';
}
// the code here is to make a gif TV, just comment the uppar loop and run this insted
// for (let i = 0; i < 25; i++) {
// setTimeout(function () {
// outContainer.innerHTML =
// '<img src="' + gifUrls[i].images.fixed_height_downsampled.url + '">';
// console.log(outContainer.innerHTML);
// }, 6000 * i);
// }
}
// "click" event listner over button icon, to read the data
document.querySelector(".js-go").addEventListener("click", function () {
// using the url, input and key, getting the data for gifs
dataParse(inputRead());
});
// "enter key press" event listener, to read input data
document
.querySelector(".js-userinput")
.addEventListener("keyup", function (keyEvent) {
if (keyEvent.key == "Enter") {
dataParse(inputRead());
}
});
// tv.giphy.com/v1/gifs/tv?api_key=CW27AW0nlp5u0&tag=giphytv
</script>
</body>
</html>