-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
94 lines (79 loc) · 2.56 KB
/
script.js
File metadata and controls
94 lines (79 loc) · 2.56 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
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
const dataUrl = "./data.json";
const pokemonInput = document.getElementById("pokemonInput");
const jsonOutput = document.getElementById("jsonOutput");
const pokemonImage = document.getElementById("pokemonImage");
// Event listeners
pokemonInput.addEventListener("input", handleInput);
pokemonInput.addEventListener("keypress", (e) => {
if (e.key === "Enter") {
handleInput();
}
});
// Load Pikachu by default
window.addEventListener("load", () => {
pokemonInput.value = "#25";
handleInput();
});
async function handleInput() {
const input = pokemonInput.value.trim();
if (!input) {
jsonOutput.textContent = "";
jsonOutput.className = "";
pokemonImage.style.display = "none";
return;
}
try {
const pokemon = await getPokemonData(input);
if (pokemon) {
displayJSON(pokemon);
} else {
jsonOutput.textContent = `Error: Pokémon ${input} not found`;
jsonOutput.className = "error";
pokemonImage.style.display = "none";
}
} catch (error) {
jsonOutput.textContent = `Error: Failed to load data - ${error.message}`;
jsonOutput.className = "error";
pokemonImage.style.display = "none";
}
}
async function getPokemonData(input) {
const response = await fetch(dataUrl);
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
const data = await response.json();
// Handle different input formats
let id;
if (input.startsWith("#")) {
// Handle ID input like #25
id = input.substring(1);
} else {
// Handle numeric input like 25
id = input;
}
const paddedId = id.padStart(4, "0");
return data.find((p) => p.id.substring(1) === paddedId);
}
function displayJSON(pokemon) {
const formatted = JSON.stringify(pokemon, null, 2);
// Clear previous highlighting
jsonOutput.className = "";
jsonOutput.removeAttribute("data-highlighted");
jsonOutput.innerHTML = "";
// Set new content and highlight
jsonOutput.textContent = formatted;
jsonOutput.className = "language-json";
hljs.highlightElement(jsonOutput);
// Show Pokemon image from PokéDB with fallback
const pokemonId = parseInt(pokemon.id.substring(1));
const pokemonName = pokemon.name.english
.toLowerCase()
.replace(/[^a-z0-9]/g, "");
pokemonImage.src = `https://img.pokemondb.net/sprites/home/normal/${pokemonName}.png`;
pokemonImage.onerror = () => {
// Fallback to PokeAPI official artwork
pokemonImage.src = `https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/other/official-artwork/${pokemonId}.png`;
};
pokemonImage.style.display = "block";
}