I'm trying to use window.fetch()
in a Thunderbird extension I'm working on. Specifically, I want to try fetching the contents of files using relative paths - but am failing, possibly because I'm not using the right relative path.
So, the scenario is that the extension provides a background.html
; that include a script file, background.js
; that script invokes the "WebListener API", an extension loader of sorts; the WL API code registers a JS file's URI I give it in a property of a window object; then, finally, when that window loads, Services.scriptloader.loadSubScript()
is called with my JS file's URI; and this invokes my actual code, where I call window.fetch()
. Yes, I realize that is rather contorted and weird, but that's just the way it is these days :-(
Anyway, the code I'm calling is something like the following:
window.fetch(uri) .then((response) => { if (!response.ok) { throw new Error(`thrown response status: ${response.status}`); } return response.text(); }) .then((text) => doStuff(text) ) .catch((err) => console.error(`fetch failed: ${err}`));
This works (mostly-works, anyway) if I specify URIs such as chrome://bidimailui/content/subdir/somefile.xhtml
- and bundle somefile.xhtml
in the appropriate location within the XPI. But if I try specifying a relative path - relative to the location of background.html
, or background.js
, or the script where the code snippt above is located, or with some parts of the path prefixed - none of those options work. I get an opaque "Network error", in the .catch()
block.
My question: Is this relative-path fetching even supposed to succeed? And if it is - how do I determine the correct relative path for the file I'm trying to fetch? i.e. what would it be relative to?
"web_accessible_resources"
, or other means. The question for me is, isfetch()
necessary to get the data you want? Can't other means be used?fetch()
usage? You can serilize the content of the file to JSON then justimport
orimport()
the data as JSON, deserilize to whatever format you want. I'm not certain what the road block is? On Chromium you have to explicityly setfile:///*
inmanifest.json
to be able tofetch()
file:
protocol, e.g.,"host_permissions":[<all_urls>","file:///*"]
, then you can dofetch("file:///home/user/bin/qjs")
or whatever.new TextEncoder().encode(data)
, slap that in your file andimport * as json with {type:"json"}
then useTextDecoder()
. Sounds like you are having an issue weith URI's. You are creating URI's that aren't there. Don't know about Firefox, though WICG Import Maps are implemented in Chromium, to handle that case. If all else fails, since you are in extension world already, there's always Native Messaging to do whatever you want using native scripts and programs. Since you have "WindowListener" thing, I would first just open the window and usepostMessage()