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

saveLocal.js 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. try {
  2. localStorage.setItem("RENGLONES", loadFile("https://artesaniaspr.website/renglones2.json"));
  3. localStorage.setItem("AZAR", loadFile("https://artesaniaspr.website/azar.json"));
  4. } catch {
  5. console.log("Files not found")
  6. }
  7. let renglones = JSON.parse(window.localStorage.getItem("RENGLONES"));
  8. //A synchronous request to the server
  9. function loadFile(filePath) {
  10. var result = null;
  11. var xmlhttp = new XMLHttpRequest();
  12. xmlhttp.open("GET", filePath, false);
  13. xmlhttp.send();
  14. if (xmlhttp.status==200) {
  15. result = xmlhttp.responseText;
  16. }
  17. return result;
  18. }
  19. //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)
  20. function checkDate() {
  21. var date = loadFile("https://artesaniaspr.website/date.txt");
  22. if (localStorage.getItem("DATE") == null || localStorage.getItem("DATE") != date || localStorage.length < 18) {
  23. localStorage.setItem("DATE", date);
  24. return false;
  25. }
  26. else {
  27. return true;
  28. }
  29. }
  30. try {
  31. if (checkDate() == false) {
  32. window.localStorage.setItem("TODOS", loadFile("https://artesaniaspr.website/artesanias2.json"));
  33. //For each name it finds in renglones, it requests the json in the server and saves them in local storage
  34. for (var i = 0; i < renglones.length; i++) {
  35. localStorage.setItem(renglones[i].Renglones, loadFile("https://artesaniaspr.website/" + renglones[i].Renglones + ".json"));
  36. }
  37. }
  38. } catch {
  39. console.log("error");
  40. }