Im not able to parse local json files (chrome extension)

996 Views Asked by At

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

1

There are 1 best solutions below

0
LoneExile On

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>