Reading/Writing JSON Files (locally)

929 views
Skip to first unread message

Vexen Crabtree

unread,
Oct 18, 2017, 12:25:54 PM10/18/17
to Chromium-discuss
Hiya... not a day goes by where someone isn't trying to do this, but I'm trying to write edits to .json files from a local web page. I can read files fine, and parse the data. It's for a local music player. But I can't edit and save any of the meta-data, because of the security restrictions that stop webbrowsers writing to files. The .json files are synced over OneDrive.

Help me pick options to overcome this inability to save the data:

(1) Use a Chrome Extension? Discussion forums say that these won't actually solve the problem; data will still be stuck in local.storage.

(2) On the page, have a "SaveChanges" button that exports all the data into a .json file that the user can save via the "Download... Save As" type dialogue box. Until the user does so, changes are only kept in local.storage.
Advantage: Quite easy to program.
Disadvantage: Pretty clunky for the user, and, will lead to broken version control; as soon as edits are made on 2 different machines, one's .json file will just have to override the other's. Rubbish.

(3) Write a .bat file or powerShell file to extract SQLLite3 data from Chrome local.storage temp file? The data is quite readable in that file, so I could write a script that every 30 minutes (or so) takes data-edits out of local.storage and merges them into the .json files on OneDrive. This will be pretty smooth for the user.

(4) Just start again and don't use webtech..., write the damn thing in a compilable language like Java (ick) or something else.

(5) SOMEHOW get Chrome to save local.storage data in a way that I can sync just this one app's data on OneDrive. But how?

I think only (3) and (4) are feasible.

For a long time this music player (Moggy) has been based on MS Access, which was a fine and proven solution... but I'm trying to break away from VBA because *all the reasons*.

--
Vexen Crabtree

PhistucK

unread,
Oct 18, 2017, 12:30:14 PM10/18/17
to ve...@vexen.co.uk, Chromium-discuss
Are you the author of the music player?
If so, if you already synchronize the JSON file to OneDrive, just take it from there every x minutes?

Otherwise -
The web cannot write files arbitrarily.
You can use an extension and native messaging, but that requires a bit of coding in a compilable language, so back to 4.
3 is a bad idea, because that file can change its structure and its format at any point. Never rely on internal implementation details.

4 is probably your best shot.


PhistucK

--
--
Chromium Discussion mailing list: chromium...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-discuss

---
You received this message because you are subscribed to the Google Groups "Chromium-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-discuss+unsubscribe@chromium.org.

Vexen Crabtree

unread,
Oct 18, 2017, 8:07:58 PM10/18/17
to Chromium-discuss, ve...@vexen.co.uk
Thanks for the reply. Yes, I'm the author. And although I do sync the files, there's no point re-reading them every x minutes, because, the files never change, because the browser can't update them as changes occur.

I actually tried (2) for a while yesterday; using JSON.stringify and just manually selecting the original .json file and overwriting it. As soon as changes are made a big "SAVE...." button appears on the player. Clunky, but it's only for me and the family so it doesn't really matter.... except.... the process is sooooooo slow. And only have the data is in the .json files so far so it'll only get slower. I started to plan delta-files so only portions would need to be exported... but....

I then went the same as you think.... #4. I'm just writing a good ol' fashioned executable app, in C#.

--
V


PhistucK

To unsubscribe from this group and stop receiving emails from it, send an email to chromium-discu...@chromium.org.

PhistucK

unread,
Oct 19, 2017, 3:09:53 AM10/19/17
to ve...@vexen.co.uk, Chromium-discuss
An extension (or even a web page) can update OneDrive, I believe.
Polling from the music player (or using the regular OneDrive application that automatically synchronizes the files) would make it get the latest changes.

Perhaps I do not understand your use case?


PhistucK

To unsubscribe from this group and stop receiving emails from it, send an email to chromium-discuss+unsubscribe@chromium.org.

Vexen Crabtree

unread,
Oct 19, 2017, 6:28:19 AM10/19/17
to Chromium-discuss, ve...@vexen.co.uk
Extensions (nor web pages) can write arbitrarily to local files. In this case, the .json files are in C:\Users\...\OneDrive\Documents\Moggy\db\ . The .html JavaScript can read the .json files in that dir, but, they can't write to them.

