-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
103 lines (72 loc) · 3.61 KB
/
main.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
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
var verify_existance = false;
function queryBooks() {
const QUERY = (document.getElementById("query").value).toLowerCase();
var results = document.getElementById("res");
loader.setAttribute('style', 'display: block;');
var query_display = document.getElementById("query-display");
if (verify_existance == true) {
results.remove(results);
results = document.createElement('div');
results.setAttribute('id', 'res');
results.setAttribute('class', 'results');
loader = document.createElement('div');
loader.setAttribute('class', 'loader');
loader.setAttribute('id', 'load');
query_display = document.createElement('div');
query_display.setAttribute('id', 'query-display');
results.appendChild(loader);
results.appendChild(query_display);
document.getElementById('main').appendChild(results);
}
query_display.innerHTML = "Related results for \"" + QUERY + "\"";
const URL = "https://www.googleapis.com/books/v1/volumes?q=" + QUERY
var request = new XMLHttpRequest();
// Open a new connection, using the GET request on the URL endpoint
request.open('GET', URL, true);
request.onload = function () {
// Begin accessing JSON data here
for (var i = 0; i < 10; i++) {
var data = JSON.parse(this.response);
// console.log(data)
var authors = (data["items"][i]["volumeInfo"]["authors"]) || 'No Author Disclosed'
var title = (data["items"][i]["volumeInfo"]["title"]) || 'No title Disclosed'
var publisher = (data["items"][i]["volumeInfo"]["publisher"]) || 'No publisher Disclosed'
try {
var thumbnail = data["items"][i]["volumeInfo"]["imageLinks"]["thumbnail"]
}
catch (err) {
var thumbnail = 'https://upload.wikimedia.org/wikipedia/commons/thumb/a/a5/Georgia_404.svg/1125px-Georgia_404.svg.png'
}
var info = (data["items"][i]["volumeInfo"]["infoLink"]) || 'No info Disclosed'
card = document.createElement('div');
card.setAttribute('class', 'card col-md-6');
card.setAttribute('style', 'max-width: 32rem;');
card.setAttribute('id', 'results');
const logo = document.createElement('img');
logo.src = thumbnail;
logo.className = "card-img-top"
card.appendChild(logo);
const card_body = document.createElement('div');
card_body.setAttribute('class', 'card-body');
const card_title = document.createElement('h5');
card_title.setAttribute('class', 'card-title');
card_title.innerHTML = title;
card_body.appendChild(card_title);
const card_text = document.createElement('p');
card_text.setAttribute('class', 'card-text');
card_text.innerHTML = "By: " + authors + "<br>Published By: " + publisher;
card_body.appendChild(card_text);
const button = document.createElement('a');
button.setAttribute('class', 'btn btn-primary btn-md');
button.setAttribute('href', info);
button.innerHTML = "See this Book"
card_body.appendChild(button);
card.appendChild(card_body);
results.appendChild(card); }
}
verify_existance = true;
// Send request
request.send()
document.getElementById('query').value = ''
setTimeout("loader.setAttribute('style', 'display: none;')", 1500);
}