Since the switch from add-on to web extension i’m getting a bit lost, I can’t find a way to simply import a local .json file in my content script.
All I managed to do so far is to get the URL with `
var URL = browser.extension.getURL(“data/debrideurs.json”)
` but I just can’t find a way to use what is inside. This is probably a very simple thingto do, thank you for your time
There are at least three different approaches to this:
Put your JSON object in the browser.storage.local from a background sscript and read it in the content script.
Read any resource (including a JSON string) as a Blob in the beckground script and bessage it’s URL to the content:
/// in a background script with full access
const blob = await (await fetch(pathInPackage)).blob();
const tmpUri = URL.createObjectURL(blob);
/// in the restricted content script
const tmpUri = ; // get string from background via messaging
const json = await (await fetch(tmpUri)).json();
Add the file to the web_accessible_resources. I’d always strictly avoid this, as it potentially allows every website to read the file as well.
I have another question
I used the local.storage method, wich work perfectly with json already in the code. But I would like to download it via a URL of this type “example.com/.../myfile.json”
I tried using in my background script :