Spaces:
Sleeping
Sleeping
Update templates/index.html
Browse files- templates/index.html +452 -310
templates/index.html
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
<!DOCTYPE html>
|
2 |
<html lang="en">
|
3 |
-
|
4 |
<meta charset="UTF-8">
|
5 |
<meta name="viewport" content="width=device-width, initial-scale=1">
|
6 |
<title>HelpingAI Search</title>
|
@@ -10,353 +10,495 @@
|
|
10 |
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600&display=swap" rel="stylesheet">
|
11 |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
|
12 |
<style>
|
13 |
-
*{box-sizing:border-box}
|
14 |
-
body{margin:0;padding:0;font-family:"Poppins",sans-serif;background-color
|
15 |
-
a{text-decoration:none;color
|
16 |
-
a:hover{text-decoration:underline}
|
17 |
-
.main-content{display:flex;flex-direction:column;align-items:center;padding:50px 20px}
|
18 |
-
.search-container{width:100%;max-width:700px;position:relative;width:60
|
19 |
-
.search-box{width:100%;padding:12px 16px;border:2px solid #4285f4;border-radius:24px;box-shadow:0 2px 4px rgba(0,0,0,0.1);transition:box-shadow .2s ease-in-out,width .3s ease,border-color .3s ease;display:flex;align-items:center}
|
20 |
-
.search-box:focus-within{box-shadow:0 4px 8px rgba(32,33,36,0.35);border-color
|
21 |
-
#search-query{width:calc(100% - 40px);border:none;outline:0;font-size:16px;padding:4px 0;transition:font-size .2s ease}
|
22 |
-
#search-query::placeholder{color
|
23 |
-
#search-query:focus{font-size:18px}
|
24 |
-
#search-query:focus::placeholder{color:transparent}
|
25 |
-
#search-form button{background:0 0;border:none;cursor:pointer;padding:8px;margin-left:10px;transition:transform .2s ease}
|
26 |
-
#search-form button:hover{transform:scale(1.1)}
|
27 |
-
#search-form button svg{display:none}
|
28 |
-
#search-form button::after{content:"\f002";font-family:"Font Awesome 5 Free";font-weight:900;font-size:1.2em;color
|
29 |
-
#search-form button:hover::after{color
|
30 |
-
#suggestions{width:calc(80% - 32px);background-color
|
31 |
-
@keyframes spin {
|
32 |
-
|
33 |
-
|
34 |
-
}
|
35 |
-
#suggestions
|
36 |
-
|
37 |
-
#suggestions
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
to{opacity:1;transform:translateY(0)}
|
45 |
-
}
|
46 |
-
.result
|
47 |
-
|
48 |
-
|
49 |
-
.
|
50 |
-
|
51 |
-
.
|
52 |
-
.
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
</style>
|
57 |
-
|
58 |
-
|
59 |
-
<div class="
|
60 |
-
|
61 |
-
<
|
62 |
-
|
63 |
-
<
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
</div>
|
79 |
<script>
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
func.apply(this, arguments);
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
|
|
114 |
const suggestions = await response.json();
|
115 |
suggestionCache[query] = suggestions;
|
116 |
return suggestions;
|
117 |
-
|
118 |
console.error("Error fetching suggestions:", response.status);
|
119 |
return [];
|
120 |
-
}
|
121 |
-
} catch (error) {
|
122 |
-
console.error("Error fetching suggestions:", error);
|
123 |
-
return [];
|
124 |
-
}
|
125 |
-
}
|
126 |
-
searchQueryInput.addEventListener("input", () => {
|
127 |
-
clearTimeout(prefetchTimeout);
|
128 |
-
const searchQuery = searchQueryInput.value.trim();
|
129 |
-
if (searchQuery === "") {
|
130 |
-
suggestionsContainer.style.display = "none";
|
131 |
-
return;
|
132 |
}
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
performSearch(suggestion.phrase);
|
153 |
-
|
154 |
-
|
155 |
selectedSuggestionIndex = index;
|
156 |
-
updateSuggestionSelection();
|
157 |
-
});
|
158 |
-
suggestionList.appendChild(listItem);
|
159 |
});
|
160 |
-
|
161 |
-
|
162 |
-
|
|
|
|
|
|
|
163 |
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
170 |
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
}, '', newURL);
|
205 |
-
}
|
206 |
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
seen.add(result.href);
|
213 |
uniqueResults.push(result);
|
214 |
-
}
|
215 |
}
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
const response = await fetch(`${BASE_URL}/api/search?q=${encodeURIComponent(query)}&max_results=${resultsPerPage}`);
|
220 |
-
if (!response.ok) {
|
221 |
-
displayError("An error occurred while fetching results.");
|
222 |
-
hideLoading();
|
223 |
-
return [];
|
224 |
-
}
|
225 |
-
const searchResults = await response.json();
|
226 |
-
return searchResults;
|
227 |
-
}
|
228 |
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
260 |
resultElement.classList.add("show");
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
304 |
selectedSuggestionIndex = (selectedSuggestionIndex - 1 + numSuggestions) % numSuggestions;
|
305 |
-
|
306 |
selectedSuggestionIndex = (selectedSuggestionIndex + 1) % numSuggestions;
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
searchQueryInput.value = suggestionItems[selectedSuggestionIndex].textContent;
|
311 |
suggestionItems[selectedSuggestionIndex].focus();
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
searchQueryInput.value = selectedSuggestion.textContent;
|
318 |
suggestionsContainer.style.display = "none";
|
319 |
performSearch(searchQueryInput.value);
|
320 |
-
}
|
321 |
}
|
322 |
-
|
323 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
324 |
event.preventDefault();
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
340 |
displayResults(resultsToDisplay, true);
|
341 |
-
|
342 |
-
|
343 |
allResultsFetched = true;
|
344 |
-
}
|
345 |
-
loadingMoreIndicator.classList.remove("active");
|
346 |
}
|
347 |
-
|
|
|
|
|
348 |
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
|
|
|
|
|
|
|
|
360 |
</script>
|
361 |
-
|
362 |
</html>
|
|
|
1 |
<!DOCTYPE html>
|
2 |
<html lang="en">
|
3 |
+
<head>
|
4 |
<meta charset="UTF-8">
|
5 |
<meta name="viewport" content="width=device-width, initial-scale=1">
|
6 |
<title>HelpingAI Search</title>
|
|
|
10 |
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600&display=swap" rel="stylesheet">
|
11 |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
|
12 |
<style>
|
13 |
+
* { box-sizing: border-box; }
|
14 |
+
body { margin: 0; padding: 0; font-family: "Poppins", sans-serif; background-color: #f8f9fa; }
|
15 |
+
a { text-decoration: none; color: #1a0dab; }
|
16 |
+
a:hover { text-decoration: underline; }
|
17 |
+
.main-content { display: flex; flex-direction: column; align-items: center; padding: 50px 20px; }
|
18 |
+
.search-container { width: 100%; max-width: 700px; position: relative; width: 60%; }
|
19 |
+
.search-box { width: 100%; padding: 12px 16px; border: 2px solid #4285f4; border-radius: 24px; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); transition: box-shadow .2s ease-in-out, width .3s ease, border-color .3s ease; display: flex; align-items: center; }
|
20 |
+
.search-box:focus-within { box-shadow: 0 4px 8px rgba(32, 33, 36, 0.35); border-color: #ea4335; }
|
21 |
+
#search-query, #main-search-query { width: calc(100% - 40px); border: none; outline: 0; font-size: 16px; padding: 4px 0; transition: font-size .2s ease; }
|
22 |
+
#search-query::placeholder, #main-search-query::placeholder { color: #9aa0a6; transition: color .2s ease; }
|
23 |
+
#search-query:focus, #main-search-query:focus { font-size: 18px; }
|
24 |
+
#search-query:focus::placeholder, #main-search-query:focus::placeholder { color: transparent; }
|
25 |
+
#search-form button, #main-search-form button { background: 0 0; border: none; cursor: pointer; padding: 8px; margin-left: 10px; transition: transform .2s ease; }
|
26 |
+
#search-form button:hover, #main-search-form button:hover { transform: scale(1.1); }
|
27 |
+
#search-form button svg, #main-search-form button svg { display: none; }
|
28 |
+
#search-form button::after, #main-search-form button::after { content: "\f002"; font-family: "Font Awesome 5 Free"; font-weight: 900; font-size: 1.2em; color: #9aa0a6; transition: color .2s ease, transform .2s ease; }
|
29 |
+
#search-form button:hover::after, #main-search-form button:hover::after { color: #4285f4; transform: scale(1.1); }
|
30 |
+
#suggestions, #main-suggestions { width: calc(80% - 32px); background-color: #fff; border: none; border-radius: 8px; box-shadow: 0 4px 6px rgba(32, 33, 36, 0.28); display: none; position: absolute; top: 100%; left: 0; z-index: 10; opacity: 0; transform: translateY(10px); transition: opacity .3s ease, transform .3s ease; padding: 10px 0; animation: slideDown 0.3s ease; }
|
31 |
+
@keyframes spin { 0% { transform: rotate(0); } 100% { transform: rotate(360deg); } }
|
32 |
+
#suggestions ul, #main-suggestions ul { list-style-type: none; padding: 0; margin: 0; }
|
33 |
+
#suggestions li, #main-suggestions li { padding: 8px 12px; cursor: pointer; border-bottom: 1px solid #eee; transition: background-color .2s ease; }
|
34 |
+
#suggestions li:hover, #main-suggestions li:hover { background-color: #e9e9e9; }
|
35 |
+
#suggestions li.selected, #main-suggestions li.selected { background-color: #f0f0f0; }
|
36 |
+
.search-box:focus-within + #suggestions,
|
37 |
+
.search-box:focus-within + #main-suggestions {
|
38 |
+
display: block;
|
39 |
+
opacity: 1;
|
40 |
+
transform: translateY(0);
|
41 |
+
}
|
42 |
+
#results { width: 100%; max-width: 700px; margin-top: 20px; }
|
43 |
+
.result { margin-bottom: 20px; padding: 15px; border-radius: 8px; background-color: #fff; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); opacity: 0; transform: translateY(10px); transition: opacity .3s ease, transform .3s ease; animation: fadeInUp .5s ease forwards; }
|
44 |
+
@keyframes fadeInUp { from { opacity: 0; transform: translateY(20px); } to { opacity: 1; transform: translateY(0); } }
|
45 |
+
.result.show { opacity: 1; transform: translateY(0); }
|
46 |
+
.result:hover {
|
47 |
+
transform: scale(1.02);
|
48 |
+
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
|
49 |
+
transition: transform 0.2s ease, box-shadow 0.2s ease;
|
50 |
+
}
|
51 |
+
.result h3 { margin: 0 0 5px; font-size: 1.2rem; color: #222; }
|
52 |
+
.result .url { color: #202124; font-size: .9rem; margin-bottom: 8px; display: block; max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
53 |
+
.result p { color: #555; font-size: .9rem; line-height: 1.6em; margin: 0; }
|
54 |
+
.loading-overlay { display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0); z-index: 1000; }
|
55 |
+
.loading-spinner { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 80px; height: 80px; border-radius: 50%; border: 5px solid #f3f3f3; border-top: 5px solid #3498db; animation: spin 1.2s linear infinite; }
|
56 |
+
#no-results { display: none; text-align: center; padding: 20px; font-size: 1.1em; color: #555; }
|
57 |
+
#loading-more { display: none; text-align: center; padding: 10px; }
|
58 |
+
#loading-more.active { display: block; }
|
59 |
+
.intro-page { display: none; flex-direction: column; align-items: center; justify-content: center; height: 100vh; text-align: center; }
|
60 |
+
.intro-page h1 { font-size: 2.5em; margin-bottom: 20px; animation: fadeIn 1s ease forwards; }
|
61 |
+
.intro-page p { font-size: 1.2em; margin-bottom: 30px; animation: fadeIn 1.5s ease forwards; }
|
62 |
+
.intro-page .search-container { width: 80%; max-width: 600px; }
|
63 |
+
.intro-page .search-box { animation: pulse 2s infinite; }
|
64 |
+
@keyframes fadeIn {
|
65 |
+
from { opacity: 0; transform: translateY(20px); }
|
66 |
+
to { opacity: 1; transform: translateY(0); }
|
67 |
+
}
|
68 |
+
@keyframes pulse {
|
69 |
+
50% { transform: scale(1.1); }
|
70 |
+
}
|
71 |
+
@keyframes slideDown {
|
72 |
+
from {
|
73 |
+
opacity: 0;
|
74 |
+
transform: translateY(-10px);
|
75 |
+
}
|
76 |
+
to {
|
77 |
+
opacity: 1;
|
78 |
+
transform: translateY(0);
|
79 |
+
}
|
80 |
+
}
|
81 |
</style>
|
82 |
+
</head>
|
83 |
+
<body>
|
84 |
+
<div class="intro-page" id="intro-page">
|
85 |
+
<h1>Welcome to HelpingAI Search</h1>
|
86 |
+
<p>Discover the web with our powerful search engine.</p>
|
87 |
+
<div class="search-container">
|
88 |
+
<form id="search-form">
|
89 |
+
<div class="search-box">
|
90 |
+
<input type="text" id="search-query" placeholder="Search the web" autocomplete="off">
|
91 |
+
<button type="submit"></button>
|
92 |
+
</div>
|
93 |
+
<div id="suggestions"></div>
|
94 |
+
</form>
|
95 |
+
</div>
|
96 |
+
</div>
|
97 |
+
<div class="main-content" id="main-content">
|
98 |
+
<div class="search-container">
|
99 |
+
<form id="main-search-form">
|
100 |
+
<div class="search-box">
|
101 |
+
<input type="text" id="main-search-query" placeholder="Search the web" autocomplete="off">
|
102 |
+
<button type="submit"></button>
|
103 |
+
</div>
|
104 |
+
<div id="main-suggestions"></div>
|
105 |
+
</form>
|
106 |
+
</div>
|
107 |
+
<div id="results-info" style="text-align: center;"></div>
|
108 |
+
<div id="results"></div>
|
109 |
+
<div id="no-results">
|
110 |
+
<p>No results found. Try refining your search.</p>
|
111 |
+
</div>
|
112 |
+
<div id="loading-more">Loading more results...</div>
|
113 |
+
<div class="loading-overlay">
|
114 |
+
<div class="loading-spinner"></div>
|
115 |
+
</div>
|
116 |
</div>
|
117 |
<script>
|
118 |
+
const BASE_URL = "https://oevortex-webscout-api.hf.space";
|
119 |
+
const searchForm = document.getElementById("search-form");
|
120 |
+
const searchQueryInput = document.getElementById("search-query");
|
121 |
+
const mainSearchForm = document.getElementById("main-search-form");
|
122 |
+
const mainSearchQueryInput = document.getElementById("main-search-query");
|
123 |
+
const resultsContainer = document.getElementById("results");
|
124 |
+
const suggestionsContainer = document.getElementById("suggestions");
|
125 |
+
const mainSuggestionsContainer = document.getElementById("main-suggestions");
|
126 |
+
const loadingOverlay = document.querySelector('.loading-overlay');
|
127 |
+
const noResultsMessage = document.getElementById('no-results');
|
128 |
+
const loadingMoreIndicator = document.getElementById('loading-more');
|
129 |
+
const introPage = document.getElementById('intro-page');
|
130 |
+
const mainContent = document.getElementById('main-content');
|
131 |
+
const INITIAL_RESULTS = 5;
|
132 |
+
const CACHED_RESULTS = 50;
|
133 |
+
const RESULTS_PER_PAGE = 10;
|
134 |
+
let allResultsFetched = false;
|
135 |
+
const seenUrls = new Set();
|
136 |
+
let selectedSuggestionIndex = -1;
|
137 |
+
let suggestionRequestTimeout;
|
138 |
+
let cachedSearchResults = [];
|
139 |
+
const suggestionCache = {};
|
140 |
+
let prefetchTimeout;
|
141 |
|
142 |
+
function debounce(func, delay) {
|
143 |
+
return function() {
|
144 |
+
clearTimeout(suggestionRequestTimeout);
|
145 |
+
suggestionRequestTimeout = setTimeout(() => {
|
146 |
func.apply(this, arguments);
|
147 |
+
}, delay);
|
148 |
+
};
|
149 |
+
}
|
150 |
+
|
151 |
+
async function fetchSuggestions(query) {
|
152 |
+
if (suggestionCache[query]) {
|
153 |
+
return suggestionCache[query];
|
154 |
+
}
|
155 |
+
try {
|
156 |
+
const response = await fetch(`${BASE_URL}/api/suggestions?q=${encodeURIComponent(query)}`);
|
157 |
+
if (response.ok) {
|
158 |
const suggestions = await response.json();
|
159 |
suggestionCache[query] = suggestions;
|
160 |
return suggestions;
|
161 |
+
} else {
|
162 |
console.error("Error fetching suggestions:", response.status);
|
163 |
return [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
164 |
}
|
165 |
+
} catch (error) {
|
166 |
+
console.error("Error fetching suggestions:", error);
|
167 |
+
return [];
|
168 |
+
}
|
169 |
+
}
|
170 |
|
171 |
+
// Event listeners for Intro Search
|
172 |
+
searchQueryInput.addEventListener("input", () => {
|
173 |
+
clearTimeout(prefetchTimeout);
|
174 |
+
const searchQuery = searchQueryInput.value.trim();
|
175 |
+
if (searchQuery === "") {
|
176 |
+
suggestionsContainer.style.display = "none";
|
177 |
+
return;
|
178 |
+
}
|
179 |
+
prefetchTimeout = setTimeout(async () => {
|
180 |
+
const suggestions = await fetchSuggestions(searchQuery);
|
181 |
+
displaySuggestions(suggestions);
|
182 |
+
}, 100);
|
183 |
+
});
|
184 |
+
|
185 |
+
searchForm.addEventListener("submit", async (event) => {
|
186 |
+
event.preventDefault();
|
187 |
+
selectedSuggestionIndex = -1;
|
188 |
+
const searchQuery = searchQueryInput.value;
|
189 |
+
performSearch(searchQuery);
|
190 |
+
});
|
191 |
+
|
192 |
+
// Event listeners for Main Content Search
|
193 |
+
mainSearchQueryInput.addEventListener("input", () => {
|
194 |
+
clearTimeout(prefetchTimeout);
|
195 |
+
const searchQuery = mainSearchQueryInput.value.trim();
|
196 |
+
if (searchQuery === "") {
|
197 |
+
mainSuggestionsContainer.style.display = "none";
|
198 |
+
return;
|
199 |
+
}
|
200 |
+
prefetchTimeout = setTimeout(async () => {
|
201 |
+
const suggestions = await fetchSuggestions(searchQuery);
|
202 |
+
displaySuggestions(suggestions, mainSuggestionsContainer);
|
203 |
+
}, 100);
|
204 |
+
});
|
205 |
+
|
206 |
+
mainSearchForm.addEventListener("submit", async (event) => {
|
207 |
+
event.preventDefault();
|
208 |
+
selectedSuggestionIndex = -1;
|
209 |
+
const searchQuery = mainSearchQueryInput.value;
|
210 |
+
performSearch(searchQuery);
|
211 |
+
});
|
212 |
+
|
213 |
+
function displaySuggestions(suggestions, container = suggestionsContainer) {
|
214 |
+
container.innerHTML = "";
|
215 |
+
if (suggestions.length === 0 || (searchQueryInput.value.trim() === "" && mainSearchQueryInput.value.trim() === "")) {
|
216 |
+
container.style.display = "none";
|
217 |
+
container.style.opacity = 0;
|
218 |
+
return;
|
219 |
+
}
|
220 |
+
const suggestionList = document.createElement("ul");
|
221 |
+
suggestions.forEach((suggestion, index) => {
|
222 |
+
const listItem = document.createElement("li");
|
223 |
+
listItem.textContent = suggestion.phrase;
|
224 |
+
listItem.addEventListener("click", () => {
|
225 |
+
if (container === suggestionsContainer) {
|
226 |
+
searchQueryInput.value = suggestion.phrase;
|
227 |
+
} else {
|
228 |
+
mainSearchQueryInput.value = suggestion.phrase;
|
229 |
+
}
|
230 |
+
container.style.display = "none";
|
231 |
+
container.style.opacity = 0;
|
232 |
performSearch(suggestion.phrase);
|
233 |
+
});
|
234 |
+
listItem.addEventListener("focus", () => {
|
235 |
selectedSuggestionIndex = index;
|
236 |
+
updateSuggestionSelection(container);
|
|
|
|
|
237 |
});
|
238 |
+
suggestionList.appendChild(listItem);
|
239 |
+
});
|
240 |
+
container.appendChild(suggestionList);
|
241 |
+
container.style.display = "block";
|
242 |
+
container.style.opacity = 1;
|
243 |
+
}
|
244 |
|
245 |
+
function updateSuggestionSelection(container) {
|
246 |
+
const suggestionItems = container.querySelectorAll("li");
|
247 |
+
suggestionItems.forEach((item, index) => {
|
248 |
+
item.classList.toggle("selected", index === selectedSuggestionIndex);
|
249 |
+
});
|
250 |
+
// Make sure the mainSuggestionsContainer is displayed when needed
|
251 |
+
if (container === mainSuggestionsContainer && selectedSuggestionIndex !== -1) {
|
252 |
+
mainSuggestionsContainer.style.display = "block";
|
253 |
+
}
|
254 |
+
}
|
255 |
+
|
256 |
+
function showLoading() {
|
257 |
+
loadingOverlay.style.display = 'block';
|
258 |
+
}
|
259 |
|
260 |
+
function hideLoading() {
|
261 |
+
loadingOverlay.style.display = 'none';
|
262 |
+
}
|
263 |
|
264 |
+
let startTime;
|
265 |
+
async function performSearch(query) {
|
266 |
+
showLoading();
|
267 |
+
seenUrls.clear();
|
268 |
+
allResultsFetched = false;
|
269 |
+
resultsContainer.innerHTML = '';
|
270 |
+
noResultsMessage.style.display = 'none';
|
271 |
+
loadingMoreIndicator.classList.remove('active');
|
272 |
+
suggestionsContainer.style.display = 'none';
|
273 |
+
mainSuggestionsContainer.style.display = 'none';
|
274 |
+
startTime = performance.now();
|
275 |
+
const initialResults = await fetchResults(query, INITIAL_RESULTS);
|
276 |
+
updateURLWithQuery(query);
|
277 |
+
displayResults(initialResults);
|
278 |
+
hideLoading();
|
279 |
+
if (initialResults.length > 0) {
|
280 |
+
cachedSearchResults = await fetchResults(query, CACHED_RESULTS);
|
281 |
+
cachedSearchResults = removeDuplicateResults(cachedSearchResults);
|
282 |
+
} else {
|
283 |
+
cachedSearchResults = [];
|
284 |
+
}
|
285 |
+
introPage.style.display = 'none';
|
286 |
+
mainContent.style.display = 'flex';
|
287 |
+
}
|
288 |
|
289 |
+
function updateURLWithQuery(query) {
|
290 |
+
const newURL = `${window.location.pathname}?query=${encodeURIComponent(query)}`;
|
291 |
+
window.history.pushState({ path: newURL }, '', newURL);
|
292 |
+
}
|
|
|
|
|
293 |
|
294 |
+
function removeDuplicateResults(results) {
|
295 |
+
const uniqueResults = [];
|
296 |
+
const seen = new Set();
|
297 |
+
for (const result of results) {
|
298 |
+
if (!seen.has(result.href)) {
|
299 |
seen.add(result.href);
|
300 |
uniqueResults.push(result);
|
|
|
301 |
}
|
302 |
+
}
|
303 |
+
return uniqueResults;
|
304 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
305 |
|
306 |
+
async function fetchResults(query, resultsPerPage) {
|
307 |
+
const response = await fetch(`${BASE_URL}/api/search?q=${encodeURIComponent(query)}&max_results=${resultsPerPage}`);
|
308 |
+
if (!response.ok) {
|
309 |
+
displayError("An error occurred while fetching results.");
|
310 |
+
hideLoading();
|
311 |
+
return [];
|
312 |
+
}
|
313 |
+
const searchResults = await response.json();
|
314 |
+
return searchResults;
|
315 |
+
}
|
316 |
+
|
317 |
+
function displayResults(results, append = false) {
|
318 |
+
if (!append) {
|
319 |
+
resultsContainer.innerHTML = '';
|
320 |
+
}
|
321 |
+
const newResults = results.filter(result => !seenUrls.has(result.href));
|
322 |
+
newResults.forEach((result, index) => {
|
323 |
+
seenUrls.add(result.href);
|
324 |
+
const resultElement = document.createElement("div");
|
325 |
+
resultElement.classList.add("result");
|
326 |
+
const titleElement = document.createElement("h3");
|
327 |
+
const titleLink = document.createElement("a");
|
328 |
+
titleLink.href = result.href;
|
329 |
+
titleLink.textContent = result.title;
|
330 |
+
titleLink.target = "_blank";
|
331 |
+
titleLink.rel = "noopener noreferrer";
|
332 |
+
titleElement.appendChild(titleLink);
|
333 |
+
const urlElement = document.createElement("div");
|
334 |
+
urlElement.classList.add("url");
|
335 |
+
const urlLink = document.createElement("a");
|
336 |
+
urlLink.href = result.href;
|
337 |
+
urlLink.textContent = result.href;
|
338 |
+
urlLink.target = "_blank";
|
339 |
+
urlLink.rel = "noopener noreferrer";
|
340 |
+
urlElement.appendChild(urlLink);
|
341 |
+
const descriptionElement = document.createElement("p");
|
342 |
+
descriptionElement.textContent = result.body;
|
343 |
+
resultElement.appendChild(titleElement);
|
344 |
+
resultElement.appendChild(urlElement);
|
345 |
+
resultElement.appendChild(descriptionElement);
|
346 |
+
resultsContainer.appendChild(resultElement);
|
347 |
+
setTimeout(() => {
|
348 |
resultElement.classList.add("show");
|
349 |
+
}, 100 * index);
|
350 |
+
});
|
351 |
+
noResultsMessage.style.display = resultsContainer.children.length === 0 ? 'block' : 'none';
|
352 |
+
if (!append) {
|
353 |
+
const endTime = performance.now();
|
354 |
+
const timeTaken = (endTime - startTime).toFixed(2);
|
355 |
+
document.getElementById('results-info').textContent = `About ${timeTaken} milliseconds`;
|
356 |
+
}
|
357 |
+
}
|
358 |
|
359 |
+
function displayError(message) {
|
360 |
+
resultsContainer.innerHTML = "";
|
361 |
+
const errorElement = document.createElement("p");
|
362 |
+
errorElement.textContent = message;
|
363 |
+
errorElement.style.color = "red";
|
364 |
+
resultsContainer.appendChild(errorElement);
|
365 |
+
}
|
366 |
+
|
367 |
+
searchQueryInput.addEventListener("input", debounce(async () => {
|
368 |
+
selectedSuggestionIndex = -1;
|
369 |
+
const searchQuery = searchQueryInput.value;
|
370 |
+
if (searchQuery.trim() === "") {
|
371 |
+
suggestionsContainer.style.display = "none";
|
372 |
+
return;
|
373 |
+
}
|
374 |
+
const suggestions = await fetchSuggestions(searchQuery);
|
375 |
+
displaySuggestions(suggestions);
|
376 |
+
}));
|
377 |
+
|
378 |
+
mainSearchQueryInput.addEventListener("input", debounce(async () => {
|
379 |
+
selectedSuggestionIndex = -1;
|
380 |
+
const searchQuery = mainSearchQueryInput.value;
|
381 |
+
if (searchQuery.trim() === "") {
|
382 |
+
mainSuggestionsContainer.style.display = "none";
|
383 |
+
return;
|
384 |
+
}
|
385 |
+
const suggestions = await fetchSuggestions(searchQuery);
|
386 |
+
displaySuggestions(suggestions, mainSuggestionsContainer);
|
387 |
+
}));
|
388 |
+
|
389 |
+
searchQueryInput.addEventListener("focus", () => {
|
390 |
+
if (searchQueryInput.value.trim() !== "") {
|
391 |
+
suggestionsContainer.style.display = "block";
|
392 |
+
}
|
393 |
+
});
|
394 |
+
mainSearchQueryInput.addEventListener("focus", () => {
|
395 |
+
if (mainSearchQueryInput.value.trim() !== "") {
|
396 |
+
mainSuggestionsContainer.style.display = "block";
|
397 |
+
}
|
398 |
+
});
|
399 |
+
|
400 |
+
document.addEventListener("click", (event) => {
|
401 |
+
if (!searchForm.contains(event.target) && !mainSearchForm.contains(event.target)) {
|
402 |
+
suggestionsContainer.style.display = "none";
|
403 |
+
mainSuggestionsContainer.style.display = "none";
|
404 |
+
}
|
405 |
+
});
|
406 |
+
|
407 |
+
searchQueryInput.addEventListener("keydown", async (event) => {
|
408 |
+
if (event.key === "ArrowUp" || event.key === "ArrowDown") {
|
409 |
+
event.preventDefault();
|
410 |
+
const suggestionItems = suggestionsContainer.querySelectorAll("li");
|
411 |
+
const numSuggestions = suggestionItems.length;
|
412 |
+
if (event.key === "ArrowUp") {
|
413 |
selectedSuggestionIndex = (selectedSuggestionIndex - 1 + numSuggestions) % numSuggestions;
|
414 |
+
} else {
|
415 |
selectedSuggestionIndex = (selectedSuggestionIndex + 1) % numSuggestions;
|
416 |
+
}
|
417 |
+
updateSuggestionSelection(suggestionsContainer);
|
418 |
+
if (selectedSuggestionIndex !== -1 && suggestionItems[selectedSuggestionIndex]) {
|
419 |
searchQueryInput.value = suggestionItems[selectedSuggestionIndex].textContent;
|
420 |
suggestionItems[selectedSuggestionIndex].focus();
|
421 |
+
}
|
422 |
+
} else if (event.key === "Enter" && selectedSuggestionIndex !== -1) {
|
423 |
+
event.preventDefault();
|
424 |
+
const selectedSuggestion = suggestionsContainer.querySelectorAll("li")[selectedSuggestionIndex];
|
425 |
+
if (selectedSuggestion) {
|
426 |
searchQueryInput.value = selectedSuggestion.textContent;
|
427 |
suggestionsContainer.style.display = "none";
|
428 |
performSearch(searchQueryInput.value);
|
|
|
429 |
}
|
430 |
+
}
|
431 |
+
});
|
432 |
+
mainSearchQueryInput.addEventListener("keydown", async (event) => {
|
433 |
+
if (event.key === "ArrowUp" || event.key === "ArrowDown") {
|
434 |
+
event.preventDefault();
|
435 |
+
const suggestionItems = mainSuggestionsContainer.querySelectorAll("li");
|
436 |
+
const numSuggestions = suggestionItems.length;
|
437 |
+
if (event.key === "ArrowUp") {
|
438 |
+
selectedSuggestionIndex = (selectedSuggestionIndex - 1 + numSuggestions) % numSuggestions;
|
439 |
+
} else {
|
440 |
+
selectedSuggestionIndex = (selectedSuggestionIndex + 1) % numSuggestions;
|
441 |
+
}
|
442 |
+
updateSuggestionSelection(mainSuggestionsContainer);
|
443 |
+
if (selectedSuggestionIndex !== -1 && suggestionItems[selectedSuggestionIndex]) {
|
444 |
+
mainSearchQueryInput.value = suggestionItems[selectedSuggestionIndex].textContent;
|
445 |
+
suggestionItems[selectedSuggestionIndex].focus();
|
446 |
+
}
|
447 |
+
} else if (event.key === "Enter" && selectedSuggestionIndex !== -1) {
|
448 |
event.preventDefault();
|
449 |
+
const selectedSuggestion = mainSuggestionsContainer.querySelectorAll("li")[selectedSuggestionIndex];
|
450 |
+
if (selectedSuggestion) {
|
451 |
+
mainSearchQueryInput.value = selectedSuggestion.textContent;
|
452 |
+
mainSuggestionsContainer.style.display = "none";
|
453 |
+
performSearch(mainSearchQueryInput.value);
|
454 |
+
}
|
455 |
+
}
|
456 |
+
});
|
457 |
+
|
458 |
+
searchForm.addEventListener("submit", async (event) => {
|
459 |
+
event.preventDefault();
|
460 |
+
selectedSuggestionIndex = -1;
|
461 |
+
const searchQuery = searchQueryInput.value;
|
462 |
+
performSearch(searchQuery);
|
463 |
+
});
|
464 |
+
mainSearchForm.addEventListener("submit", async (event) => {
|
465 |
+
event.preventDefault();
|
466 |
+
selectedSuggestionIndex = -1;
|
467 |
+
const searchQuery = mainSearchQueryInput.value;
|
468 |
+
performSearch(searchQuery);
|
469 |
+
});
|
470 |
+
|
471 |
+
window.addEventListener("scroll", () => {
|
472 |
+
if (allResultsFetched || cachedSearchResults.length === 0) return;
|
473 |
+
const { scrollTop, scrollHeight, clientHeight } = document.documentElement;
|
474 |
+
if (scrollTop + clientHeight >= scrollHeight - 100 && !loadingMoreIndicator.classList.contains("active")) {
|
475 |
+
loadingMoreIndicator.classList.add("active");
|
476 |
+
const resultsToDisplay = cachedSearchResults.splice(0, RESULTS_PER_PAGE);
|
477 |
+
if (resultsToDisplay.length > 0) {
|
478 |
displayResults(resultsToDisplay, true);
|
479 |
+
}
|
480 |
+
if (cachedSearchResults.length === 0) {
|
481 |
allResultsFetched = true;
|
|
|
|
|
482 |
}
|
483 |
+
loadingMoreIndicator.classList.remove("active");
|
484 |
+
}
|
485 |
+
});
|
486 |
|
487 |
+
function getQueryParameter(name) {
|
488 |
+
const urlParams = new URLSearchParams(window.location.search);
|
489 |
+
return urlParams.get(name);
|
490 |
+
}
|
491 |
+
|
492 |
+
window.addEventListener('load', () => {
|
493 |
+
const initialQuery = getQueryParameter('query');
|
494 |
+
if (initialQuery) {
|
495 |
+
mainSearchQueryInput.value = initialQuery;
|
496 |
+
performSearch(initialQuery);
|
497 |
+
} else {
|
498 |
+
introPage.style.display = 'flex';
|
499 |
+
mainContent.style.display = 'none';
|
500 |
+
}
|
501 |
+
});
|
502 |
</script>
|
503 |
+
</body>
|
504 |
</html>
|