-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
60 lines (50 loc) · 1.8 KB
/
script.js
File metadata and controls
60 lines (50 loc) · 1.8 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
const searchBtn = document.getElementById("searchBtn");
const searchInput = document.getElementById("searchInput");
const resultsDiv = document.getElementById("results");
async function fetchBooks(query) {
const url = `https://www.googleapis.com/books/v1/volumes?q=${query}`;
const res = await fetch(url);
const data = await res.json();
return data.items || [];
}
function displayBooks(books) {
resultsDiv.innerHTML = "";
if (books.length === 0) {
resultsDiv.innerHTML = `<p>No results found. Try another search.</p>`;
return;
}
books.forEach(book => {
const info = book.volumeInfo;
const title = info.title || "No Title";
const authors = info.authors ? info.authors.join(", ") : "Unknown Author";
const published = info.publishedDate || "N/A";
const thumbnail = info.imageLinks ? info.imageLinks.thumbnail : "https://via.placeholder.com/200x300?text=No+Cover";
const preview = info.previewLink || "#";
const bookCard = document.createElement("div");
bookCard.classList.add("book-card");
bookCard.innerHTML = `
<img src="${thumbnail}" alt="${title}">
<h3>${title}</h3>
<p><strong>Author:</strong> ${authors}</p>
<p><strong>Published:</strong> ${published}</p>
<a href="${preview}" target="_blank">🔗 Preview</a>
`;
resultsDiv.appendChild(bookCard);
});
}
searchBtn.addEventListener("click", async () => {
const query = searchInput.value.trim();
if (query) {
const books = await fetchBooks(query);
displayBooks(books);
}
});
searchInput.addEventListener("keypress", async (e) => {
if (e.key === "Enter") {
const query = searchInput.value.trim();
if (query) {
const books = await fetchBooks(query);
displayBooks(books);
}
}
});