-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
59 lines (57 loc) · 1.75 KB
/
main.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
var api = "https://en.wikipedia.org/w/api.php?action=opensearch&format=json&search=";
function searchArticles(item) {
$(".search-results").html("");
$.ajax({
type: "GET",
url: api + item,
dataType: "jsonp",
crossDomain: true,
success: function(results) {
if (results[1].length === 0) {
$(".loading").html("No results found.");
$(".loading").removeClass("hidden");
} else {
var keywords = results[1];
var descriptions = results[2];
var urls = results[3];
var html = "";
for (var i = 0; i < keywords.length; i++) {
html +=
'<a target="_blank" href="' +
urls[i] +
'"><div class="card"><div class="title">' +
keywords[i].toUpperCase() +
'</div><div class="divider"></div><div class="desc">' +
descriptions[i] +
"</div></div></a>";
}
$(".loading").html("");
$(".loading").addClass("hidden");
if($("#searchBox").val() === item) {
$(".search-results").html(html);
} else if ($("#searchBox").val() === "") {
$(".loading").html("Start typing to get results...");
$(".loading").removeClass("hidden");
}
}
}
});
}
$(document).ready(function() {
$("#random-article").on("click", function() {
window.open("https://en.wikipedia.org/wiki/Special:Random", "_blank");
});
$("#searchBox").on("keyup search", function() {
$(".search-results").html("");
$(".loading").html("Loading...");
$(".loading").removeClass("hidden");
var searchString = $(this).val();
if (searchString !== "") {
setTimeout(searchArticles(searchString), 2000);
} else {
$(".loading").html("Start typing to get results...");
$(".loading").removeClass("hidden");
$(".search-results").html("");
}
});
});