selvaneyas commited on
Commit
eef4815
·
1 Parent(s): 0c87184

Update static/js/script.js

Browse files
Files changed (1) hide show
  1. static/js/script.js +129 -129
static/js/script.js CHANGED
@@ -1,129 +1,129 @@
1
- document.addEventListener("DOMContentLoaded", function () {
2
- fetch("navbar.html")
3
- .then(response => response.text())
4
- .then(data => {
5
- document.getElementById("navbar-container").innerHTML = data;
6
- })
7
- .catch(error => console.error("Error loading the navbar:", error));
8
- });
9
-
10
- async function searchWord() {
11
- let word = document.getElementById("wordInput").value.trim();
12
- let resultDiv = document.getElementById("result");
13
-
14
- if (word === "") {
15
- resultDiv.innerHTML = "<p style='color: red;'>❌ Please enter a word!</p>";
16
- return;
17
- }
18
-
19
- let url = `https://api.dictionaryapi.dev/api/v2/entries/en/${word}`;
20
-
21
- try {
22
- let response = await fetch(url);
23
- let data = await response.json();
24
-
25
- if (data.title) {
26
- resultDiv.innerHTML = `<p style="color: red;">❌ Word not found!</p>`;
27
- } else {
28
- let definition = data[0].meanings[0]?.definitions[0]?.definition || "No definition found.";
29
- let phonetics = data[0].phonetics[0]?.text || "No phonetics available.";
30
- let audio = data[0].phonetics[0]?.audio || "";
31
-
32
- resultDiv.innerHTML = `
33
- <h2>${word}</h2>
34
- <p><strong>Definition:</strong> ${definition}</p>
35
- <p><strong>Phonetics:</strong> ${phonetics}</p>
36
- ${audio ? `<audio controls><source src="${audio}" type="audio/mpeg"></audio>` : ""}
37
- `;
38
- }
39
- } catch (error) {
40
- resultDiv.innerHTML = "<p style='color: red;'>⚠️ Error fetching data. Please try again.</p>";
41
- }
42
- }
43
-
44
- const YOUTUBE_API_KEY = "AIzaSyBnWZp2Bc_lv1duMO0r3D4THx5tBhISeXg"; // Replace with your actual API Key
45
-
46
- function fetchContent() {
47
- let query = document.getElementById("searchQuery").value;
48
- if (!query) {
49
- alert("Please enter a topic.");
50
- return;
51
- }
52
-
53
- // Wikipedia API
54
- $("#wikiContent").html("Loading...");
55
- $.getJSON(`https://en.wikipedia.org/api/rest_v1/page/summary/${query}`, function(data) {
56
- $("#wikiContent").html(data.extract ? data.extract : "No summary available.");
57
- }).fail(function() {
58
- $("#wikiContent").html("Error fetching data.");
59
- });
60
-
61
- // JavaTpoint
62
- $("#javatpointContent").html("Loading...");
63
- let javaTpointLink = `https://www.javatpoint.com/${query.replace(/ /g, '-')}`;
64
- $("#javatpointContent").html(`<a href='${javaTpointLink}' target='_blank'>View on JavaTpoint</a>`);
65
-
66
-
67
- // YouTube API with SafeSearch and keyword filtering
68
- $("#youtubeContent").html("Loading...");
69
- $.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) {
70
- let videos = "";
71
- data.items.forEach(item => {
72
- let videoId = item.id.videoId;
73
- let title = item.snippet.title;
74
- let thumbnail = item.snippet.thumbnails.medium.url;
75
- let videoUrl = `https://www.youtube.com/watch?v=${videoId}`;
76
-
77
- videos += `
78
- <div class="card mb-2">
79
- <img src="${thumbnail}" alt="${title}" class="video-thumbnail" onclick="playVideo('${videoId}', this)">
80
- <div class="card-body">
81
- <p class="card-text">${title}</p>
82
- <a href="${videoUrl}" target="_blank" class="btn btn-sm btn-danger">Open in YouTube</a>
83
- </div>
84
- </div>
85
- `;
86
- });
87
- $("#youtubeContent").html(videos || "No videos found.");
88
- }).fail(function() {
89
- $("#youtubeContent").html("Error fetching videos.");
90
- });
91
- }
92
-
93
- function playVideo(videoId, element) {
94
- let videoFrame = `<iframe width='100%' height='300' src='https://www.youtube.com/embed/${videoId}' frameborder='0' allowfullscreen></iframe>`;
95
- let parentCard = element.closest(".card");
96
- parentCard.innerHTML = videoFrame;
97
- }
98
-
99
- function searchBox() {
100
- var query = document.getElementById("searchInput").value.toLowerCase();
101
- var pages = {
102
- "home": "index.html",
103
- "ai": "ai.html",
104
- "about": "about.html",
105
- "contact": "contact.html",
106
- "services": "services.html",
107
- "library": "library.html"
108
- };
109
-
110
- if (query in pages) {
111
- window.location.href = pages[query];
112
- } else {
113
- alert("Page not found. Try searching: Home, AI, About, Contact, Services, Library.");
114
- }
115
- return false; // Prevent form submission
116
- }
117
-
118
- // Function to load the footer dynamically
119
- function loadFooter() {
120
- fetch("footer.html")
121
- .then(response => response.text())
122
- .then(data => {
123
- document.getElementById("footer-container").innerHTML = data;
124
- })
125
- .catch(error => console.error("Error loading the footer:", error));
126
- }
127
-
128
- // Call the function after the page loads
129
- document.addEventListener("DOMContentLoaded", loadFooter);
 
