-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
63 lines (55 loc) · 1.8 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
document.getElementById("searchButton").addEventListener("click", function () {
const searchTerm = document.getElementById("searchQuery").value.trim();
if (!searchTerm) {
alert("Please enter a search term.");
return;
}
const endpoint = "https://www.wikidata.org/w/api.php";
const params = {
origin: "*",
action: "wbsearchentities",
format: "json",
search: searchTerm,
language: "en",
limit: 30,
};
const queryString = new URLSearchParams(params).toString();
fetch(`${endpoint}?${queryString}`, {
method: "GET",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
})
.then((response) => {
if (!response.ok) {
throw new Error(`Error: ${response.status}`);
}
return response.json();
})
.then((data) => {
const resultsContainer = document.getElementById("results");
resultsContainer.innerHTML = "";
if (data.search && data.search.length > 0) {
data.search.forEach((item) => {
const resultDiv = document.createElement("div");
resultDiv.className = "result";
const label = item.label || "No label";
const description = item.description || "No description";
const entityURL = `https://www.wikidata.org/wiki/${item.id}`;
resultDiv.innerHTML = `
<strong>${label}</strong>: ${description}
<br>
<a href="${entityURL}" target="_blank">View on Wikidata</a>
`;
resultsContainer.appendChild(resultDiv);
});
} else {
resultsContainer.innerHTML = "No results found.";
}
})
.catch((error) => {
console.error("Request error:", error);
alert("An error occurred while processing the request.");
});
});