-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
69 lines (62 loc) · 2.22 KB
/
script.js
File metadata and controls
69 lines (62 loc) · 2.22 KB
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
let api = "https://api.scryfall.com/cards/";
let cardName = document.getElementById("input_CardName");
let randomButton = document.getElementById("button_GetRandom");
seachEnter = document.getElementById("input_CardName");
button_GetSpecific = document.getElementById("button_GetSpecific");
randomButton.addEventListener("click", fetchRandom);
button_GetSpecific.addEventListener("click", fetchSpecific);
seachEnter.addEventListener("keypress", function (pressKey) {
// If the user presses the "Enter" key on the keyboard
if (pressKey.key === "Enter") {
fetchSpecific();
}
});
//Fetch random card data from scryfall api
async function fetchRandom() {
let data = api + "/random";
const response = await fetch(data);
const receivedData = await response.json();
displayCard(receivedData);
displayLegality(receivedData);
}
//Fetch specific card data from the scryfall api
async function fetchSpecific() {
let encoded = encodeURIComponent(cardName.value);
let data = api + "named?fuzzy=" + encoded;
const response = await fetch(data);
const receivedData = await response.json();
displayCard(receivedData);
displayLegality(receivedData);
}
//display img and card name
const displayCard = (recievedData) => {
document.getElementById("display_Container").style.backgroundColor =
"#2f3543";
const img = document.getElementById("display_MtgImg");
img.src = recievedData.image_uris.normal;
display_MtgCard.appendChild(img);
const cardHeader = document.getElementById("display_CardHeader");
cardHeader.textContent = recievedData.name;
};
//display in which formats the cards is legal
const displayLegality = (recievedData) => {
const ulLabel = document.getElementById("display_UlLabel");
while (ulLabel.firstChild) {
ulLabel.removeChild(ulLabel.firstChild);
}
ulLabel.textContent = "Legal formats:";
for (const legalities in recievedData) {
if (legalities == "legalities") {
for (const key in recievedData.legalities) {
const value = recievedData.legalities[key];
{
if (value === "legal") {
const legalFormat = document.createElement("li");
legalFormat.textContent = key;
ulLabel.appendChild(legalFormat);
}
}
}
}
}
};