How to "fetch" local files manifest v3?

6,975 views
Skip to first unread message

Rory

unread,
Mar 7, 2021, 12:23:19 AM3/7/21
to Chromium Extensions
Hey guys,

I'm trying to migrate an extension from manifest V2 to V3 and I've encountered an issue. In my extension I am loading a local file by listing the filename in the "web_accessible_resources" section of the manifest, then "fetch"-ing that resource after getting it's URL with "chrome.runtime.getURL". 

If we pretend the file is called "data.txt" then the problematic section of code looks something like this:

data = fetch(chrome.runtime.getURL("data.txt")).then(response => response.text())

However, I am no longer able to use "fetch" to retrieve my local file (I think because "fetch" has been limited to prevent loading remote-code). Any ideas how I can circumvent this issue in manifest V3?

Thanks,
Rory

Jackie Han

unread,
Mar 7, 2021, 3:49:20 AM3/7/21
to Rory, Chromium Extensions
Fetching local files works for me in MV3. I don't have the problem you said.

Jackie Han

Rory <rorygar...@gmail.com> 于2021年3月7日周日 下午1:23写道:
--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/2a1c224d-b070-4b61-a6eb-cf4e4f60ebbcn%40chromium.org.

Deco

unread,
Mar 7, 2021, 7:11:45 AM3/7/21
to Jackie Han, Rory, Chromium Extensions
Have you tried using getURL? It shouldn't work in this scenario as the local file becomes an external resource, which is blocked in Manifest V3.

Thanks,
Deco

wOxxOm

unread,
Mar 7, 2021, 9:08:46 AM3/7/21
to Chromium Extensions, Rory
MV3 uses a new format for web_accessible_resources so maybe yours is incorrect?
See https://developer.chrome.com/docs/extensions/mv3/intro/mv3-migration/#web-accessible-resources

"web_accessible_resources": [{
  "resources": ["data.txt"],
  "matches": []
}]
  

guest271314

unread,
Mar 7, 2021, 9:14:12 AM3/7/21
to Chromium Extensions, decklin...@gmail.com, Rory, Chromium Extensions, Jackie Han
When you chain catch(console.error) to fetch() what is logged at console?

I have not tried in some time, though chrome: protocol might not be supported by Fetch Standard. 

You can get the file using File System Access API (Native File System).

guest271314

unread,
Mar 7, 2021, 9:22:16 AM3/7/21
to Chromium Extensions, guest271314, decklin...@gmail.com, Rory, Chromium Extensions, Jackie Han
console.log(await (await (await showOpenFilePicker())[0].getFile()).text())

wOxxOm

unread,
Mar 7, 2021, 11:25:26 AM3/7/21
to Chromium Extensions, guest...@gmail.com, decklin...@gmail.com, Rory, Chromium Extensions, Jackie Han
> chrome: protocol might not be supported by Fetch Standard. 

It's not chrome, it's chrome-extension:// and it's intentionally allowed for content scripts, this is how web_accessible_resources is implemented.


> You can get the file using File System Access API (Native File System).
> console.log(await (await (await showOpenFilePicker())[0].getFile()).text())

Don't be confused by the OP's usage of the word "local". Their code clearly indicated the subject is a file from an extension package declared via web_accessible_resources.

Jackie Han

unread,
Mar 7, 2021, 11:45:59 AM3/7/21
to Deco, Rory, Chromium Extensions
Deco,

Yes, I tested it in MV3. I use fetch api from an extension page to fetch another file in this extension. See below screenshot:
Screen Shot 2021-03-08 at 12.39.31 AM.png

If you use it in content script, see wOxxOm's answer, it has a new format and restriction for "web_accessible_resources".


Deco <decklin...@gmail.com> 于2021年3月7日周日 下午8:11写道:

Deco

unread,
Mar 7, 2021, 11:49:05 AM3/7/21
to Jackie Han, Rory, Chromium Extensions
Ah thanks, it was my error of judgement from reading the OP's post, I assumed it was an externally loaded asset as I missed the use of web_accessible_resources.

Regards,
Deco

Rory

unread,
Mar 7, 2021, 2:10:45 PM3/7/21
to Chromium Extensions, decklin...@gmail.com, Rory, Chromium Extensions, Jackie Han
Thanks everyone.

It's an html file not a txt file by the way, not sure if that matters, I just used text in the example. Maybe this is why.

Everything I've tried in here has not worked - it keeps saying:

Denying load of chrome-extension://hgipopnedpcknmapfakdedlnjjkmpnao/widget.html. Resources must be listed in the web_accessible_resources manifest key in order to be loaded by pages outside the extension.

I'm using this line in the content script:
await fetch(chrome.runtime.getURL("widget.html"))

and in the manifest it's declared to MV3 specifications (screenshot attached).



I must be missing something small. Is it because it's html? This hasn't been a problem up until now.

Thank you for your assistance on this.
Screen Shot 2021-03-07 at 11.09.24 AM.png
Screen Shot 2021-03-07 at 10.51.47 AM.png

Rory

unread,
Mar 7, 2021, 2:20:58 PM3/7/21
to Chromium Extensions, Rory, decklin...@gmail.com, Chromium Extensions, Jackie Han
Worth noting - it IS in the same folder as the manifest. So, that file path in the manifest should be fine.

wOxxOm

unread,
Mar 7, 2021, 2:21:13 PM3/7/21
to Chromium Extensions, Rory, decklin...@gmail.com, Chromium Extensions, Jackie Han
Looks like "matches" in web_accessible_resources is actually required now so it should look like "matches": ["<all_urls>"] in case you expose it on all URLs.

Rory

unread,
Mar 7, 2021, 2:23:18 PM3/7/21
to Chromium Extensions, wOxxOm, Rory, decklin...@gmail.com, Chromium Extensions, Jackie Han
Thanks w0xx0m, this fixed it.

Ravish Desai

unread,
Mar 8, 2021, 2:08:39 AM3/8/21
to Chromium Extensions, Rory, wOxxOm, decklin...@gmail.com, Chromium Extensions, Jackie Han
Hello world, i want to develop an extension to extract the information from a page and display it into side bar in chrome extension, can you tell from where shall I start?
Reply all
Reply to author
Forward
0 new messages