script.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // global variables
  2. // CORS errors are cringe
  3. const pokedex = "https://codeberg.org/zortazert/pages/raw/branch/main/homepage/pokemon.json"
  4. const castforms = {
  5. "sunny": "https://archives.bulbagarden.net/media/upload/thumb/9/90/0351Castform-Sunny.png/120px-0351Castform-Sunny.png",
  6. "snowy": "https://archives.bulbagarden.net/media/upload/thumb/3/34/0351Castform-Snowy.png/120px-0351Castform-Snowy.png",
  7. "rainy": "https://archives.bulbagarden.net/media/upload/thumb/a/ab/0351Castform-Rainy.png/120px-0351Castform-Rainy.png",
  8. "normal": "https://archives.bulbagarden.net/media/upload/thumb/f/ff/0351Castform.png/120px-0351Castform.png"
  9. }
  10. const phases = {
  11. "New Moon": "🌑",
  12. "Waxing Crescent": "🌒",
  13. "First Quarter": "🌓",
  14. "Waxing Gibbous": "🌔",
  15. "Full Moon": "🌕",
  16. "Waning Gibbous": "🌖",
  17. "Last Quarter": "🌗",
  18. "Waning Crescent": "🌘",
  19. }
  20. var latlon = "";
  21. // GEO location
  22. function successCallback(position) {
  23. console.log(position);
  24. latlon = `${position.coords.latitude},${position.coords.longitude}`;
  25. };
  26. const errorCallback = (error) => {
  27. console.log(error);
  28. };
  29. navigator.geolocation.getCurrentPosition(successCallback, errorCallback);
  30. fetch(`https://wttr.in/${latlon}?format=j1`)
  31. .then((response) => response.json())
  32. .then((data) => {
  33. var description = data.current_condition[0].weatherDesc[0].value.toLowerCase();
  34. console.log(description)
  35. var form = "";
  36. if (description.includes("sun")) {
  37. form = "sunny";
  38. } else if (description.includes("cold")) {
  39. form = "snowy";
  40. } else if (description.includes("rain")) {
  41. form = "rainy";
  42. } else {
  43. form = "normal"
  44. }
  45. console.log(form)
  46. document.querySelector(".weather").innerHTML = `<img src="${castforms[form]}">`;
  47. moonPhase = data.weather[0].astronomy[0].moon_phase;
  48. console.log(moonPhase);
  49. document.querySelector(".phase").innerHTML = `<p>${phases[moonPhase]} ${moonPhase}</p>`;
  50. })
  51. .catch((err) => {
  52. document.querySelector(".info").innerHTML = `
  53. <div>
  54. <p>Could not find this 😟. Try again.</p>
  55. </div>`;
  56. console.log(err);
  57. });
  58. function typeFormatter(types) {
  59. var typeFormat = [];
  60. // https://www.w3schools.com/js/js_loop_for.asp
  61. // https://www.freecodecamp.org/news/how-to-capitalize-words-in-javascript/
  62. // https://stackoverflow.com/questions/351409/how-to-append-something-to-an-array
  63. for (let i = 0; i < types.length; i++) {
  64. typeFormat.push(types[i][0].toUpperCase() + types[i].substring(1));
  65. }
  66. return typeFormat.join(", ")
  67. }
  68. fetch("https://api.allorigins.win/raw?url="+encodeURIComponent(pokedex))
  69. .then((response) => response.json())
  70. .then((data) => {
  71. const value = data[Math.floor(Math.random() * Object.keys(data).length)];
  72. console.log(value)
  73. document.querySelector(".image").innerHTML = `<img src="${value["ThumbnailImage"]}">`;
  74. document.querySelector(".info").innerHTML = `
  75. <p><b>Number</b>: #${value["number"]}</p>
  76. <p><b>Name</b>: ${value["name"]}</p>
  77. <p><b>Type</b>: ${typeFormatter(value["type"])}</p>
  78. <p><b>Weakness</b>: ${typeFormatter(value["weakness"])}</p>
  79. <p><b>Abilities</b>: ${value["abilities"]}</p>
  80. <p><b>Weight</b>: ${value["weight"]}</p>
  81. <p><b>Height</b>: ${value["height"]}</p>
  82. <p><b>Link</b>: <a href="https://pokemon.com${value["detailPageURL"]}">https://pokemon.com${value["detailPageURL"]}</a>
  83. `;
  84. })
  85. .catch((err) => {
  86. document.querySelector(".info").innerHTML = `
  87. <div>
  88. <p>Could not find this 😟. Try again.</p>
  89. </div>`;
  90. console.log(err);
  91. });