KingNish commited on
Commit
a98f1c4
·
verified ·
1 Parent(s): 9397fe3

Create script.js

Browse files
Files changed (1) hide show
  1. web/script.js +185 -0
web/script.js ADDED
@@ -0,0 +1,185 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const searchForm = document.getElementById("search-form");
2
+ const searchQueryInput = document.getElementById("search-query");
3
+ const timelimitSelect = document.getElementById("timelimit");
4
+ const safesearchSelect = document.getElementById("safesearch");
5
+ const resultsContainer = document.getElementById("results");
6
+ const suggestionsContainer = document.getElementById("suggestions");
7
+ const peopleAlsoSearchList = document.getElementById(
8
+ "people-also-search-list"
9
+ );
10
+ const peopleAlsoSearchSection = document.getElementById(
11
+ "people-also-search-section"
12
+ );
13
+ const aboutSection = document.getElementById("about-section");
14
+ const sidebar = document.getElementById("sidebar");
15
+
16
+ searchQueryInput.addEventListener("input", async () => {
17
+ const searchQuery = searchQueryInput.value;
18
+ if (searchQuery.trim() === "") {
19
+ suggestionsContainer.style.display = "none";
20
+ return;
21
+ }
22
+
23
+ try {
24
+ const data = await eel.get_suggestions(searchQuery)();
25
+ displaySuggestions(data);
26
+ } catch (error) {
27
+ console.error("Error:", error);
28
+ suggestionsContainer.style.display = "none";
29
+ }
30
+ });
31
+
32
+ searchQueryInput.addEventListener("focus", () => {
33
+ if (searchQueryInput.value.trim() !== "") {
34
+ suggestionsContainer.style.display = "block";
35
+ }
36
+ });
37
+
38
+ document.addEventListener("click", (event) => {
39
+ if (!searchForm.contains(event.target)) {
40
+ suggestionsContainer.style.display = "none";
41
+ }
42
+ });
43
+
44
+ searchForm.addEventListener("submit", async (event) => {
45
+ event.preventDefault();
46
+ const searchQuery = searchQueryInput.value;
47
+ const timelimit = timelimitSelect.value;
48
+ const safesearch = safesearchSelect.value;
49
+ await performSearch(searchQuery, timelimit, safesearch);
50
+ });
51
+
52
+ function displaySuggestions(suggestions) {
53
+ suggestionsContainer.innerHTML = "";
54
+ if (suggestions.length === 0) {
55
+ suggestionsContainer.style.display = "none";
56
+ return;
57
+ }
58
+ const suggestionList = document.createElement("ul");
59
+ suggestions.forEach((suggestion) => {
60
+ const listItem = document.createElement("li");
61
+ listItem.textContent = suggestion.phrase;
62
+ listItem.addEventListener("click", () => {
63
+ searchQueryInput.value = suggestion.phrase;
64
+ suggestionsContainer.style.display = "none";
65
+ performSearch(suggestion.phrase);
66
+ });
67
+ suggestionList.appendChild(listItem);
68
+ });
69
+ suggestionsContainer.appendChild(suggestionList);
70
+ suggestionsContainer.style.display = "block";
71
+ }
72
+
73
+ async function performSearch(query, timelimit, safesearch) {
74
+ try {
75
+ const searchResults = await eel.perform_search(
76
+ query,
77
+ timelimit,
78
+ safesearch
79
+ )();
80
+ displayResults(searchResults);
81
+ suggestionsContainer.style.display = "none";
82
+
83
+ // Get "People Also Search For" data
84
+ const peopleAlsoSearchData = await eel.get_people_also_search(query)();
85
+
86
+ // Display "About" section
87
+ const aboutDescription = peopleAlsoSearchData.find(
88
+ (item) => item.topic === null
89
+ );
90
+ if (aboutDescription) {
91
+ document.getElementById("about-description").textContent =
92
+ aboutDescription.text;
93
+ aboutSection.style.display = "block";
94
+ } else {
95
+ aboutSection.style.display = "none";
96
+ }
97
+
98
+ const peopleAlsoSearchItems = peopleAlsoSearchData.filter(
99
+ (item) => item.topic === "See also"
100
+ );
101
+ displayPeopleAlsoSearch(peopleAlsoSearchItems.slice(0, 6));
102
+ if (peopleAlsoSearchItems.length > 0) {
103
+ peopleAlsoSearchSection.style.display = "block";
104
+ } else {
105
+ peopleAlsoSearchSection.style.display = "none";
106
+ }
107
+
108
+ if (aboutSection.style.display === "block" || peopleAlsoSearchSection.style.display === "block") {
109
+ sidebar.style.display = "block";
110
+ document.querySelector(".main-content").style.marginLeft = "320px";
111
+ } else {
112
+ sidebar.style.display = "none";
113
+ document.querySelector(".main-content").style.marginLeft = "0px";
114
+ }
115
+ } catch (error) {
116
+ console.error("Error:", error);
117
+ displayError("An error occurred while fetching results.");
118
+ }
119
+ }
120
+
121
+ function displayPeopleAlsoSearch(data) {
122
+ peopleAlsoSearchList.innerHTML = "";
123
+
124
+ if (data.length === 0) {
125
+ return;
126
+ }
127
+
128
+ data.forEach((item) => {
129
+ const listItem = document.createElement("li");
130
+ const link = document.createElement("a");
131
+ link.href = item.url;
132
+ link.textContent = item.text;
133
+ link.target = "_blank";
134
+ link.rel = "noopener noreferrer";
135
+ listItem.appendChild(link);
136
+ peopleAlsoSearchList.appendChild(listItem);
137
+ });
138
+ }
139
+
140
+ function displayResults(results) {
141
+ resultsContainer.innerHTML = "";
142
+ if (results.length === 0) {
143
+ displayError("No results found.");
144
+ return;
145
+ }
146
+
147
+ results.forEach((result) => {
148
+ const resultElement = document.createElement("div");
149
+ resultElement.classList.add("result");
150
+
151
+ const titleElement = document.createElement("h3");
152
+ const titleLink = document.createElement("a");
153
+ titleLink.href = result.href;
154
+ titleLink.textContent = result.title;
155
+ titleLink.target = "_blank";
156
+ titleLink.rel = "noopener noreferrer";
157
+ titleElement.appendChild(titleLink);
158
+
159
+ const urlElement = document.createElement("div");
160
+ urlElement.classList.add("url");
161
+ urlElement.textContent = result.href;
162
+
163
+ const descriptionElement = document.createElement("p");
164
+ descriptionElement.textContent = result.body;
165
+
166
+ resultElement.appendChild(titleElement);
167
+ resultElement.appendChild(urlElement);
168
+ resultElement.appendChild(descriptionElement);
169
+ resultsContainer.appendChild(resultElement);
170
+ });
171
+ }
172
+
173
+ function displayError(message) {
174
+ resultsContainer.innerHTML = "";
175
+ const errorElement = document.createElement("p");
176
+ errorElement.textContent = message;
177
+ errorElement.style.color = "red";
178
+ resultsContainer.appendChild(errorElement);
179
+ }
180
+
181
+ function toggleSettings() {
182
+ const settingsMenu = document.getElementById("settings-menu");
183
+ settingsMenu.style.display =
184
+ settingsMenu.style.display === "block" ? "none" : "block";
185
+ }