Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Function to load JSON
- function loadJSON(url) {
- if(basename(url) === `[object Promise].json`){
- return false;
- }
- // Ensure the URL starts with the host
- if ( !url.startsWith( host ) ) {
- url = `${host}${url}`;
- }
- // Replace spaces with %20
- url = url.replace(/ /g, '%20');
- 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]}`;
- // 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) {
- return path.split('/').pop().split('\\').pop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement