-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrenderer.js
56 lines (45 loc) · 1.57 KB
/
renderer.js
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
require('whatwg-fetch')
// Default output: ~/Library/Log/GiphySearch/log.log
const logger = require('electron-log')
const { clipboard } = require('electron')
// For local dev, we can set this on the environment.
// We'll need it hardcoded for distribution builds, though!
const apiKey = process.env.GIPHY_API_KEY || "YOUR_API_KEY";
const apiURL = (query) => {
return 'http://api.giphy.com/v1/gifs/search?q=' + query + '&api_key=' + apiKey + '&rating=pg'
}
const copyImageURL = (url) => {
clipboard.writeText(url)
document.querySelector('.copy').classList.add('show')
setTimeout(function(){ document.querySelector('.copy').classList.remove('show') }, 1000);
}
const bindImageSelectors = () => {
var list = document.querySelectorAll('.results img')
for (var i = 0; i < list.length; i++) {
list[i].addEventListener('click', function(e) {
copyImageURL(this.dataset.url)
})
}
}
const clearImages = () => {
document.querySelector('.results').innerHTML = ""
}
const renderImages = (json) => {
json.data.map(function(gifResult, i) {
var content = ""
content += "<img data-url='" + gifResult.images.original.url + "' "
content += "src='" + gifResult.images.preview_gif.url + "'/>"
document.querySelector('.results').innerHTML += content
})
}
document.querySelector('#search').addEventListener('keyup', function(){
fetch(apiURL(this.value)).then(function(response) {
return response.json()
}).then(function(json) {
clearImages()
renderImages(json)
bindImageSelectors()
}).catch(function(ex) {
logger.warn('parsing failed', ex)
})
})