Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get local file inside the extension folder in Chrome

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?

like image 933
Donovan Avatar asked Oct 04 '11 07:10

Donovan


People also ask

Can a Chrome extension access local files?

For security reasons, by default the Chrome browser does not allow extensions to access local files. If you want the accelerator to access local files (locations of "file:///...", instead of "http://" or "https://"), you must configure Chrome to allow the access.

Where are chrome extensions saved locally?

When extensions are installed into Chrome they are extracted into the C:\Users\[login_name]\AppData\Local\Google\Chrome\User Data\Default\Extensions folder. Each extension will be stored in its own folder named after the ID of the extension.


2 Answers

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.

like image 82
kbtz Avatar answered Sep 22 '22 07:09

kbtz


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)

like image 36
aviv Avatar answered Sep 24 '22 07:09

aviv