Get local file inside the extension folder in Chrome
Asked Answered
W

2

28

I know that I can't get a local file from within the extension directory. It is possible to get a file that is inside the extension directory itself?

Withdraw answered 4/10, 2011 at 7:59 Comment(0)
M
39

You can use chrome.runtime.getURL to get a fully-qualified URL to a resource.

// Outputs path to the file regardless if it exits
> chrome.runtime.getURL('assets/extension-icon.png');
"chrome-extension://kfcphocilcidmjolfgicbchdfjjlfkmh/assets/extension-icon.png"

The chrome-extension protocol plus the extension id, will be the address for the extension's root directory.

If you need something more powerful, you might also use HTML5's FileSystem API which can create, read, write and list files from a sandbox in the current user's local file system.

Mott answered 4/10, 2011 at 9:25 Comment(1)
chrome.extension.getURL is deprecated since Chrome 58. Use chrome.runtime.getURL.Ens
U
13

On Chrome 17 or later, for this to work you must include the web_accessible_resources section to allow an image packed within the extension to be injected into a web page. http://developer.chrome.com/extensions/manifest.html#web_accessible_resources

{...
"web_accessible_resources": [
"images/my-awesome-image1.png",
"images/my-amazing-icon1.png"
],...}

(courtesy of jhaury)

Untread answered 9/1, 2013 at 13:20 Comment(1)
I've done this but I still can't load my file via the chrome-extension://... format. It's a JSON file, it always 404s. Any ideas?Combine

© 2022 - 2024 — McMap. All rights reserved.