Advertisement
jargon

Roe2Js :: loadJSONDirect ( )

Jun 25th, 2024
599
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.     console.log(`Requesting: "` + basename(url,`.json`) + `"`);
  5.  
  6.     if(basename(url,`.json`) === `[object Promise]`){
  7.         return false;
  8.     }
  9.  
  10.     if(basename(url,`.json`) === `undefined`){
  11.         return false;
  12.     }
  13.    
  14.     // Ensure the URL starts with the host
  15.     if ( !url.startsWith( host ) ) {
  16.         url = `${host}${url}`;
  17.     }
  18.                
  19.     let originalRequest = ``;
  20.    
  21.     let addy = ``;
  22.    
  23.     if(instr(url,`//`)){
  24.         addy = url.split(`//`)[1];
  25.     }else{
  26.         addy = url;
  27.     }
  28.    
  29.     addy = addy.split(`/`);
  30.     let addy2 = ``;
  31.     for(index = 1; index < addy.length - 1; index++){
  32.         if(addy[index]!==``){
  33.             addy2 += `/${addy[index]}`;
  34.         }
  35.     }
  36.    
  37.     addy3 = addy2.split(`/`);
  38.    
  39.     let nestRequest = ``;
  40.    
  41.     for(index = 2; index <= 2; index++){
  42.         nestRequest += `/${addy3[index]}`;
  43.     }
  44.  
  45.     for(index = 3; index < addy.length; index++){
  46.         originalRequest += `/${addy[index]}`;
  47.     }
  48.     let requestPath=`${addy[0]}`;
  49.  
  50.     // Replace spaces with %20
  51.     url = url.replace(/ /g, '%20');
  52.  
  53.     // Fetch the JSON file
  54.     return fetch(url)
  55.         .then(response => {
  56.             if (!response.ok) {
  57.                 throw new Error(`Oh, Noez!! It was a bad network response\n\nunder "${nestRequest}" in "${originalRequest}" (${response.statusText})\n\n`);
  58.             }
  59.             return response.json();
  60.         })
  61.         .then(data => {
  62.             console.log(`Woot!! Data fetched!\n\nunder "${nestRequest}" in "${originalRequest}"\n\n`, data);
  63.             return data;
  64.         })
  65.         .catch(error => {
  66.             console.error(`Oh, Noez!! There has been a problem with your fetch operation!\n\nunder "${nestRequest}" in "${originalRequest}"\n\n`, error);
  67.         });
  68. }
  69.  
  70. function fileExists(url) {
  71.  
  72.     // Ensure the URL starts with the host
  73.     if ( !url.startsWith( host ) ) {
  74.         url = `${host}${url}`;
  75.     }
  76.            
  77.     // Replace spaces with %20
  78.     url = url.replace(/ /g, '%20');
  79.  
  80.     try {
  81.         const response = fetch(url, { method: 'HEAD' });
  82.         return response.status === 200;
  83.     } catch (error) {
  84.         console.error(`Oh, Noez!! Error fetching the file!\n\n"${url}"`, error);
  85.         return false;
  86.     }
  87. }
  88.  
  89. function basename(path = ``, ext = ``) {
  90.    
  91.     let intermediate = path.split(`/`);
  92.    
  93.     intermediate = intermediate[intermediate.length - 1];
  94.    
  95.     if ( ext === right( intermediate, len( ext ) ) ) {
  96.         intermediate = left( intermediate, len( intermediate ) - len( ext ));
  97.     }
  98.    
  99.     return intermediate;
  100. }
  101.  
  102. function loadJSONDirect(url){
  103.     let intermediate = [loadJSON(url)][0];
  104.     console.log(intermediate);
  105.     return intermediate;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement