// ChatGPT Search const chatgptSearch = document.querySelector('.chatgpt-search'); if (chatgptSearch) { const input = chatgptSearch.querySelector('[type="search"]'), submit = chatgptSearch.querySelector('[type="submit"]'); if (input) { // Check if the browser supports the Web Speech API if ('SpeechRecognition' in window || 'webkitSpeechRecognition' in window) { // // Create a new SpeechRecognition object const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)(); // When the speech recognition starts, set the input value to empty recognition.onstart = function () { // document.getElementById('speechInput').value = ''; input.value = ''; console.log('Speech recognition started.'); }; // When the speech recognition results are available, set the input value to the recognized speech recognition.onresult = function (event) { const transcript = event.results[0][0].transcript; // document.getElementById('speechInput').value = transcript; input.value = transcript; }; const buttonElement = document.createElement('div'); buttonElement.style.width = '20px'; buttonElement.style.top = '20px'; buttonElement.style.left = '25px'; buttonElement.style.position = 'absolute'; buttonElement.innerHTML = ` `; buttonElement.addEventListener('click', function () { recognition.start(); }); input.parentNode.appendChild(buttonElement); // Add event listener to the button to start speech recognition // document.getElementById('startButton').addEventListener('click', function () { // recognition.start(); // }); } else { console.log('Web Speech API is not supported in this browser.'); } if (submit) { submit.addEventListener('click', (e) => { e.preventDefault(); searchgpt(input); }); } input.addEventListener('keypress', (e) => { const keycode = (e.keyCode ? e.keyCode : e.which); if (keycode == '13') { searchgpt(input); } }); } } /** * */ const searchgpt = (input) => { const loading = document.createElement('div'); loading.classList.add('loading'); loading.innerText = '...'; input.parentNode.append(loading); let progress = 0; const interval = setInterval(() => { if (progress > 100) { clearInterval(interval); } else { if (progress >= 20) loading.innerText = 'Preparing your request ...'; if (progress >= 40) loading.innerText = 'AI is submitting your request to all the major airlines ...'; if (progress >= 60) loading.innerText = 'AI is processing all the ticket options being returned by the airlines ...'; if (progress >= 80) loading.innerText = 'It’s almost ready, hold tight!'; progress += 20; if (progress >= 90) { loading.innerText = 'Wait please ...'; loading.style.width = `90%`; } else { loading.style.width = `${progress}%`; } } }, 1000); input.disabled = true; const formData = new FormData(), xmlHttp = new XMLHttpRequest(); formData.append('query', input.value); xmlHttp.onreadystatechange = function() { if (xmlHttp.readyState == 4 && xmlHttp.status == 200) { clearInterval(interval); loading.innerText = 'Opening search ...'; loading.style.width = `100%`; setTimeout(() => { input.disabled = false; loading.remove(); }, 300); const json = JSON.parse(xmlHttp.responseText); if (json.trips.length == 1) { location.href = `/search/one-way/${json.trips[0].origin},${json.trips[0].destination},${json.trips[0].date}/${json.cabinClass}/${json.passengers.adults}/${json.passengers.children}/${json.passengers.infants}/${json.override}`; } else if (json.trips.length == 2 && json.trips[0].origin === json.trips[1].destination && json.trips[0].destination === json.trips[1].origin) { location.href = `/search/round-trip/${json.trips[0].origin},${json.trips[0].destination},${json.trips[0].date},${json.trips[1].date}/${json.cabinClass}/${json.passengers.adults}/${json.passengers.children}/${json.passengers.infants}/${json.override}`; } else if (json.trips.length >= 3) { let trips = []; for (const trip of json.trips) trips.push(`${trip.origin},${trip.destination},${trip.date}`); location.href = `/search/multi-city/${trips.join(';')}/${json.cabinClass}/${json.passengers.adults}/${json.passengers.children}/${json.passengers.infants}/${json.override}`; } else { alert('Sorry something went wront!'); } } } xmlHttp.open('POST.html', '/api/chatgpt/search'); xmlHttp.send(formData); };