0

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?

7
  • I think you're gonna have to find somebody that is using that special case "WindowListener" API that is now deprecated. In an ordinary extension, yes, you can just fetch resources from within the extension; or from a different extension, using "web_accessible_resources", or other means. The question for me is, is fetch() necessary to get the data you want? Can't other means be used?CommentedApr 7 at 13:39
  • @guest271314: The data is currently in the form of a string literal (well, back-ticked string really) into my JS; I'm trying to figure out how to put it in a separate file, to make my code more generic. But then I need the file contents as a string, to work with.
    – einpoklum
    CommentedApr 7 at 13:57
  • What is the issue? Just fetch() usage? You can serilize the content of the file to JSON then just import or import() 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 set file:///* in manifest.json to be able to fetch()file: protocol, e.g., "host_permissions":[<all_urls>","file:///*"], then you can do fetch("file:///home/user/bin/qjs") or whatever.CommentedApr 7 at 14:00
  • @guest271314: This is not chrome, and I'm on manifest v2; but even ignoring the question of permissions - can I import plain text?
    – einpoklum
    CommentedApr 7 at 14:19
  • You can import JSON. Just do new TextEncoder().encode(data), slap that in your file and import * as json with {type:"json"} then use TextDecoder(). 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 use postMessage()CommentedApr 7 at 14:21

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.