@@ -193,6 +193,7 @@ fetch('/public-key').then(res=>res.json()).then(publicKey => {
193
193
194
194
async function postData ( url , searchTopic ) {
195
195
skeletonLoader ( ) ;
196
+ if ( public_key_server == null ) return
196
197
const searchTopicEnc = ks . encryption ( ( JSON . stringify ( searchTopic ) ) , public_key_server ) ;
197
198
console . log ( "Encrypted search" , searchTopicEnc )
198
199
// Create the request body with encrypted searchTopic and public key
@@ -260,10 +261,33 @@ function skeletonLoader() {
260
261
mainDivSelector . appendChild ( childDivElement ) ;
261
262
}
262
263
}
263
-
264
+ var searchResult
265
+ const searchQuery = document . getElementById ( "inputBox" ) ;
266
+ searchQuery . addEventListener ( 'keydown' , function ( event ) {
267
+ // Check if the key pressed is Enter (key code 13)
268
+ if ( event . keyCode === 13 ) {
269
+ // Prevent the default action (form submission, page reload, etc.)
270
+ event . preventDefault ( ) ;
271
+ const searchTopic = document . getElementById ( "inputBox" ) . value ;
272
+ console . log ( "Searching : " , searchTopic ) ;
273
+ postData ( "/submit" , { searchTopic :searchTopic } )
274
+ . then ( searchResult => {
275
+ console . log ( "search result before Decryption" , searchResult )
276
+
277
+ decryptedSearchResult = ks . decryption ( searchResult )
278
+
279
+ decryptedSearchResult = JSON . parse ( decryptedSearchResult )
280
+
281
+ populateMainDiv ( decryptedSearchResult )
282
+ } )
283
+ . catch ( error => {
284
+ console . error ( "Error: " , error ) ;
285
+ } ) ;
286
+ }
287
+ } ) ;
264
288
document . getElementById ( "searchBtn" ) . onclick = function ( ) {
265
289
const searchTopic = document . getElementById ( "inputBox" ) . value ;
266
-
290
+ console . log ( "Searching : " , searchTopic ) ;
267
291
postData ( "/submit" , { searchTopic :searchTopic } )
268
292
. then ( searchResult => {
269
293
console . log ( "search result before Decryption" , searchResult )
@@ -278,7 +302,19 @@ document.getElementById("searchBtn").onclick = function() {
278
302
console . error ( "Error: " , error ) ;
279
303
} ) ;
280
304
} ;
281
-
305
+ window . addEventListener ( 'beforeunload' , function ( event ) {
306
+ fetch ( "/exit" , {
307
+ method : "POST" ,
308
+ headers : {
309
+ "Content-Type" : "application/json"
310
+ } ,
311
+ body : JSON . stringify ( "Client left" )
312
+ } ) ;
313
+ // var ws = new WebSocket('ws://localhost:8080'); // Replace with your server's WebSocket URL
314
+ // ws.onopen = function() {
315
+ // ws.send('TabClosed'); // Send a message to the server
316
+ // };
317
+ } ) ;
282
318
/*const searchResult = [
283
319
{
284
320
"link": "https://www.javatpoint.com/array-in-java",
@@ -335,14 +371,14 @@ function populateMainDiv(searchResult) {
335
371
childDivElement . innerHTML = `<br>
336
372
<div class="search-result-element">
337
373
<div class="flex-container">
338
- <div> <a href="${ link } " class="icon"> <img src="${ googleFaviconGrabber + domain [ 2 ] } "> </a> </div>
374
+ <div> <a href="${ link } " class="icon" target="_blank" > <img src="${ googleFaviconGrabber + domain [ 2 ] } "> </a> </div>
339
375
<div>
340
- <div> <a href="${ link } " class="domain"> ${ domainParts [ 1 ] } </a> </div>
341
- <div> <a href="${ link } " class="link"> ${ link } </a> </div>
376
+ <div> <a href="${ link } " class="domain" target="_blank" > ${ domainParts [ 1 ] } </a> </div>
377
+ <div> <a href="${ link } " class="link" target="_blank" > ${ link } </a> </div>
342
378
</div>
343
379
</div>
344
380
<div class="heading-div">
345
- <a href="${ link } " class="heading"> ${ title } </a>
381
+ <a href="${ link } " class="heading" target="_blank" > ${ title } </a>
346
382
</div>
347
383
<div class="description">
348
384
${ desc . substring ( 0 , 225 ) + " ..." }
0 commit comments