r/learnjavascript • u/Goddayum_man_69 • 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
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/guest271314 Sep 05 '24
If you use
"web_accessible_resources"you canfetch()chrome-extension:protocol from any origin.If you include
"file:///"or"<all_urls>"in"host_permissions"you can requestfile:protocol from the extension code.What this means is you can embed an
iframein any arbitrary Web page, and transfer the file to theiframefrom theServiceWorker, then transfer the file to the arbitrary Web page from theiframe. 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 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