0

When I try to load URLs it works without problems, but unfortunately not when I specify the local path can someone help me here?

function fileurl_read() { // save the url for the next time var storage_fileurl = input_fileurl_value.value localStorage.setItem(localstorage_key_data_name, storage_fileurl); if (storage_fileurl (storage_fileurl)) { getJSON(storage_fileurl, function(err, data) { document.getElementById('textarea_json').value = err ? JSON.stringify(err) : JSON.stringify(data, null, 2); if (!err && data) { chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) { fill_form_now(tabs[0]); }); } } ); } else if (storage_fileurl) { alert('Invalid URL'); } } 
/* File url read */ var input_fileurl_value = document.getElementById('input_fileurl'); let button_fill_form_from_url = document.getElementById('button_fill_form_from_url'); button_fill_form_from_url.onclick = function() { fileurl_read(); } 
function getJSON(url, callback) { var xhr = new XMLHttpRequest(); xhr.open('GET', url, true); xhr.responseType = 'json'; xhr.onload = function() { var status = xhr.status; if (status === 200) { callback(null, xhr.response); } else { callback(status, xhr.response); } }; xhr.send(); }; 

This one is /* */ out.

function isValidHttpUrl(string) { let url; try { url = new URL(string); } catch (_) { return false; } return url.protocol === "http:" || url.protocol === "https:"; } 

Thanks for the future help.

I tried to load a path instead of an url Example: file:///C:/Users/Demo/Desktop/Doener/data/Test.json

8
  • 1. open chrome://extensions, 2. click details of your extension, 3. enable "file access" Commented Nov 11, 2022 at 20:32
  • already done. @wOxxOm but still not working Commented Nov 11, 2022 at 20:33
  • Also tried --allow-file-access-from-files --disable-web-security Commented Nov 11, 2022 at 21:37
  • If this is a content script it won't work, you'll have to fetch the file in the background script, see example. Also make sure your permissions in manifest.json include file://*/ or <all_urls>. Commented Nov 11, 2022 at 22:44
  • I just found out Chrome can only access extension files themselves or the ones from the subfolder, but I don't know how to access them and Google isn't really helping me out any solutions based on my code? I would then put them under Jsons in the extension folder. Commented Nov 11, 2022 at 22:58

1 Answer 1

0

You can use Web Accessible Resources

Web-accessible resources are files inside an extension that can be accessed by web pages or other extensions. Extensions typically use this feature to expose images or other assets that need to be loaded in web pages, but any asset included in an extension's bundle can be made web accessible.

access your file by chrome-extension://<extension-id>/<your file name>

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.