Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading and writing files in chrome extensions

I am trying to write a chrome extension that initialize some parameters from a config file. I would like to allow the extension to change those parameters and save them to the file so that the next time the extension was loaded it uses the new configuration.

I have been reading the chrome.filesystem api but it needs the interaction of the user to choose the file. However in this case the process must be done automatically without any action of the user.

Since this configuration file will be only accessed by the extension it could be sand-boxed but It must be persistent even if chrome is closed.

I manage to read the file using an XMLHttpRequest but I could not find a way to modify the file.

Is it possible to do this from a chrome extension?

like image 495
Eduardo Avatar asked Jan 11 '13 20:01

Eduardo


1 Answers

This is an old question but unfortunately the only response it got was wrong.

It's definitely possible to read and write files using HTML5 in Chrome, without the vague "security" issues mentioned. The HTML5 Filesystem creates a protected sandbox in which you write and read virtual files: you can think of it as files being written in file based database managed by Chrome and not accessible by either other Chrome apps & extensions or other OS based applications. The user won't be able to copy or move these files using his OS file explorer since they reside inside the web browser's file DB.

You can't read (or write) arbitrary files from (to) disk based on any given file path. If you need a file from disk you can only let the user select it himself by using chrome.fileSystem.chooseEntry()

You can however read (and write) your own files from (to) the HTML5 Filesystem.

So to answer your question: no you don't need user interaction to write your config file to the browser's file system. An alternative to files could be chrome storage, localstorage or even indexedDB to store your (persisted) config key-value pairs.

Here are a couple of useful links to start reading about it:

Toying with the HTML5 filesystem

HTML5 Rocks

HTML5 demos

like image 141
user1661268 Avatar answered Oct 02 '22 13:10

user1661268