Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I preserve FileSystem API access to a directory between app launches?

I'm trying to port my existing SPA web app to Electron in order to make it a desktop app, working fully offline from executable. This app is meant to store user's data in file system (in a directory chosen by user) and I use FileSystem API to access it.

I want user to choose target directory once and after he chooses one, directory should be opened automatically at app startup.

In order to do this I save directory handle in IndexedDB, the same way like it's shown here Storing file handles or directory handles in IndexedDB, I queryPermission and when it does not return "granted", I use requestPermission to restore it.

In the browser it seems to be working fine. There is an option "Allow on every visit" and when I check it, I can close the browser, enter my website again and my app automatically loads data from remembered directory just like I wanted. enter image description here

This doesn't seem to work the same way in Electron app. Every time I open it, it prevents me from accessing directory:

SecurityError: Failed to execute 'requestPermission' on 'FileSystemHandle': User activation is required to request permissions.

If permission cannot be acquired, my app shows a button that lets user try again and when he click this button, requestPermission is called again and this time it works: directory is being loaded (even without requiring user to choose directory again from filepicker).

So this shows that my app stores directory handle correctly and it correctly requests to regain access to it, but Electron requires manual user interaction (or: "user gesture") to do it.

My question is: Can I regain permission directory without user gesture in Electron? Is there some equivalent of "Allow on every visit" checkbox in Electron (which is missing now, as no address bar is visible in my app)?

like image 390
Piotrek Avatar asked Sep 02 '25 04:09

Piotrek


1 Answers

This is a known issue in Electron JS. The support for persistent permissions is not yet implemented in Electron. Prior to version 30, it was possible to bypass the permissions by enabling experimental features using the switch - enable-experimental-web-platform-features. But now it is not possible.

So, at the moment you have two options:

  1. Use Electron version 29 or earlier and set the flag enable-experimental-web-platform-features.
  2. Trick the user to perform a gesture, such as displaying a simple startup message that they must click to close.
like image 75
Raghavendra N Avatar answered Sep 05 '25 01:09

Raghavendra N