Advertisement
jargon

Roe2Js :: loadJSON ( )

Jun 25th, 2024
578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Function to load JSON
  2. function loadJSON(url) {
  3.  
  4.     if(basename(url) === `[object Promise].json`){
  5.         return false;
  6.     }
  7.    
  8.     // Ensure the URL starts with the host
  9.     if ( !url.startsWith( host ) ) {
  10.         url = `${host}${url}`;
  11.     }
  12.            
  13.     // Replace spaces with %20
  14.     url = url.replace(/ /g, '%20');
  15.    
  16.     let originalRequest = ``;
  17.    
  18.     let addy = ``;
  19.    
  20.     if(instr(url,`//`)){
  21.         addy = url.split(`//`)[1];
  22.     }else{
  23.         addy = url;
  24.     }
  25.    
  26.     addy = addy.split(`/`);
  27.     let addy2 = ``;
  28.     for(index = 1; index < addy.length - 1; index++){
  29.         if(addy[index]!==``){
  30.             addy2 += `/${addy[index]}`;
  31.         }
  32.     }
  33.    
  34.     addy3 = addy2.split(`/`);
  35.    
  36.     let nestRequest = ``;
  37.    
  38.     for(index = 2; index <= 2; index++){
  39.         nestRequest += `/${addy3[index]}`;
  40.     }
  41.  
  42.     for(index = 3; index < addy.length; index++){
  43.         originalRequest += `/${addy[index]}`;
  44.     }
  45.     let requestPath=`${addy[0]}`;
  46.  
  47.     // Fetch the JSON file
  48.     return fetch(url)
  49.         .then(response => {
  50.             if (!response.ok) {
  51.                 throw new Error(`Oh, Noez!! It was a bad network response\n\nunder "${nestRequest}" in "${originalRequest}" (${response.statusText})\n\n`);
  52.             }
  53.             return response.json();
  54.         })
  55.         .then(data => {
  56.             console.log(`Woot!! Data fetched!\n\nunder "${nestRequest}" in "${originalRequest}"\n\n`, data);
  57.             return data;
  58.         })
  59.         .catch(error => {
  60.             console.error(`Oh, Noez!! There has been a problem with your fetch operation!\n\nunder "${nestRequest}" in "${originalRequest}"\n\n`, error);
  61.         });
  62. }
  63.  
  64. function fileExists(url) {
  65.  
  66.     // Ensure the URL starts with the host
  67.     if ( !url.startsWith( host ) ) {
  68.         url = `${host}${url}`;
  69.     }
  70.            
  71.     // Replace spaces with %20
  72.     url = url.replace(/ /g, '%20');
  73.  
  74.     try {
  75.         const response = fetch(url, { method: 'HEAD' });
  76.         return response.status === 200;
  77.     } catch (error) {
  78.         console.error(`Oh, Noez!! Error fetching the file!\n\n"${url}"`, error);
  79.         return false;
  80.     }
  81. }
  82.  
  83. function basename(path) {
  84.   return path.split('/').pop().split('\\').pop();
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement