-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapiLoader.js
More file actions
97 lines (83 loc) · 3.18 KB
/
apiLoader.js
File metadata and controls
97 lines (83 loc) · 3.18 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
yourApiKey = "AIzaSyCmMcLAlgv2cuEmBO2QtQGqoOrQsZVw5qE"
var topic = "";
function loadSearchTopic(){
window.topic = document.getElementById("nav-keyword-id").value;
// check();
// function check(){
if(topic==""){}
else{
const xhr = new XMLHttpRequest();
xhr.open('GET',`https://www.googleapis.com/books/v1/volumes?q=${window.topic}+&key=${yourApiKey}`,true)
var mybooks = document.getElementById("mybooks");
var p = document.getElementById("forPreview");
var mybookscode = "";
const arrIdentifier = []
var z = 0;
xhr.onload = function(){
if(this.status === 200){
let json = JSON.parse(this.responseText);
let items = json.items;
for(i=0;i<items.length;i++){
var subItems = items[i]
var volumeInfo = subItems.volumeInfo;
var title = volumeInfo.title;
var description = volumeInfo.description;
var publisher = volumeInfo.publisher;
var pageCount = volumeInfo.pageCount;
var publishedDate = volumeInfo.publishedDate;
var desclen = description?.length || 0;
if(desclen == 0){
description = `The book ${title} is published by ${publisher}`
}
if(desclen > 110){
description = description.substring(0,110)
}
let book = "";
var largeImage = volumeInfo.imageLinks?.thumbnail || "images/book.jpg";
try{
var identifier = volumeInfo.industryIdentifiers[1].identifier;
var type = volumeInfo.industryIdentifiers[0].type;}
catch(err){
console.log(err);
identifier = 0;
type = 0;
}
console.log(volumeInfo)
// console.log(type)
// console.log(identifier)
var viewerUrl = 'bookPreview.html?isbn='+identifier;
book = `<div class="col-lg-4 col-md-6">
<div class="card mycard">
<img src="${largeImage}" class="card-img-top card-images" alt="...">
<div class="card-body">
<h5 class="card-title">${title}</h5>
<p class="card-text">${description} .......</p>
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item">Publisher - ${publisher}</li>
<li class="list-group-item">Pages - ${pageCount}</li>
<li class="list-group-item">Published on - ${publishedDate}</li>
</ul>
<div class="card-body">
<a target="_blank" class="btn btn-primary" id="${z}" href="${viewerUrl}">Preview</a>
</div>
</div></div>`;
// WRITE CODE FOR PREVIEW LOADER HERE
// arrIdentifier[z] = identifier;
++z;
mybookscode += book;
}
// var p_content = "";
// arrIdentifier.forEach(element => {
// p_content += element+",";
// });
// p.innerHTML = p_content;
mybooks.innerHTML = mybookscode;
}
else{
console.log("Some error occured")
}
}
xhr.send();
}
}