-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
167 lines (162 loc) · 5.22 KB
/
script.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const searchField = $('#search_field')
const goBtn = $('#search-button')
const placeholderDiv = $('#plot')
const container = $('.container')
const video = $('#video-space')
const titleDiv = $('#title')
const pBox = $('#p-box')
const youtubeDiv = $("#youtube")
const lastsearchDiv = $("#pGames")
var input = ''
var gameArray = JSON.parse(localStorage.getItem("prevGames")) || []
var top10 = $('#top-10')
var top10Btn = $('#10-btn')
goBtn.on('click', function (event) {
$('#alert').hide()
event.preventDefault()
pBox.empty()
input = searchField.val()
inputStr = input.replace(/\s+/g, '-').toLowerCase()
$("#balloon").empty()
getInfo(input)
})
function getInfo(input) {
$("body").attr("id", 'tv');
$("body").css("color", "white");
const settings = {
"async": true,
"crossDomain": true,
"url": "https://rawg-video-games-database.p.rapidapi.com/games/" + inputStr,
"method": "GET",
"headers": {
"x-rapidapi-key": "7524861152msh237a3378b10e9bfp1a411ejsn270bb159b78f",
"x-rapidapi-host": "rawg-video-games-database.p.rapidapi.com"
}
};
$.ajax(settings).then(function (response) {
if (response.redirect) {
input = response.slug
inputStr = input.replace(/\s+/g, '-').toLowerCase()
getInfo()
}
else {
placeholderDiv.html(response.description)
video.html('<img src=' + response.background_image + '>')
title = response.name
titleDiv.html(title)
removeDuplicates
gameArray.push(title)
addToList()
getYT()
$('#yBtn').show()
}
}).catch(function (error) {
$('#alert').show()
if (error.status === 404) {
$('#balloon').append("Game " + error.statusText)
}
else if (error.status >= 500) {
$('#balloon').append("Server Down, Please try again later")
}
});
}
function getYT() {
youtubeDiv.empty()
var userInput = input
var queryURL = "https://www.googleapis.com/youtube/v3/search?part=snippet&key=AIzaSyCa_6yhtb95grtNTn6fca0k_dhcGkioEjk&type=video&q=" + userInput
$.ajax({
url: queryURL,
method: "GET",
}).then(function (response) {
for (var i = 0; i < 3; i++) {
$(youtubeDiv).append('<div class="player"><iframe width="250" height="250" src="https://www.youtube.com/embed/' + response.items[i].id.videoId + '"frameborder="0" allowfullscreen></iframe></div>');
}
}).catch(function (error) {
if (error.status === 403) {
youtubeDiv.text("youtube quota is full, please try again tomorrow")
}
})
}
function addToList() {
removeDuplicates()
JSON.parse(localStorage.getItem("prevGames"))
for (var i = 0; i < gameArray.length; i++) {
var glist = $('<li>' + gameArray[i] + "</li>")
lastsearchDiv.prepend(glist)
}
}
function removeDuplicates() {
gameArrayUnique = gameArray.filter(
function (a) { if (!this[a]) { this[a] = 1; return a; } }, {}
)
gameArray = gameArrayUnique
maxGameArray()
localStorage.setItem('prevGames', JSON.stringify(gameArray))
lastsearchDiv.empty()
}
function maxGameArray() {
if (gameArray.length === 5) {
gameArray.shift()
}
}
function getPastSearch(event) {
var target = event.target;
if (event.target.matches("li")) {
input = target.textContent.trim(2);
inputStr = input.replace(/\s+/g, '-').replace(/:|!/g,'').replace(/\(|\)/g, "").toLowerCase()
input=inputStr
$("#balloon").empty()
pBox.empty()
getInfo();
$('#alert').hide()
}
}
$(document).on("click", getPastSearch);
addToList()
function top10Fun() {
const listSettings = {
"async": true,
"crossDomain": true,
"url": "https://rawg-video-games-database.p.rapidapi.com/games",
"method": "GET",
"headers": {
"x-rapidapi-key": "7524861152msh237a3378b10e9bfp1a411ejsn270bb159b78f",
"x-rapidapi-host": "rawg-video-games-database.p.rapidapi.com"
}
};
$.ajax(listSettings).done(function (response) {
topGameArray = response.results;
top10.empty()
for (let i = 0; i < 10; i++) {
const currentGame = topGameArray[i];
top10.append('<li>' + currentGame.name)
}
});
}
$(top10Btn).on('click', top10Fun)
const openModalButtons = document.querySelectorAll("[data-modal-target]")
const closeModalButtons = document.querySelectorAll("[data-close-button]")
const overlay = document.getElementById("overlay")
openModalButtons.forEach(button => {
button.addEventListener("click", () => {
const modal = document.querySelector(button.dataset.modalTarget)
openModal(modal)
})
})
closeModalButtons.forEach(button => {
button.addEventListener("click", () => {
const modal = button.closest('.modal')
closeModal(modal)
})
})
function openModal(modal) {
if (modal == null) return
modal.classList.add('active')
overlay.classList.add('active')
}
function closeModal(modal) {
if (modal == null) return
modal.classList.remove('active')
overlay.classList.remove('active')
}
$('#alert').hide()