-
Notifications
You must be signed in to change notification settings - Fork 0
/
wiki-script.js
43 lines (33 loc) · 1.35 KB
/
wiki-script.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
(function(){
const form = document.querySelector('.searchForm');
form.addEventListener('submit', handleSubmit);
function handleSubmit(event) {
event.preventDefault();
const input = document.querySelector('.searchForm-input').value;
const searchQuery = input.trim();
fetchResults(searchQuery);
}
function fetchResults(searchQuery) {
const endpoint = `https://en.wikipedia.org/w/api.php?action=query&list=search&prop=info&inprop=url&utf8=&format=json&origin=*&srlimit=20&srsearch=${searchQuery}`;
fetch(endpoint)
.then(response => response.json())
.then(data => {
const results = data.query.search[0];
displayResults(results);
})
.catch(() => console.log('An error occurred'));
}
function displayResults(results) {
const searchResults = document.querySelector('.searchResults');
searchResults.innerHTML = '';
const url = encodeURI(`https://en.wikipedia.org/wiki/${results.title}`);
searchResults.insertAdjacentHTML('beforeend',
`<div class="resultItem">
<h3 class="resultItem-title">
<a href="${url}" target="_blank" rel="noopener">${results.title}</a>
</h3>
<span class="resultItem-snippet">${results.snippet}</span><br>
</div>`
);
}
})();