-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
142 lines (127 loc) · 4.72 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
document.addEventListener("DOMContentLoaded", () => {
const searchInput = document.getElementById("searchInput");
const searchButton = document.getElementById("searchButton");
const resultsList = document.getElementById("results");
const articleDiv = document.getElementById("article");
const backButton = document.getElementById("backButton");
const articleTitle = document.getElementById("articleTitle");
const articleContent = document.getElementById("articleContent");
const languageSelect = document.getElementById("languageSelect");
const suggestionList = document.getElementById("suggestionList");
let currentLanguage = "en";
// Change language dynamically
languageSelect.addEventListener("change", () => {
currentLanguage = languageSelect.value;
});
// Handle Search Button Click
searchButton.addEventListener("click", () => {
const query = searchInput.value.trim();
if (query) {
fetchSearchResults(query);
} else {
alert("Please enter a search term.");
}
});
// Real-time Suggestions
searchInput.addEventListener("input", () => {
const query = searchInput.value.trim();
if (query) {
fetchSuggestions(query);
} else {
suggestionList.classList.add("d-none");
}
});
// Fetch Suggestions
function fetchSuggestions(query) {
fetch(
`https://${currentLanguage}.wikipedia.org/w/api.php?action=opensearch&search=${encodeURIComponent(
query
)}&limit=10&namespace=0&format=json&origin=*`
)
.then((response) => response.json())
.then((data) => {
displaySuggestions(data[1]);
})
.catch((error) => console.error("Error fetching suggestions:", error));
}
// Display Suggestions
function displaySuggestions(suggestions) {
suggestionList.innerHTML = ""; // Clear previous suggestions
if (suggestions.length > 0) {
suggestions.forEach((suggestion) => {
const li = document.createElement("li");
li.textContent = suggestion;
li.className = "list-group-item list-group-item-action";
li.addEventListener("click", () => {
searchInput.value = suggestion; // Fill input with clicked suggestion
suggestionList.classList.add("d-none"); // Hide suggestions
fetchSearchResults(suggestion); // Trigger search
});
suggestionList.appendChild(li);
});
suggestionList.classList.remove("d-none"); // Show suggestions
} else {
suggestionList.classList.add("d-none"); // Hide if no suggestions
}
}
// Fetch search results
function fetchSearchResults(query) {
resultsList.innerHTML = ""; // Clear previous results
articleDiv.classList.add("hidden");
fetch(
`https://${currentLanguage}.wikipedia.org/w/api.php?action=query&list=search&srsearch=${encodeURIComponent(
query
)}&format=json&origin=*`
)
.then((response) => response.json())
.then((data) => {
if (data.query && data.query.search.length > 0) {
displayResults(data.query.search);
} else {
resultsList.innerHTML = "<p>No results found. Try a different query.</p>";
}
})
.catch((error) => console.error("Error fetching search results:", error));
}
// Display search results
function displayResults(results) {
resultsList.innerHTML = ""; // Clear previous results
results.forEach((result) => {
const li = document.createElement("li");
li.textContent = result.title;
li.dataset.pageId = result.pageid;
li.tabIndex = 0; // Make it focusable for accessibility
li.setAttribute("role", "button");
li.addEventListener("click", () => fetchArticle(result.pageid, result.title));
resultsList.appendChild(li);
});
}
// Fetch article details
function fetchArticle(pageId, title) {
fetch(
`https://${currentLanguage}.wikipedia.org/w/api.php?action=parse&pageid=${pageId}&prop=text&format=json&origin=*`
)
.then((response) => response.json())
.then((data) => {
if (data.parse && data.parse.text) {
const htmlContent = data.parse.text["*"];
displayArticle(title, htmlContent);
} else {
alert("Article content could not be fetched.");
}
})
.catch((error) => console.error("Error fetching article:", error));
}
// Display article content
function displayArticle(title, htmlContent) {
articleTitle.textContent = title;
articleContent.innerHTML = htmlContent;
resultsList.innerHTML = ""; // Clear results
articleDiv.classList.remove("hidden");
}
// Back Button Functionality
backButton.addEventListener("click", () => {
articleDiv.classList.add("hidden");
resultsList.innerHTML = ""; // Clear the article view
});
});