This repository has been archived by the owner on Aug 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
google_search.html
62 lines (57 loc) · 2.81 KB
/
google_search.html
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
{% include head.html %}
<body>
{% include header_nav.html %}
<main class="main_wrapper container search-page">
{% include left_col.html %}
<article>
<div class="wrapper article" id="googleSearchPage">
<div id="content_col" class="googleSearch">
<article>
<h1>Search results for: <span class="searchQuery"></span></h1>
<div class="resultsCount"><span></span> results</div>
<div id="searchResults"></div>
</article>
<script>
const version = window.location.pathname.split('/')[2];
const queryParams = new URLSearchParams(window.location.search);
const searchInput = document.getElementById('top_search');
const searchResults = document.getElementById('searchResults');
const searchFor = document.querySelector('.searchQuery');
const resultsCount = document.querySelector('.resultsCount span');
const displayResults = (results) => {
let html = '<ul>';
resultsCount.innerHTML = results.length;
if (results.length > 0) {
for (const result of results) {
html += `<li>
${result.breadCrumbs}<a href="${result.uri}">${result.title}</a>
<div class="search-description">${result.content}</div>
</li>`;
}
html += `</ul>`;
} else {
html = `No results found`;
}
searchResults.innerHTML = html;
}
const search = searchQuery =>
fetch(`https://${window.location.hostname}/docs/search/${version}/?searchQuery=${searchQuery}`)
.then(response => response.json()).then(data => displayResults(data))
.catch(() => searchResults.innerTextr = 'An error happened while search');
searchInput.oninput = searchInput.onchange = () => {
search(searchInput.value);
searchFor.innerText = searchInput.value;
}
if (queryParams.has('q')) {
searchInput.value = queryParams.get('q');
searchFor.innerText = queryParams.get('q');
searchInput.dispatchEvent(new Event('change'));
}
</script>
</div>
</div>
</article>
</main>
{% include footer.html %}
</body>
</html>