-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
36 lines (32 loc) · 1.21 KB
/
app.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
const search = document.getElementById('search');
const matchList = document.getElementById('match-list');
const sercretKey = "AIzaSyC4TCttieyjBJMOX6dY3XKttgGCeNpVKXY";
const bloggerId = "2159530372678106659";
//Search books and filter it
const searchBooks = async searchText => {
const res = await fetch(`https://www.googleapis.com/blogger/v3/blogs/${bloggerId}/posts?maxResults=500&key=${sercretKey}`);
const books = await res.json();
//Get matches to current text input
let matches = books.items.filter(book =>{
const regex = new RegExp(`^${searchText}`, 'gi');
return book.title.match(regex) || book.content.match(regex);
});
if(searchText.length === 0){
matches = [];
matchList.innerHTML = '';
}
outputHtml(matches);
};
//Show results in HTML
const outputHtml = matches => {
if(matches.length > 0){
const html = matches.map(match => `
<div class="card card-body mb-1">
<h4><a href="${match.url}">${match.title}</a><small class="text-primary"> ${match.published}</small></h4>
<small>Author: ${match.author.displayName}</small>
</div>
`).join('');
matchList.innerHTML = html;
}
};
search.addEventListener('input', () => searchBooks(search.value));