-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgiphy.js
More file actions
47 lines (31 loc) · 1.23 KB
/
giphy.js
File metadata and controls
47 lines (31 loc) · 1.23 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
const API_KEY = 'l09bSCBP0Se4iS9cFsiGy8Fml8szc6mM';
const $search = document.getElementById('search')
const $doc = document.getEle
const $searchBtn = document.getElementById('searchBtn')
const $content = document.getElementById('content')
const API_URL = 'https://api.giphy.com/v1/gifs/search'
const $inputLim = document.getElementById('inputLimit')
function searchGifs(options = {keyword: '', limit:0}) {
const request = new XMLHttpRequest();
request.open('GET', API_URL +`?api_key=${API_KEY}&q=${options.keyword}&limit=${options.limit}`);
request.addEventListener('load', function() {
console.log(request.status);
const parsed = JSON.parse(request.responseText);
parsed.data.forEach(function(gif) {
const $iframe = document.createElement('iframe');
$iframe.src = gif.embed_url;
$iframe.width = 150;
$iframe.height = 150;
$content.append($iframe)
})
});
request.send()
}
$searchBtn.addEventListener('click', function() {
if(!$search.value) {
alert('Заполние поле')
return
}
$content.innerHTML = '';
searchGifs({keyword:$search.value, limit:$inputLim.value})
})