document.addEventListener("DOMContentLoaded", function () { fetch("/static/components/navbar.html") .then(response => response.text()) .then(data => { document.getElementById("navbar-container").innerHTML = data; }) .catch(error => console.error("Error loading the navbar:", error)); }); async function searchWord() { let word = document.getElementById("wordInput").value.trim(); let resultDiv = document.getElementById("result"); if (word === "") { resultDiv.innerHTML = "

❌ Please enter a word!

"; return; } let url = `https://api.dictionaryapi.dev/api/v2/entries/en/${word}`; try { let response = await fetch(url); let data = await response.json(); if (data.title) { resultDiv.innerHTML = `

❌ Word not found!

`; } else { let definition = data[0].meanings[0]?.definitions[0]?.definition || "No definition found."; let phonetics = data[0].phonetics[0]?.text || "No phonetics available."; let audio = data[0].phonetics[0]?.audio || ""; resultDiv.innerHTML = `

${word}

Definition: ${definition}

Phonetics: ${phonetics}

${audio ? `` : ""} `; } } catch (error) { resultDiv.innerHTML = "

âš ī¸ Error fetching data. Please try again.

"; } } function handleKeyPress0(event){ if (event.key === "Enter") { searchWord(); } } const YOUTUBE_API_KEY = "AIzaSyBnWZp2Bc_lv1duMO0r3D4THx5tBhISeXg"; // Replace with your actual API Key function fetchContent() { let query = document.getElementById("searchQuery").value; if (!query) { alert("Please enter a topic."); return; } // Wikipedia API $("#wikiContent").html("Loading..."); $.getJSON(`https://en.wikipedia.org/api/rest_v1/page/summary/${query}`, function(data) { $("#wikiContent").html(data.extract ? data.extract : "No summary available."); }).fail(function() { $("#wikiContent").html("Error fetching data."); }); // JavaTpoint $("#javatpointContent").html("Loading..."); let javaTpointLink = `https://www.javatpoint.com/${query.replace(/ /g, '-')}`; $("#javatpointContent").html(`View on JavaTpoint`); $("#mediumContent").html("Loading..."); let mediumLink = `https://medium.com/search?q=${query.replace(/ /g, '+')}`; $("#mediumContent").html(`View on Medium`); $("#quoraContent").html("Loading..."); let quoraLink = `https://www.quora.com/search?q=${query.replace(/ /g, '+')}`; $("#quoraContent").html(`View on Quora`); // YouTube API with SafeSearch and keyword filtering $("#youtubeContent").html("Loading..."); $.getJSON(`https://www.googleapis.com/youtube/v3/search?part=snippet&q=${query} tutorial|lecture|course&type=video&maxResults=5&safeSearch=strict&key=${YOUTUBE_API_KEY}`, function(data) { let videos = ""; data.items.forEach(item => { let videoId = item.id.videoId; let title = item.snippet.title; let thumbnail = item.snippet.thumbnails.medium.url; let videoUrl = `https://www.youtube.com/watch?v=${videoId}`; videos += `
${title}

${title}

Open in YouTube
`; }); $("#youtubeContent").html(videos || "No videos found."); }).fail(function() { $("#youtubeContent").html("Error fetching videos."); }); } function handleKeyPress(event) { if (event.key === "Enter") { fetchContent(); } } function playVideo(videoId, element) { let videoFrame = ``; let parentCard = element.closest(".card"); parentCard.innerHTML = videoFrame; } function searchBox() { var query = document.getElementById("searchInput").value.toLowerCase(); var pages = { "home": "index.html", "ai": "ai.html", "about": "about.html", "contact": "contact.html", "services": "services.html", "library": "library.html" }; if (query in pages) { window.location.href = pages[query]; } else { alert("Page not found. Try searching: Home, AI, About, Contact, Services, Library."); } return false; // Prevent form submission } let recognition; let isListening = false; let speechSynthesisUtterance; let isSpeaking = false; // 🎤 Speech-to-Text Function function toggleSpeechRecognition() { if (!recognition) { recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)(); recognition.lang = "en-US"; recognition.interimResults = false; recognition.maxAlternatives = 1; recognition.onstart = function() { document.getElementById("speak-btn").textContent = "🛑 Stop"; }; recognition.onresult = function(event) { let transcript = event.results[0][0].transcript; document.getElementById("user-input").value = transcript; }; recognition.onerror = function(event) { console.error("Error:", event.error); }; recognition.onend = function() { document.getElementById("speak-btn").textContent = "🎤 Speak"; isListening = false; }; } if (isListening) { recognition.stop(); } else { recognition.start(); } isListening = !isListening; } // 🔊 Text-to-Speech Function function toggleReadAloud(button, text) { if (!isSpeaking) { if ('speechSynthesis' in window) { speechSynthesis.cancel(); speechSynthesisUtterance = new SpeechSynthesisUtterance(text); speechSynthesisUtterance.lang = 'en-US'; speechSynthesisUtterance.rate = 1; speechSynthesisUtterance.onstart = () => { button.textContent = "âšī¸ Stop"; }; speechSynthesisUtterance.onend = () => { button.textContent = "🔊 Read"; isSpeaking = false; }; speechSynthesis.speak(speechSynthesisUtterance); isSpeaking = true; } else { alert("Text-to-Speech is not supported in your browser."); } } else { speechSynthesis.cancel(); button.textContent = "🔊 Read"; isSpeaking = false; } } // Library // Function to load the footer dynamically function loadFooter() { fetch("/static/components/footer.html") .then(response => response.text()) .then(data => { document.getElementById("footer-container").innerHTML = data; }) .catch(error => console.error("Error loading the footer:", error)); } // Call the function after the page loads document.addEventListener("DOMContentLoaded", loadFooter);