Advertisement
cosenza987

Untitled

Apr 17th, 2024
929
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. async function main() {
  2.     const queryString = window.location.search;
  3.    
  4.     const urlParams = new URLSearchParams(queryString);
  5.    
  6.     let pokemon = urlParams.get('pokemon');
  7.    
  8.     if(pokemon == null) {
  9.         pokemon = "bulbasaur";
  10.     }
  11.  
  12.     let language = urlParams.get('language');
  13.    
  14.     if(language == null) {
  15.         language = "en";
  16.     }
  17.    
  18.     const url = "https://pokeapi.co/api/v2/pokemon/" + pokemon;
  19.     const firstResult = await fetch(url);
  20.     const firstData = await firstResult.json();
  21.  
  22.     console.log(firstData);
  23.  
  24.     const speciesResult = await fetch(firstData.species.url);
  25.     const speciesData = await speciesResult.json();
  26.  
  27.     let getname = "";
  28.    
  29.     speciesData.flavor_text_entries.forEach(async(entry) => {
  30.         let flavor = "";
  31.         if(entry.language.name.localeCompare(language) == 0){
  32.             const versionResult = await fetch(entry.version.url);
  33.             const versionData = await versionResult.json();
  34.             flavor += "<tr>";
  35.             versionData.names.forEach(name => {
  36.                 if(name.language.name.localeCompare(language) == 0) {
  37.                     flavor += "<th>" + name.name + "</th>";
  38.                 }
  39.             });
  40.             flavor += "<td>" + entry.flavor_text + "</td></tr>";
  41.         }
  42.         document.getElementById("flavor").innerHTML += flavor;
  43.     });
  44.  
  45.     speciesData.names.forEach(name => {
  46.         if(name.language.name.localeCompare(language) == 0) {
  47.             getname += name.name;
  48.         }
  49.     });
  50.  
  51.     document.getElementById("getname").innerHTML = getname;
  52.  
  53.     let image = "";
  54.     image += "<img src='" + firstData.sprites.front_default + "' id='sprite'/>";
  55.     image += "<img src='" + firstData.sprites.back_default + "' id='sprite'/>";
  56.     document.getElementById("image-normal").innerHTML = image;
  57.  
  58.     image = "";
  59.     image += "<img src='" + firstData.sprites.front_shiny + "' id='sprite'/>";
  60.     image += "<img src='" + firstData.sprites.back_shiny + "' id='sprite'/>";
  61.     document.getElementById("image-shiny").innerHTML = image;
  62.  
  63.     firstData.types.forEach(async(type) => {
  64.         const typeResult = await fetch(type.type.url);
  65.         const typeData = await typeResult.json();
  66.         let types = "";
  67.         typeData.names.forEach(name => {
  68.             if(name.language.name.localeCompare(language) == 0) {
  69.                 types += "<td><a class=\"type-icon type-" + typeData.name + "\">" + name.name + "</td>";
  70.             }
  71.         });
  72.         document.getElementById("types").innerHTML += types;
  73.     });
  74. }
  75.  
  76. main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement