Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

saveLocal.js 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. localStorage.setItem("RENGLONES", loadFile("https://artesaniaspr.website/renglones2.json"));
  2. localStorage.setItem("AZAR", loadFile("https://artesaniaspr.website/azar.json"));
  3. let renglones = JSON.parse(window.localStorage.getItem("RENGLONES"));
  4. //A synchronous request to the server
  5. function loadFile(filePath) {
  6. var result = null;
  7. var xmlhttp = new XMLHttpRequest();
  8. xmlhttp.open("GET", filePath, false);
  9. xmlhttp.send();
  10. if (xmlhttp.status==200) {
  11. result = xmlhttp.responseText;
  12. }
  13. return result;
  14. }
  15. //Checks if date has changed in the server or if number or renglones has been shortened (since local storage can be erased when the device runs out of memory)
  16. function checkDate() {
  17. var date = loadFile("https://artesaniaspr.website/date.txt");
  18. if (localStorage.getItem("DATE") == null || localStorage.getItem("DATE") != date || localStorage.length < 17) {
  19. localStorage.setItem("DATE", date);
  20. return false;
  21. }
  22. else {
  23. return true;
  24. }
  25. }
  26. try {
  27. if (checkDate() == false) {
  28. window.localStorage.setItem("TODOS", loadFile("https://artesaniaspr.website/artesanias2.json"));
  29. //For each name it finds in renglones, it requests the json in the server and saves them in local storage
  30. for (var i = 0; i < renglones.length; i++) {
  31. localStorage.setItem(renglones[i].Renglones, loadFile("https://artesaniaspr.website/" + renglones[i].Renglones + ".json"));
  32. }
  33. }
  34. } catch {
  35. console.log("error");
  36. }