Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Function to load JSON
- function loadJSON(url) {
- console.log(`Requesting: "` + basename(url,`.json`) + `"`);
- if(basename(url,`.json`) === `[object Promise]`){
- return false;
- }
- if(basename(url,`.json`) === `undefined`){
- return false;
- }
- // Ensure the URL starts with the host
- if ( !url.startsWith( host ) ) {
- url = `${host}${url}`;
- }
- let originalRequest = ``;
- let addy = ``;
- if(instr(url,`//`)){
- addy = url.split(`//`)[1];
- }else{
- addy = url;
- }
- addy = addy.split(`/`);
- let addy2 = ``;
- for(index = 1; index < addy.length - 1; index++){
- if(addy[index]!==``){
- addy2 += `/${addy[index]}`;
- }
- }
- addy3 = addy2.split(`/`);
- let nestRequest = ``;
- for(index = 2; index <= 2; index++){
- nestRequest += `/${addy3[index]}`;
- }
- for(index = 3; index < addy.length; index++){
- originalRequest += `/${addy[index]}`;
- }
- let requestPath=`${addy[0]}`;
- // Replace spaces with %20
- url = url.replace(/ /g, '%20');
- // Fetch the JSON file
- return fetch(url)
- .then(response => {
- if (!response.ok) {
- throw new Error(`Oh, Noez!! It was a bad network response\n\nunder "${nestRequest}" in "${originalRequest}" (${response.statusText})\n\n`);
- }
- return response.json();
- })
- .then(data => {
- console.log(`Woot!! Data fetched!\n\nunder "${nestRequest}" in "${originalRequest}"\n\n`, data);
- return data;
- })
- .catch(error => {
- console.error(`Oh, Noez!! There has been a problem with your fetch operation!\n\nunder "${nestRequest}" in "${originalRequest}"\n\n`, error);
- });
- }
- function fileExists(url) {
- // Ensure the URL starts with the host
- if ( !url.startsWith( host ) ) {
- url = `${host}${url}`;
- }
- // Replace spaces with %20
- url = url.replace(/ /g, '%20');
- try {
- const response = fetch(url, { method: 'HEAD' });
- return response.status === 200;
- } catch (error) {
- console.error(`Oh, Noez!! Error fetching the file!\n\n"${url}"`, error);
- return false;
- }
- }
- function basename(path = ``, ext = ``) {
- let intermediate = path.split(`/`);
- intermediate = intermediate[intermediate.length - 1];
- if ( ext === right( intermediate, len( ext ) ) ) {
- intermediate = left( intermediate, len( intermediate ) - len( ext ));
- }
- return intermediate;
- }
- function loadJSONDirect(url){
- let intermediate = [loadJSON(url)][0];
- console.log(intermediate);
- return intermediate;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement