JagadeshMagesh commited on
Commit
9d5fb99
·
verified ·
1 Parent(s): e912956

Create index.html

Browse files
Files changed (1) hide show
  1. index.html +606 -0
index.html ADDED
@@ -0,0 +1,606 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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>
7
+ <link rel="icon" href="https://www.gstatic.com/favicon/favicon.ico" type="image/x-icon">
8
+ <link rel="preconnect" href="https://fonts.googleapis.com">
9
+ <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
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%;margin-bottom:20px}
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{width:calc(100% - 40px);border:none;outline:0;font-size:16px;padding:4px 0;transition:font-size .2s ease}
22
+ #search-query::placeholder{color:#9aa0a6;transition:color .2s ease}
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";color:#9aa0a6;transition:color .2s ease,transform .2s ease;font:900 1.2em "Font Awesome 5 Free"}
29
+ #search-form button:hover::after{color:#4285f4;transform:scale(1.1)}
30
+ #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}
31
+ @keyframes spin {
32
+ 0%{transform:rotate(0)}
33
+ 100%{transform:rotate(360deg)}
34
+ }
35
+ #suggestions ul{list-style-type:none;padding:0;margin:0}
36
+ #suggestions li{padding:8px 12px;cursor:pointer;border-bottom:1px solid #eee;transition:background-color .2s ease}
37
+ #suggestions li:hover{background-color:#e9e9e9}
38
+ #suggestions li.selected{background-color:#f0f0f0}
39
+ .search-box:focus-within+#suggestions,.search-box:hover+#suggestions{display:block;opacity:1;transform:translateY(0)}
40
+ #results{width:100%;max-width:700px;margin-top:20px}
41
+ .result,.ai-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}
42
+ .ai-result{background-color:#f0f0f5;width:100%;max-width:800px}
43
+ @keyframes fadeInUp {
44
+ from{opacity:0;transform:translateY(20px)}
45
+ to{opacity:1;transform:translateY(0)}
46
+ }
47
+ .result.show,.ai-result.show{opacity:1;transform:translateY(0)}
48
+ .result:hover,.ai-result:hover{box-shadow:0 4px 8px rgba(0,0,0,0.2)}
49
+ .result h3{margin:0 0 5px;font-size:1.2rem;color:#222}
50
+ .result .url{color:#202124;font-size:.9rem;margin-bottom:8px;display:block;max-width:100%;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
51
+ .result p{color:#555;font-size:.9rem;line-height:1.6em;margin:0}
52
+ .loading-overlay{display:none;position:fixed;top:0;left:0;width:100%;height:100%;background-color:rgba(0,0,0,0);z-index:1000}
53
+ .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}
54
+ @keyframes spin {
55
+ 0%{transform:rotate(0deg)}
56
+ 100%{transform:rotate(360deg)}
57
+ }
58
+ .loading-spinner{width:40px;height:40px;border-radius:50%;border:5px solid #f3f3f3;border-top:5px solid #3498db;animation:spin 1.2s linear infinite}
59
+ .loading-text{margin-left:10px;font-size:1rem;color:#333}
60
+ #no-results{display:none;text-align:center;padding:20px;font-size:1.1em;color:#555}
61
+ .result .actions button{background-color:#f2f2f2;color:#000;border:1px solid #ddd;padding:8px 16px;border-radius:20px;font-size:.9rem;font-weight:700;cursor:pointer;transition:background-color .2s ease,box-shadow .2s ease;margin-right:10px}
62
+ .result .actions button:hover{background-color:#e0e0e0;box-shadow:0 2px 4px rgba(0,0,0,0.1)}
63
+ .summary-popup,.answer-popup{border-radius:16px;box-shadow:0 4px 12px rgba(0,0,0,0.25);background-color:#f8f9fa;color:#333;font-family:'Poppins',sans-serif;padding:20px;position:fixed;left:50%;top:50%;transform:translate(-50%,-50%);max-width:80%;max-height:80%;overflow-y:auto;z-index:1001}
64
+ .summary-popup .close,.answer-popup .close{position:absolute;top:15px;right:15px;cursor:pointer;font-size:1.5em;color:#666;transition:color .2s ease}
65
+ .summary-popup .close:hover,.answer-popup .close:hover{color:#333}
66
+ .summary-popup .loading,.answer-popup .loading{display:flex;justify-content:center;align-items:center;height:100px;display:none}
67
+ .summary-popup .loading-spinner,.answer-popup .loading-spinner{width:60px;height:60px;border-radius:50%;border:5px solid #f3f3f3;border-top:5px solid #3498db;animation:spin 1.2s linear infinite}
68
+ .summary-popup .content,.answer-popup .content{padding:20px;font-size:1rem;line-height:1.5}
69
+ .summary-popup #summaryContent,.answer-popup #answerContent{font-family:'Poppins',sans-serif;margin-bottom:20px}
70
+ #no-results{display:none;text-align:center;padding:20px;font-size:1.1em;color:#555}
71
+ #loading-more{display:none;text-align:center;padding:10px}
72
+ #loading-more.active{display:block}
73
+ .ai-result h2{margin:0 0 10px;font-size:1.5rem;font-weight:700;color:#333}
74
+ .ai-result p{color:#444;font-size:.9rem;line-height:1.5em;margin:0;display:flex;flex-direction:column}
75
+ .ai-result .actions {
76
+ display: flex;
77
+ justify-content: flex-end; /* Align buttons to the right */
78
+ margin-top: 10px;
79
+ }
80
+ .ai-result .actions button {
81
+ background-color: #f2f2f2;
82
+ color: #000;
83
+ border: 1px solid #ddd;
84
+ padding: 8px 12px; /* Adjust padding if needed */
85
+ border-radius: 20px;
86
+ font-size: .9rem;
87
+ font-weight: 700;
88
+ cursor: pointer;
89
+ transition: background-color .2s ease, box-shadow .2s ease;
90
+ margin-left: 10px; /* Add space between buttons */
91
+ }
92
+ .ai-result .actions button:hover {
93
+ background-color: #e0e0e0;
94
+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
95
+ }
96
+ .ai-result .actions button i { /* Style Font Awesome icons */
97
+ font-size: 1.2rem; /* Adjust icon size as needed */
98
+ color: #333;
99
+ }
100
+ </style>
101
+ </head>
102
+ <body>
103
+ <div class="main-content">
104
+ <div class="search-container">
105
+ <form id="search-form">
106
+ <div class="search-box">
107
+ <input type="text" id="search-query" placeholder="Search the web" autocomplete="off">
108
+ <button type="submit"></button>
109
+ </div>
110
+ <div id="suggestions"></div>
111
+ </form>
112
+ </div>
113
+ <div id="ai-response"></div>
114
+ <div id="results-info" style="text-align: center;"></div>
115
+ <div id="results"></div>
116
+ <div id="no-results">
117
+ <p>No results found. Try refining your search.</p>
118
+ </div>
119
+ <div id="loading-more">Loading more results...</div>
120
+ <div class="loading-overlay">
121
+ <div class="loading-spinner"></div>
122
+ </div>
123
+ </div>
124
+ <div id="summaryPopup" class="summary-popup" style="display: none;">
125
+ <span class="close">×</span>
126
+ <div class="content">
127
+ <div class="loading">
128
+ <div class="loading-spinner"></div>
129
+ <p class="loading-text">Loading...</p>
130
+ </div>
131
+ <div id="summaryContent"></div>
132
+ </div>
133
+ </div>
134
+ <div id="answerPopup" class="answer-popup" style="display: none;">
135
+ <span class="close">×</span>
136
+ <div class="content">
137
+ <div class="loading">
138
+ <div class="loading-spinner"></div>
139
+ <p class="loading-text">Loading...</p>
140
+ </div>
141
+ <div id="answerContent"></div>
142
+ </div>
143
+ </div>
144
+ <script>
145
+ const BASE_URL = "https://oevortex-webscout-api.hf.space";
146
+ const searchForm = document.getElementById("search-form");
147
+ const searchQueryInput = document.getElementById("search-query");
148
+ const resultsContainer = document.getElementById("results");
149
+ const suggestionsContainer = document.getElementById("suggestions");
150
+ const loadingOverlay = document.querySelector('.loading-overlay');
151
+ const noResultsMessage = document.getElementById('no-results');
152
+ const loadingMoreIndicator = document.getElementById('loading-more');
153
+ const aiResponseContainer = document.getElementById('ai-response');
154
+ const INITIAL_RESULTS = 5;
155
+ const CACHED_RESULTS = 50;
156
+ const RESULTS_PER_PAGE = 5;
157
+ let allResultsFetched = false;
158
+ const seenUrls = new Set();
159
+ let selectedSuggestionIndex = -1;
160
+ let suggestionRequestTimeout;
161
+ let cachedSearchResults = [];
162
+ const suggestionCache = {};
163
+ let prefetchTimeout;
164
+ let allResults = [];
165
+ let startTime;
166
+ function debounce(func, delay) {
167
+ return function() {
168
+ clearTimeout(suggestionRequestTimeout);
169
+ suggestionRequestTimeout = setTimeout(() => {
170
+ func.apply(this, arguments);
171
+ }, delay);
172
+ };
173
+ }
174
+ async function fetchSuggestions(query) {
175
+ if (suggestionCache[query]) {
176
+ return suggestionCache[query];
177
+ }
178
+ try {
179
+ const response = await fetch(`${BASE_URL}/api/suggestions?q=${encodeURIComponent(query)}`);
180
+ if (response.ok) {
181
+ const suggestions = await response.json();
182
+ suggestionCache[query] = suggestions;
183
+ return suggestions;
184
+ } else {
185
+ console.error("Error fetching suggestions:", response.status);
186
+ return [];
187
+ }
188
+ } catch (error) {
189
+ console.error("Error fetching suggestions:", error);
190
+ return [];
191
+ }
192
+ }
193
+ searchQueryInput.addEventListener("input", () => {
194
+ clearTimeout(prefetchTimeout);
195
+ const searchQuery = searchQueryInput.value.trim();
196
+ if (searchQuery === "") {
197
+ suggestionsContainer.style.display = "none";
198
+ return;
199
+ }
200
+ prefetchTimeout = setTimeout(async () => {
201
+ const suggestions = await fetchSuggestions(searchQuery);
202
+ displaySuggestions(suggestions);
203
+ }, 100);
204
+ });
205
+ function displaySuggestions(suggestions) {
206
+ suggestionsContainer.innerHTML = "";
207
+ if (suggestions.length === 0 || searchQueryInput.value.trim() === "") {
208
+ suggestionsContainer.style.display = "none";
209
+ return;
210
+ }
211
+ const suggestionList = document.createElement("ul");
212
+ suggestions.forEach((suggestion, index) => {
213
+ const listItem = document.createElement("li");
214
+ listItem.textContent = suggestion.phrase;
215
+ listItem.addEventListener("click", () => {
216
+ searchQueryInput.value = suggestion.phrase;
217
+ suggestionsContainer.style.display = "none";
218
+ performSearch(suggestion.phrase);
219
+ });
220
+ listItem.addEventListener("focus", () => {
221
+ selectedSuggestionIndex = index;
222
+ updateSuggestionSelection();
223
+ });
224
+ suggestionList.appendChild(listItem);
225
+ });
226
+ suggestionsContainer.appendChild(suggestionList);
227
+ suggestionsContainer.style.display = "block";
228
+ }
229
+ function updateSuggestionSelection() {
230
+ const suggestionItems = suggestionsContainer.querySelectorAll("li");
231
+ suggestionItems.forEach((item, index) => {
232
+ item.classList.toggle("selected", index === selectedSuggestionIndex);
233
+ });
234
+ }
235
+ function showLoading() {
236
+ loadingOverlay.style.display = 'block';
237
+ }
238
+ function hideLoading() {
239
+ loadingOverlay.style.display = 'none';
240
+ }
241
+ async function performSearch(query) {
242
+ showLoading();
243
+ aiResponseContainer.style.display = 'none';
244
+ suggestionsContainer.style.display = "none";
245
+ startTime = performance.now();
246
+ seenUrls.clear();
247
+ allResultsFetched = false;
248
+ resultsContainer.innerHTML = '';
249
+ noResultsMessage.style.display = 'none';
250
+ loadingMoreIndicator.classList.remove('active');
251
+ speechSynthesis.cancel();
252
+ const initialResults = await fetchResults(query, INITIAL_RESULTS);
253
+ displayResults(initialResults);
254
+ hideLoading();
255
+ fetchResults(query, CACHED_RESULTS).then(cachedResults => {
256
+ cachedSearchResults = removeDuplicateResults(cachedResults);
257
+ allResults = allResults.concat(cachedSearchResults);
258
+ displayResults(cachedSearchResults.slice(INITIAL_RESULTS, RESULTS_PER_PAGE), true);
259
+ if (cachedSearchResults.length > RESULTS_PER_PAGE) {
260
+ allResultsFetched = false;
261
+ loadingMoreIndicator.classList.add('active');
262
+ }
263
+ });
264
+ fetchAIResponse(query).then(aiResponse => {
265
+ displayAIResponse(aiResponse);
266
+ aiResponseContainer.style.display = 'block';
267
+ }).catch(error => {
268
+ console.error("Error fetching AI response:", error);
269
+ });
270
+ updateURLWithQuery(query);
271
+ }
272
+ async function fetchAIResponse(query) {
273
+ try {
274
+ const encodedQuery = encodeURIComponent(query);
275
+ const websiteURL = 'https://google.com/search?q=${encodedQuery}'
276
+ const response = await fetch(`${BASE_URL}/api/ask_website?url=https://google.com/search?q=${encodedQuery}&question=Answer this question from google search result ${encodedQuery}&model=gpt-4o-mini`);
277
+ if (response.ok) {
278
+ const aiResponse = await response.json();
279
+ return aiResponse;
280
+ } else {
281
+ console.error("Error fetching AI response from website:", response.status);
282
+ return null;
283
+ }
284
+ } catch (error) {
285
+ console.error("Error fetching AI response from website:", error);
286
+ return null;
287
+ }
288
+ }
289
+ function displayAIResponse(response) {
290
+ aiResponseContainer.innerHTML = '';
291
+ if (response) {
292
+ const aiResultElement = document.createElement('div');
293
+ aiResultElement.classList.add('ai-result');
294
+ const aiHeading = document.createElement('h2');
295
+ aiHeading.textContent = "AI Response";
296
+ aiResultElement.appendChild(aiHeading);
297
+ const aiText = document.createElement('p');
298
+ const decodedResponse = decodeHtml(response);
299
+ const msg = new SpeechSynthesisUtterance(decodedResponse);
300
+ speechSynthesis.speak(msg);
301
+ aiText.textContent = decodedResponse;
302
+ const pauseButton = document.createElement('button');
303
+ pauseButton.id = 'pause';
304
+ pauseButton.innerHTML = '<i class="fas fa-pause"></i>';
305
+ const stopButton = document.createElement('button');
306
+ stopButton.id = 'stop';
307
+ stopButton.innerHTML = '<i class="fas fa-stop"></i>';
308
+ let isPaused = false;
309
+ let isStoped = false;
310
+ pauseButton.addEventListener('click', () => {
311
+ if ('speechSynthesis' in window) {
312
+ if (isPaused) {
313
+ window.speechSynthesis.resume();
314
+ isPaused = false;
315
+ stopButton.style.display = 'inline-block';
316
+ pauseButton.innerHTML = '<i class="fas fa-pause"></i>';
317
+ } else {
318
+ window.speechSynthesis.pause();
319
+ isPaused = true;
320
+ stopButton.style.display = 'none';
321
+ pauseButton.innerHTML = '<i class="fas fa-play"></i>';
322
+ }
323
+ }
324
+ });
325
+ stopButton.addEventListener('click', () => {
326
+ if ('speechSynthesis' in window) {
327
+ if (isStoped){
328
+ speechSynthesis.speak(msg);
329
+ isPaused = false;
330
+ isStoped = false;
331
+ pauseButton.innerHTML = '<i class="fas fa-pause"></i>';
332
+ pauseButton.style.display = 'inline-block';
333
+ stopButton.innerHTML = '<i class="fas fa-stop"></i>';
334
+ }else{
335
+ window.speechSynthesis.cancel();
336
+ isPaused = false;
337
+ isStoped = true;
338
+ pauseButton.style.display = 'none';
339
+ stopButton.innerHTML = '<i class="fas fa-play"></i>'; }
340
+ }
341
+ });
342
+ // Add the buttons to the actions container
343
+ aiResultElement.appendChild(pauseButton);
344
+ aiResultElement.appendChild(stopButton);
345
+ // Style the AI response text
346
+ aiText.setAttribute('style', 'font-size: 1rem; line-height: 1.5rem;');
347
+ aiResultElement.appendChild(aiText);
348
+ aiResponseContainer.appendChild(aiResultElement);
349
+ }
350
+ }
351
+ function updateURLWithQuery(query) {
352
+ const newURL = `${window.location.pathname}?query=${encodeURIComponent(query)}`;
353
+ window.history.pushState({
354
+ path: newURL
355
+ }, '', newURL);
356
+ }
357
+ function removeDuplicateResults(results) {
358
+ const uniqueResults = [];
359
+ const seen = new Set();
360
+ for (const result of results) {
361
+ if (!seen.has(result.href)) {
362
+ seen.add(result.href);
363
+ uniqueResults.push(result);
364
+ }
365
+ }
366
+ return uniqueResults;
367
+ }
368
+ async function fetchResults(query, resultsPerPage) {
369
+ const response = await fetch(`${BASE_URL}/api/search?q=${encodeURIComponent(query)}&max_results=${resultsPerPage}`);
370
+ if (!response.ok) {
371
+ displayError("An error occurred while fetching results.");
372
+ hideLoading();
373
+ return [];
374
+ }
375
+ const searchResults = await response.json();
376
+ return searchResults;
377
+ }
378
+ const summaryPopup = document.getElementById('summaryPopup');
379
+ const summaryContent = document.getElementById('summaryContent');
380
+ const answerPopup = document.getElementById('answerPopup');
381
+ const answerContent = document.getElementById('answerContent');
382
+ const summaryCache = {};
383
+ const answerCache = {};
384
+ function displayResults(results, append = false) {
385
+ if (!append) {
386
+ resultsContainer.innerHTML = '';
387
+ }
388
+ const newResults = results.filter(result => !seenUrls.has(result.href));
389
+ newResults.forEach((result, index) => {
390
+ seenUrls.add(result.href);
391
+ const resultElement = document.createElement("div");
392
+ resultElement.classList.add("result");
393
+ const titleElement = document.createElement("h3");
394
+ const titleLink = document.createElement("a");
395
+ titleLink.href = result.href;
396
+ titleLink.textContent = result.title;
397
+ titleLink.target = "_blank";
398
+ titleLink.rel = "noopener noreferrer";
399
+ titleElement.appendChild(titleLink);
400
+ const urlElement = document.createElement("div");
401
+ urlElement.classList.add("url");
402
+ const urlLink = document.createElement("a");
403
+ urlLink.href = result.href;
404
+ urlLink.textContent = result.href;
405
+ urlLink.target = "_blank";
406
+ urlLink.rel = "noopener noreferrer";
407
+ urlElement.appendChild(urlLink);
408
+ const descriptionElement = document.createElement("p");
409
+ descriptionElement.textContent = result.body;
410
+ resultElement.appendChild(titleElement);
411
+ resultElement.appendChild(urlElement);
412
+ resultElement.appendChild(descriptionElement);
413
+ const actionsContainer = document.createElement('div');
414
+ actionsContainer.classList.add('actions');
415
+ const summarizeButton = document.createElement('button');
416
+ summarizeButton.textContent = 'Summarize this site';
417
+ summarizeButton.addEventListener('click', () => {
418
+ showSummaryPopup(result.href);
419
+ });
420
+ actionsContainer.appendChild(summarizeButton);
421
+ const answerButton = document.createElement('button');
422
+ answerButton.textContent = 'Answer from this site';
423
+ answerButton.addEventListener('click', () => {
424
+ showAnswerPopup(result.href);
425
+ });
426
+ actionsContainer.appendChild(answerButton);
427
+ resultElement.appendChild(actionsContainer);
428
+ resultsContainer.appendChild(resultElement);
429
+ setTimeout(() => {
430
+ resultElement.classList.add("show");
431
+ }, 1 * index);
432
+ });
433
+ if (resultsContainer.children.length === 0) {
434
+ noResultsMessage.style.display = 'block';
435
+ } else {
436
+ noResultsMessage.style.display = 'none';
437
+ }
438
+ if (!append) {
439
+ const endTime = performance.now();
440
+ const timeTaken = (endTime/2 - startTime/2).toFixed(2);
441
+ document.getElementById('results-info').textContent = `About ${timeTaken} milliseconds`;
442
+ }
443
+ }
444
+ function displayError(message) {
445
+ resultsContainer.innerHTML = '';
446
+ const errorElement = document.createElement("p");
447
+ errorElement.textContent = message;
448
+ errorElement.style.color = "red";
449
+ resultsContainer.appendChild(errorElement);
450
+ }
451
+ searchQueryInput.addEventListener("input", debounce(async () => {
452
+ selectedSuggestionIndex = -1;
453
+ const searchQuery = searchQueryInput.value;
454
+ if (searchQuery.trim() === "") {
455
+ suggestionsContainer.style.display = "none";
456
+ return;
457
+ }
458
+ const suggestions = await fetchSuggestions(searchQuery);
459
+ displaySuggestions(suggestions);
460
+ }, 500));
461
+ searchQueryInput.addEventListener("focus", () => {
462
+ if (searchQueryInput.value.trim() !== "") {
463
+ suggestionsContainer.style.display = "block";
464
+ }
465
+ });
466
+ document.addEventListener("click", (event) => {
467
+ if (!searchForm.contains(event.target)) {
468
+ suggestionsContainer.style.display = "none";
469
+ }
470
+ });
471
+ searchQueryInput.addEventListener("keydown", async (event) => {
472
+ if (event.key === "ArrowUp" || event.key === "ArrowDown") {
473
+ event.preventDefault();
474
+ const suggestionItems = suggestionsContainer.querySelectorAll("li");
475
+ const numSuggestions = suggestionItems.length;
476
+ if (event.key === "ArrowUp") {
477
+ selectedSuggestionIndex = (selectedSuggestionIndex - 1 + numSuggestions) % numSuggestions;
478
+ } else {
479
+ selectedSuggestionIndex = (selectedSuggestionIndex + 1) % numSuggestions;
480
+ }
481
+ updateSuggestionSelection();
482
+ if (selectedSuggestionIndex !== -1 && suggestionItems[selectedSuggestionIndex]) {
483
+ searchQueryInput.value = suggestionItems[selectedSuggestionIndex].textContent;
484
+ suggestionItems[selectedSuggestionIndex].focus();
485
+ }
486
+ } else if (event.key === "Enter" && selectedSuggestionIndex !== -1) {
487
+ event.preventDefault();
488
+ const selectedSuggestion = suggestionsContainer.querySelectorAll("li")[selectedSuggestionIndex];
489
+ if (selectedSuggestion) {
490
+ searchQueryInput.value = selectedSuggestion.textContent;
491
+ suggestionsContainer.style.display = "none";
492
+ performSearch(searchQueryInput.value);
493
+ }
494
+ }
495
+ });
496
+ searchForm.addEventListener("submit", async (event) => {
497
+ event.preventDefault();
498
+ selectedSuggestionIndex = -1;
499
+ const searchQuery = searchQueryInput.value;
500
+ performSearch(searchQuery);
501
+ });
502
+ window.addEventListener('scroll', () => {
503
+ if (allResultsFetched) return;
504
+ const {
505
+ scrollTop,
506
+ scrollHeight,
507
+ clientHeight
508
+ } = document.documentElement;
509
+ const isNearBottom = scrollTop + clientHeight >= scrollHeight - 100;
510
+ if (isNearBottom && allResults.length > seenUrls.size) {
511
+ loadingMoreIndicator.classList.add('active');
512
+ const resultsToDisplay = allResults.slice(seenUrls.size, seenUrls.size + RESULTS_PER_PAGE);
513
+ setTimeout(() => {
514
+ displayResults(resultsToDisplay, true);
515
+ for (const result of resultsToDisplay) {
516
+ seenUrls.add(result.href);
517
+ }
518
+ if (allResults.length === seenUrls.size) {
519
+ allResultsFetched = true;
520
+ loadingMoreIndicator.classList.remove('active');
521
+ }
522
+ }, 300);
523
+ }
524
+ });
525
+ function getQueryParameter(name) {
526
+ const urlParams = new URLSearchParams(window.location.search);
527
+ return urlParams.get(name);
528
+ }
529
+ window.addEventListener('load', () => {
530
+ const initialQuery = getQueryParameter('query');
531
+ if (initialQuery) {
532
+ searchQueryInput.value = initialQuery;
533
+ performSearch(initialQuery);
534
+ }
535
+ });
536
+ function decodeHtml(html) {
537
+ var txt = document.createElement("textarea");
538
+ txt.innerHTML = html;
539
+ return txt.value;
540
+ }
541
+ function showSummaryPopup(url) {
542
+ summaryContent.innerHTML = '';
543
+ summaryPopup.querySelector('.loading').style.display = 'block';
544
+ summaryPopup.style.display = 'block';
545
+ if (summaryCache[url]) {
546
+ displaySummary(summaryCache[url]);
547
+ } else {
548
+ fetchWebsiteSummary(url).then(summary => {
549
+ summaryCache[url] = summary;
550
+ displaySummary(summary);
551
+ }).catch(error => {
552
+ console.error('Error fetching summary:', error);
553
+ displaySummary('Error fetching summary.');
554
+ });
555
+ }
556
+ }
557
+ function displaySummary(summary) {
558
+ summaryPopup.querySelector('.loading').style.display = 'none';
559
+ summaryContent.textContent = summary;
560
+ }
561
+ async function fetchWebsiteSummary(url) {
562
+ const response = await fetch(`${BASE_URL}/api/ask_website?url=${url}&question=${encodeURIComponent("Summarize this site in Detail, Do not start with any intro, Just start summarizing. Summarize in this manner which is very important to human user.")}&model=mixtral-8x7b`);
563
+ if (!response.ok) {
564
+ throw new Error('Error fetching website summary.');
565
+ }
566
+ const data = await response.json();
567
+ return data[0] || 'No summary available';
568
+ }
569
+ function showAnswerPopup(url) {
570
+ answerContent.innerHTML = '';
571
+ answerPopup.querySelector('.loading').style.display = 'block';
572
+ answerPopup.style.display = 'block';
573
+ if (answerCache[url]) {
574
+ displayAnswer(answerCache[url]);
575
+ } else {
576
+ const question = searchQueryInput.value;
577
+ fetchWebsiteAnswer(url, question).then(answer => {
578
+ answerCache[url] = answer;
579
+ displayAnswer(answer);
580
+ }).catch(error => {
581
+ console.error('Error fetching answer:', error);
582
+ displayAnswer('Error fetching answer.');
583
+ });
584
+ }
585
+ }
586
+ function displayAnswer(answer) {
587
+ answerPopup.querySelector('.loading').style.display = 'none';
588
+ answerContent.textContent = answer;
589
+ }
590
+ async function fetchWebsiteAnswer(url, question) {
591
+ const response = await fetch(`${BASE_URL}/api/ask_website?url=${url}&question=${encodeURIComponent(question)}(encodeURIComponent("Answer in concise way, First tell answer then Other thing"))&model=llama-3-70b`);
592
+ if (!response.ok) {
593
+ throw new Error('Error fetching answer from website.');
594
+ }
595
+ const data = await response.json();
596
+ return data[0] || 'No answer found.';
597
+ }
598
+ summaryPopup.querySelector('.close').addEventListener('click', () => {
599
+ summaryPopup.style.display = 'none';
600
+ });
601
+ answerPopup.querySelector('.close').addEventListener('click', () => {
602
+ answerPopup.style.display = 'none';
603
+ });
604
+ </script>
605
+ </body>
606
+ </html>