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

DanielMx
  • 1
  • 1
  • 1. open `chrome://extensions`, 2. click `details` of your extension, 3. enable "file access" – wOxxOm Nov 11 '22 at 20:32
  • already done. @wOxxOm but still not working – DanielMx Nov 11 '22 at 20:33
  • Also tried --allow-file-access-from-files --disable-web-security – DanielMx Nov 11 '22 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](https://stackoverflow.com/a/55292071). Also make sure your `permissions` in manifest.json include `file://*/` or ``. – wOxxOm Nov 11 '22 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. – DanielMx Nov 11 '22 at 22:58
  • Extensions can read any `file://` URL. – wOxxOm Nov 12 '22 at 06:46
  • @wOxxOm then please tell me how i can implement this, because mine dont work. – DanielMx Nov 12 '22 at 11:41
  • See my second comment. – wOxxOm Nov 12 '22 at 11:56

1 Answers1

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>

LoneExile
  • 151
  • 1
  • 5