-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
64 lines (62 loc) · 1.93 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
let input = document.getElementById("inp");
let output = document.getElementById("out");
let container_result = document.getElementById("container_result");
input.addEventListener("keypress",e =>{
if(e.key==='Enter')
{
fetchbooks();
} })
function fetchbooks() {
remove_previous_result();
console.log("clicked");
fetch(`http://openlibrary.org/search.json?q=${input.value}`)
.then((a) => a.json())
.then((response) => {
for (let i = 0; i<(response.docs.length)-1; i++) {
console.log(response);
a = response;
if(('author_name' in response.docs[i]) && ('title' in response.docs[i]) && ('isbn' in response.docs[i]) )
{
createbook(i, response);
}
}
});
}
function createbook(i, response) {
let div = document.createElement("div");
div.setAttribute("class", "a_book");
div.setAttribute("id", `i-${i}`);
container_result.appendChild(div);
let img = document.createElement("img");
img.setAttribute("id", "cover_img");
let title = document.createElement("h5");
title.setAttribute("id", "title");
let author = document.createElement("h5");
author.setAttribute("id", "author");
let view = document.createElement("div");
view.setAttribute("id", "view_more");
view.innerText='view more';
let d = fetch(
`http://covers.openlibrary.org/b/isbn/${response.docs[i].isbn[0]}-M.jpg`
);
d.then((res) =>
{ if(res.status!=200)
{
img.src = `images/no_cover.jpg`;
}
else {
img.src = `http://covers.openlibrary.org/b/isbn/${response.docs[i].isbn[0]}-M.jpg`;
}});
title.innerHTML = `<span>title:</span>${response.docs[i].title}`;
author.innerHTML = `<span>Author:</span>${response.docs[i]?.author_name[0]}`;
div.append(img);
div.append(title);
div.append(author);
div.append(view);
}
function remove_previous_result()
{while(container_result.firstChild)
{
container_result.removeChild(container_result.firstChild);
}
}