Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving data to a local file using only JavaScript

The set-up in question:

I have a stand alone, offline, kiosk mode instance of Chrome running on a Windows machine. I have full access to the system and any admin rights. I can start Chrome with any flags set or unset.

The task:

I have been asked to create a log file which tracks user activity within the offline app I am coding. It's a simple form of analytics which will append each event to the end of the file separated with a comma. The file will then be sent to a server once a day via a scheduled task. (None of this is my idea so please don't troll me)

Ruled out: Any server side code - I have lobbied for Node, PHP etc but as this will be distributed to many different installations so we cannot guarantee this will be installed. Flash/ActiveX/Java - ideally would like to avoid this (even though these will be installed by default)

Possible solutions: File API - I have looked at this but AFAIK if opens dialogue boxes to save the data to each file and this needs to happen in the background. Security - I have read in other SO Questions that this can be achieved if the security settings are reduced but no-one goes on to explain which ones. Is there a simple flag which allows it? How to enable local file system read write access from Google chrome? - similar question!

Ideal result: (something akin to PHP)

$file = 'log.txt';
$current = file_get_contents($file);
$current .= ",clicked:link";
file_put_contents($file, $current);

Possible ideal side result: proving this isn't possible and forcing PHP/Node/Java to be used ;)

In reply to those suggesting local storage : I'm not storing unique key/value pairs and that is very much like setting a cookie. Similarily there are file size limits. To those suggesting web SQL such as SQLite in chrome - there are file size limits if it's not a chrome extension. The only way I see that working is if I were to find the location of the file in the windows directory (C:\Users\%USERNAME%\AppData\Local\Google\Chrome\User Data\Default\databases) and upload that from the schedules task. Perfectly feasible but it is not a desirable answer.

like image 690
sidonaldson Avatar asked Sep 09 '26 12:09

sidonaldson


2 Answers

You could use HTML5?

http://diveintohtml5.info/storage.html

var foo = localStorage.getItem("bar");
// ...
localStorage.setItem("bar", foo);
like image 82
Ryan Avatar answered Sep 11 '26 03:09

Ryan


You can use the Chrome Apps File API, you will need to grant access to the file via a user action once, but after that you can get access the file again by using restoreEntry

like image 44
Kinlan Avatar answered Sep 11 '26 02:09

Kinlan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!