Discord
Login
Community
DARK THEME

Does fetch not work in the index.html file?

I'm trying to get my json files from my assets folder into the resource variable automatically instead of inputting them manually, but I keep getting error messages like:
TypeError: Failed to fetch
My code:


var resources = {
  "images": [{"file":"icon.png","version":0,"size":0,"properties":{}}],
  "assets": [],
  "maps": {},
  "sounds": [],
  "music": []
};
fetch('assets/')
.then(response => response.text())
.then(data => {
    const parser = new DOMParser();
    const doc = parser.parseFromString(data, 'text/html');
    const fileList = Array.from(doc.querySelectorAll('a'))
        .map(a => a.textContent)
        .filter(name => name.endsWith('.json'))
    resources.assets = fileList;
});
I also tried xhr and stuff but nothing would work.

Ctrl - Alt - I - see what error message you have.

Why don't you load assets in the program using asset_manager ?

Because doesn't that load stuff from the resources variable I want to have it automatically scan the static/assets folder and add those to the resource variable. I am using the asset_manager inside the regular code btw.

Access to fetch at 'file:///C:/' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, isolated-app, chrome-extension, chrome, https, chrome-untrusted.
index.html:1 Access to internal resource at 'file:///C:/Users/......./manifest.json' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, isolated-app, chrome-extension, chrome, https, chrome-untrusted.

https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors/CORSRequestNotHttp?utm_source=devtools&utm_medium=firefox-cors-errors&utm_campaign=default

I have such errors (Chrome and Firefox)

Both indicate that the browser has blocked the request to the resources.

You can

  • modify the flags in the browser settings. But this will work on all websites and is dangerous.

  • set up a local server that will share this folder and you will run index.html as localhost/index.html

  • build an application for windows / mac (export your application to .exe) and see if Elektron allows you to download data locally.

I once tried to force the browser to run *.txt files (which contained javascript code) as a .js file but I also received a security policy error and it didn't work.

https://juplo.de/bypassing-the-same-origin-policiy-for-loal-files-during-development/

https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community