1
+ document.addEventListener("DOMContentLoaded", function () {
2
+ fetch("/static/components/navbar.html")
3
+ .then(response => response.text())
4
+ .then(data => {
5
+ document.getElementById("navbar-container").innerHTML = data;
6
+ })
7
+ .catch(error => console.error("Error loading the navbar:", error));
8
+ });
9
+
10
+ async function searchWord() {
11
+ let word = document.getElementById("wordInput").value.trim();
12
+ let resultDiv = document.getElementById("result");
13
+
14
+ if (word === "") {
15
+ resultDiv.innerHTML = "<p style='color: red;'>❌ Please enter a word!</p>";
16
+ return;
17
+ }
18
+
19
+ let url = `https://api.dictionaryapi.dev/api/v2/entries/en/${word}`;
20
+
21
+ try {
22
+ let response = await fetch(url);
23
+ let data = await response.json();
24
+
25
+ if (data.title) {
26
+ resultDiv.innerHTML = `<p style="color: red;">❌ Word not found!</p>`;
27
+ } else {
28
+ let definition = data[0].meanings[0]?.definitions[0]?.definition || "No definition found.";
29
+ let phonetics = data[0].phonetics[0]?.text || "No phonetics available.";
30
+ let audio = data[0].phonetics[0]?.audio || "";
31
+
32
+ resultDiv.innerHTML = `
33
+ <h2>${word}</h2>
34
+ <p><strong>Definition:</strong> ${definition}</p>
35
+ <p><strong>Phonetics:</strong> ${phonetics}</p>
36
+ ${audio ? `<audio controls><source src="${audio}" type="audio/mpeg"></audio>` : ""}
37
+ `;
38
+ }
39
+ } catch (error) {
40
+ resultDiv.innerHTML = "<p style='color: red;'>⚠️ Error fetching data. Please try again.</p>";
41
+ }
42
+ }
43
+
44
+ const YOUTUBE_API_KEY = "AIzaSyBnWZp2Bc_lv1duMO0r3D4THx5tBhISeXg"; // Replace with your actual API Key
45
+
46
+ function fetchContent() {
47
+ let query = document.getElementById("searchQuery").value;
48
+ if (!query) {
49
+ alert("Please enter a topic.");
50
+ return;
51
+ }
52
+
53
+ // Wikipedia API
54
+ $("#wikiContent").html("Loading...");
55
+ $.getJSON(`https://en.wikipedia.org/api/rest_v1/page/summary/${query}`, function(data) {
56
+ $("#wikiContent").html(data.extract ? data.extract : "No summary available.");
57
+ }).fail(function() {
58
+ $("#wikiContent").html("Error fetching data.");
59
+ });
60
+
61
+ // JavaTpoint
62
+ $("#javatpointContent").html("Loading...");
63
+ let javaTpointLink = `https://www.javatpoint.com/${query.replace(/ /g, '-')}`;
64
+ $("#javatpointContent").html(`<a href='${javaTpointLink}' target='_blank'>View on JavaTpoint</a>`);
65
+
66
+
67
+ // YouTube API with SafeSearch and keyword filtering
68
+ $("#youtubeContent").html("Loading...");
69
+ $.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) {
70
+ let videos = "";
71
+ data.items.forEach(item => {
72
+ let videoId = item.id.videoId;
73
+ let title = item.snippet.title;
74
+ let thumbnail = item.snippet.thumbnails.medium.url;
75
+ let videoUrl = `https://www.youtube.com/watch?v=${videoId}`;
76
+
77
+ videos += `
78
+ <div class="card mb-2">
79
+ <img src="${thumbnail}" alt="${title}" class="video-thumbnail" onclick="playVideo('${videoId}', this)">
80
+ <div class="card-body">
81
+ <p class="card-text">${title}</p>
82
+ <a href="${videoUrl}" target="_blank" class="btn btn-sm btn-danger">Open in YouTube</a>
83
+ </div>
84
+ </div>
85
+ `;
86
+ });
87
+ $("#youtubeContent").html(videos || "No videos found.");
88
+ }).fail(function() {
89
+ $("#youtubeContent").html("Error fetching videos.");
90
+ });
91
+ }
92
+
93
+ function playVideo(videoId, element) {
94
+ let videoFrame = `<iframe width='100%' height='300' src='https://www.youtube.com/embed/${videoId}' frameborder='0' allowfullscreen></iframe>`;
95
+ let parentCard = element.closest(".card");
96
+ parentCard.innerHTML = videoFrame;
97
+ }
98
+
99
+ function searchBox() {
100
+ var query = document.getElementById("searchInput").value.toLowerCase();
101
+ var pages = {
102
+ "home": "index.html",
103
+ "ai": "ai.html",
104
+ "about": "about.html",
105
+ "contact": "contact.html",
106
+ "services": "services.html",
107
+ "library": "library.html"
108
+ };
109
+
110
+ if (query in pages) {
111
+ window.location.href = pages[query];
112
+ } else {
113
+ alert("Page not found. Try searching: Home, AI, About, Contact, Services, Library.");
114
+ }
115
+ return false; // Prevent form submission
116
+ }
117
+
118
+ // Function to load the footer dynamically
119
+ function loadFooter() {
120
+ fetch("/static/components/footer.html")
121
+ .then(response => response.text())
122
+ .then(data => {
123
+ document.getElementById("footer-container").innerHTML = data;
124
+ })
125
+ .catch(error => console.error("Error loading the footer:", error));
126
+ }
127
+
128
+ // Call the function after the page loads
129
+ document.addEventListener("DOMContentLoaded", loadFooter);