-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript2.js
79 lines (71 loc) · 3.38 KB
/
script2.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
const API_URL = 'https://gutendex.com/books';
async function searchBooks() {
const query = document.getElementById('searchInput').value;
const resultsContainer = document.getElementById('results');
resultsContainer.innerHTML = 'Loading...';
try {
const response = await fetch(`${API_URL}?search=${encodeURIComponent(query)}`);
const data = await response.json();
if (data.results && data.results.length > 0) {
resultsContainer.innerHTML = '';
data.results.forEach(book => {
const bookElement = document.createElement('div');
bookElement.className = 'book';
const title = book.title || 'No title available';
const author = book.authors.map(a => a.name).join(', ') || 'Unknown author';
const downloadLink = book.formats['application/pdf'] || book.formats['text/plain'];
bookElement.innerHTML = `
<h3>${title}</h3>
<p>${author}</p>
${downloadLink
? `<a href="${downloadLink}" target="_blank">Download</a>`
: '<p>No accessible formats available.</p>'}
`;
resultsContainer.appendChild(bookElement);
});
} else {
resultsContainer.innerHTML = '<p>No books found.</p>';
}
} catch (error) {
resultsContainer.innerHTML = '<p>Error fetching books. Please try again later.</p>';
console.error(error);
}
}
// Function to search books (already defined)
async function searchBooks() {
const query = document.getElementById('searchInput').value;
const resultsContainer = document.getElementById('results');
resultsContainer.innerHTML = 'Loading...';
try {
const response = await fetch(`https://gutendex.com/books?search=${encodeURIComponent(query)}`);
const data = await response.json();
if (data.results && data.results.length > 0) {
resultsContainer.innerHTML = '';
data.results.forEach(book => {
const bookElement = document.createElement('div');
bookElement.className = 'book';
const title = book.title || 'No title available';
const author = book.authors.map(a => a.name).join(', ') || 'Unknown author';
const downloadLink = book.formats['application/pdf'] || book.formats['text/plain'];
bookElement.innerHTML = `
<h3>${title}</h3>
<p>${author}</p>
${downloadLink
? `<a href="${downloadLink}" target="_blank">Download</a>`
: '<p>No accessible formats available.</p>'}
`;
resultsContainer.appendChild(bookElement);
});
} else {
resultsContainer.innerHTML = '<p>No books found.</p>';
}
} catch (error) {
resultsContainer.innerHTML = '<p>Error fetching books. Please try again later.</p>';
console.error(error);
}
}
// Function to clear search results and refresh the input field
function clearSearch() {
document.getElementById('searchInput').value = ''; // Clear input field
document.getElementById('results').innerHTML = ''; // Clear results
}