-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpage.js
78 lines (61 loc) · 2.12 KB
/
page.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
let searchInputEl = document.getElementById("searchInput");
let searchResultsEl = document.getElementById("searchResults");
let spinnerEl = document.getElementById("spinner");
function createAndAppendSearchResult(result) {
let {
link,
title,
description
} = result;
let resultItemEl = document.createElement("div");
resultItemEl.classList.add("result-item");
let titleEl = document.createElement("a");
titleEl.href = link;
titleEl.target = "_blank";
titleEl.textContent = title;
titleEl.classList.add("result-title");
resultItemEl.appendChild(titleEl);
let titleBreakEl = document.createElement("br");
resultItemEl.appendChild(titleBreakEl);
let urlEl = document.createElement("a");
urlEl.classList.add("result-url");
urlEl.href = link;
urlEl.target = "_blank";
urlEl.textContent = link;
resultItemEl.appendChild(urlEl);
let linkBreakEl = document.createElement("br");
resultItemEl.appendChild(linkBreakEl);
let descriptionEl = document.createElement("p");
descriptionEl.classList.add("link-description");
descriptionEl.textContent = description;
resultItemEl.appendChild(descriptionEl);
searchResultsEl.appendChild(resultItemEl);
}
function displayResults(searchResults) {
spinnerEl.classList.add("d-none");
for (let result of searchResults) {
createAndAppendSearchResult(result);
}
}
function searchWikipedia(event) {
if (event.key === "Enter") {
spinnerEl.classList.remove("d-none");
searchResultsEl.textContent = "";
let searchInput = searchInputEl.value;
let url = "https://apis.ccbp.in/wiki-search?search=" + searchInput;
let options = {
method: "GET"
};
fetch(url, options)
.then(function(response) {
return response.json();
})
.then(function(jsonData) {
let {
search_results
} = jsonData;
displayResults(search_results);
});
}
}
searchInputEl.addEventListener("keydown", searchWikipedia);