I can very easily re-poll the files for updates (that was the plan!). But because the .html app can't write to the .json files, then, they are never updated with any information. So I've not yet coded for picking up the updates - no point unless I find a way of writing the data (hence, options #1 - #4 in this thread....).



PhistucK

To unsubscribe from this group and stop receiving emails from it, send an email to chromium-discu...@chromium.org.

PhistucK

unread,
Oct 19, 2017, 6:44:20 AM10/19/17
to Vexen Crabtree, Chromium-discuss
Comments inline.


PhistucK

On Thu, Oct 19, 2017 at 1:28 PM, Vexen Crabtree <ve...@vexen.co.uk> wrote:
Extensions (nor web pages) can write arbitrarily to local files.

​You mean, "cannot", right?​

In this case, the .json files are in C:\Users\...\OneDrive\Documents\Moggy\db\ . The .html JavaScript can read the .json files in that dir, but, they can't write to them.

I can very easily re-poll the files for updates (that was the plan!). But because the .html app can't write to the .json files, then, they are never updated with any information. So I've not yet coded for picking up the updates - no point unless I find a way of writing the data (hence, options #1 - #4 in this thread....).

I meant, re-poll from OneDrive, not from the file system. The webpage/extension will write to OneDrive and the player will poll from OneDrive.​
Or the OneDrive synchronization application is installed and the player will poll from the file system (after the webpage/extension will write to OneDrive).

 
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-discuss+unsubscribe@chromium.org.

Vexen Crabtree

unread,
Oct 20, 2017, 4:32:08 AM10/20/17
to Chromium-discuss, ve...@vexen.co.uk
On Thursday, 19 October 2017 11:44:20 UTC+1, PhistucK wrote:
Comments inline.

Ditto.
 
PhistucK

On Thu, Oct 19, 2017 at 1:28 PM, Vexen Crabtree <ve...@vexen.co.uk> wrote:
Extensions (nor web pages) can write arbitrarily to local files.

​You mean, "cannot", right?​

Oops, I certainly did mean "cannot". :-/

 <snip>
I meant, re-poll from OneDrive, not from the file system. The webpage/extension will write to OneDrive and the player will poll from OneDrive.​
Or the OneDrive synchronization application is installed and the player will poll from the file system (after the webpage/extension will write to OneDrive).

Ahhhhhhh ... the webpage IS the app and IS the music player. There's no other software involved.

My wife is out today; I've got all day to carry on with the c# replacement. I done it as a chrome-specific solution to start off with simply because I like .js and I knew I could make a great front-end to the player if I could lay it out with HTML/CSS/JQuery.

--
Vexen Crabtree
 



PhistucK

Chromium Discussion mailing list: chromium...@chromium.org

PhistucK

unread,
Oct 23, 2017, 12:22:39 PM10/23/17
to Vexen Crabtree, Chromium-discuss
Oh, so why do you even need a local file?
Why not just work directly with OneDrive using its JavaScript API?


PhistucK

--
--
Chromium Discussion mailing list: chromium...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-discuss

---
You received this message because you are subscribed to the Google Groups "Chromium-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-discuss+unsubscribe@chromium.org.

Vexen Crabtree

unread,
Oct 23, 2017, 4:30:11 PM10/23/17
to Chromium-discuss, ve...@vexen.co.uk
On Monday, 23 October 2017 17:22:39 UTC+1, PhistucK wrote:
Oh, so why do you even need a local file?
Why not just work directly with OneDrive using its JavaScript API?

Because I typically spend long periods (weeks, sometimes months) offline. If I got the .html page writing to local files, the idea was it would write a load of update1.json , update2.json files, and when the machine gets online, OneDrive would sync them with other devices of mine (and my misses), and that even if they then went offline again without the app being opened, when opened, it would find all the update.json files, merge them into its localStorage, manage dates&versions&conflicts, and all would be peachy.

Unless I've missed a trick, and, there IS a local OneDrive API that will let Chrome write files locally, to something like ~currentUserOneDrive/music/db/ , callable from JavaScript.

function writeUpdates(){
    OneDriveAPICallThatRunsOnLocalMachine ( pContents, pToFilenameWithinLocalOneDriveDir, success: function () {....}) ;
};


PhistucK

To unsubscribe from this group and stop receiving emails from it, send an email to chromium-discu...@chromium.org.

PhistucK

unread,
Oct 23, 2017, 4:37:00 PM10/23/17
to Vexen Crabtree, Chromium-discuss
I do not know about OneDrive, but you can detect being online (as long as the browser is open, of open in the background) by either using the online event. Either that, or use background sync one-shot.


PhistucK

To unsubscribe from this group and stop receiving emails from it, send an email to chromium-discuss+unsubscribe@chromium.org.

Reply all
Reply to author
Forward
0 new messages