I have tried different ways to load a config.json file into a chrome extension, but none of them seems to work.
Tried using XHR: the response is empty E.g:
var xhr = new XMLHttpRequest;
xhr.open("GET", chrome.runtime.getURL("config.json"));
xhr.onreadystatechange = function() {
if (this.readyState == 4) {
console.log("request finished")
console.log(xhr.responseText) // Returns empty
}
};
xhr.send();
Tried using file and didn't work while granting the web_accessible_resources to that file in the manifest file.
const url = chrome.runtime.getURL('config.json');
fetch(url)
.then((response) => {response.json()}) //assuming file contains json
.then((json) => doSomething(json));
Tried using this solution:
chrome.runtime.getPackageDirectoryEntry(function(root) {
root.getFile("config.json", {}, function(fileEntry) {
fileEntry.file(function(file) {
var reader = new FileReader();
reader.onloadend = function(e) {
var myFile = JSON.parse(this.result);
/*do here whatever with your JS object(s)*/
};
reader.readAsText(file);
});
});
});
I get getPackageDirectoryEntry undefined
The only solution that works is obviously to include the json as an object into the javascript contentScript file but that is against code modularity.
Anyone with solution ?
Edit: This is my part of the manifest regarding the web_accessible_resources
"web_accessible_resources": [
{
"resources": [ "config.js"],
"matches": [ "https://mywebsite/*" ]
}
]