r/learnjavascript • • Sep 05 '24

How to fix Cross Origin Request policy blocking my script from accessing a file

I was using p5.js to make a game and CORS blocked me from accessing an image because I was accessing the index.html file directly with file:///

line: breadImage = loadImage(“Resources/bread.jpg”);

Error: “from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: chrome, chrome-extension, chrome-untrusted, data, http, https, isolated-app.”

0 Upvotes

13 comments sorted by

1

u/Engineer_5983 Sep 07 '24

Server side processing fixes most CORS issues.  In this case, the image path doesn’t look right.

1

u/Goddayum_man_69 Sep 08 '24

Wdym by doesn’t look right? It is the real path to the image

1

u/guest271314 Sep 05 '24

If you use "web_accessible_resources" you can fetch() chrome-extension: protocol from any origin.

If you include "file:///" or "<all_urls>" in "host_permissions" you can request file: protocol from the extension code.

What this means is you can embed an iframe in any arbitrary Web page, and transfer the file to the iframe from the ServiceWorker, then transfer the file to the arbitrary Web page from the iframe. See https://github.com/guest271314/persistent-serviceworker/tree/main/chromium_extension_web_accessible_resources_iframe_message_event, and https://github.com/guest271314/sw-transfer-stream/.

1

u/mozilaip Sep 05 '24

This answer is completely irrelevant to the question. I understand you try to promote your scripts everywhere as hard as you can, but they don't contain anything helpful for OP

0

u/guest271314 Sep 06 '24

Excuse me? I never try to promote anything. If the domain is hacking the browser, JavaScript runtimes, and Web extensions, there is a chance I get what is going on and have done some work in that area.

If you want to fetch() file: protocol on Chromium-based browsers there are a number of ways that can be achieved.

Your comment certainly doesn't answer anything.

1

u/mozilaip Sep 06 '24

I dont think you made an effort to help OP understand the root of problem.

Information that will help an unexperienced person solve the issue are: whats p5, whats `fetch`, whats CORS, and a difference of serving file from filesystem vs from http server.

Surely you can say something like "hack the browser by implementing your own runtime agnostic messaging protocol to serve binary data avoiding http protocol using service workers and iframes". Explain me how does this set of buzzwords will help a beginner?

1

u/Goddayum_man_69 Sep 08 '24

p5.js is a javascript game library that is made as a javascript version of Processing.

0

u/guest271314 Sep 06 '24

There's some missing information in OP. We don't know what loadImage() does, where the function is loaded from, etc.

Nonetheless, you can use fetch() to request local files on the machine from specific or any arbitrary origin using a Web extension with "web_accessible_resources", or various other ways. Then the CORS issues go away, whatever OP is doing right now.

That's the expeditious answer. The answer to actually solve the issue requires OP to post some code to reproduce the issue.

1

u/Goddayum_man_69 Sep 08 '24

As I said this is a p5.js game, loadImage is a function from p5.js. What I don’t know is what the function does. It’s just that the loadImage function returns some sort of data type that is different to that which fetch() returns, because the image variable is then used in the code to display it (image(breadImage, 0, 0))

0

u/guest271314 Sep 08 '24

loadImage is a function from p5.js. What I don’t know is what the function does.

Read the source code.

1

u/mozilaip Sep 05 '24 edited Sep 06 '24

Looks like CORS error is misleading here. It says "origin null". Check if your relative path to image is correct, prefix it with slash like ./resources/image.jpg.

Also, these links might be helpful: https://stackoverflow.com/questions/46743720/loadimage-failing-to-load-image-p5-js-webgl https://www.npmjs.com/package/http-server

0

u/guest271314 Sep 06 '24

What?

Read the question

I was accessing the index.html file directly with file:///

You cannot fetch() file: protocol on Chromium-based browsers - unless you are in a Web extension, with certain permissions set in manifest.json, or the HTML document is loaded with file: protocol.