Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function loadJSONSync(url) {
- url = fixURL(url);
- var xhr = new XMLHttpRequest();
- xhr.open('GET', url, false); // 'false' makes the request synchronous
- xhr.send(null);
- if (xhr.status === 200) {
- return JSON.parse(xhr.responseText);
- } else {
- // throw new Error('Failed to load JSON');
- return [];
- }
- }
- function loadJSONDirect (url) {
- url = fixURL ( url );
- if( !fileExists ( url ) ) {
- return [];
- }
- try {
- var data = loadJSONSync(url);
- // // console.log(data);
- } catch (error) {
- return [];
- // console.error(error);
- }
- // console.log ( basename(url), data );
- return data;
- }
- function fileExists ( url ) {
- url = fixURL( url );
- try {
- fetch ( url );
- } catch (error) {
- return false;
- }
- return true;
- }
- function basename(path = '', ext = '') {
- // Ensure path is a string
- path = path.toString();
- // Split the path by '/'
- let parts = path.split('/');
- // Get the last part of the path
- let base = parts[parts.length - 1];
- // If an extension is specified and the base ends with that extension
- if (ext && right(base, len(ext)) === ext) {
- // Remove the extension from the base
- base = left(base, len(base) - len(ext));
- }
- return base;
- }
- function fixURL ( url ) {
- // Ensure the URL starts with the host
- if ( !url.startsWith( host ) ) {
- url = `${host}${url}`;
- }
- // Replace spaces with %20
- url = url.replace(/ /g, '%20');
- return url;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement