-
-
Notifications
You must be signed in to change notification settings - Fork 144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use the "File System Access API" to be able to "remember" last ZIM file #656
Comments
It works if the site is part of the Origin Trial (I did sign up for that for kiwix.github.io and have provided some feedback to Google). See the implementation in Kiwix JS Windows here (also some screenshots): I discuss some limitations in that issue, but generally it has a native feel (a few more prompts than native). It works best in a PWA when the user installs the PWA, otherwise the user would need to visit the website each time, while a PWA is an offline version of that. |
Excellent! |
Hmm, I'm not sure (I never implemented an extension version of KJSW because I feel it's properly covered by Kiwix JS, and it would be confusing to have two different extensions built on essentially the same codebase). My understanding is that the API requires |
The Origin Trial token for kiwix.github.io seems to have finished or the token has expired (at least on Edge Chromium, for which it was working fine until very recently). :-( |
I've now updated KJSW to use the finalized version of the API. Quite a few things had changed from the trial. I can also confirm that for a completely fresh install of both Chrome and Edge in Windows sandbox, the Native Filesystem API is used out-of-the-box in the PWA without the user having set any flags. To test, visit: https://kiwix.github.io/kiwix-js-windows/ . If you have ever visited that version before, you will probably need to ensure the PWA Cache is updated to see the latest functional code. Easiest way is to open Dev Tools, go to Application -> Service Workers. Click Unregister next to the KJSW Service Worker (pwabuilder-sw.js). Then force reload (with Dev Tools still open), followed by normal reload. [I need to write a proper code updating routine, but the PWA is currently only used for testing experimental features.] |
Here a user feedback about this:
|
@kelson42: Yes, as @mossroy said at the head of this issue, it is a popular request. This can be achieved in two ways currently:
These both offer different APIs for accessing the File System. In the case of File System Access, a file handle is requested and this is serialized in IndexedDB. It can be accessed later, but it will ask the user for permission, so two simple clicks are required to re-open a file, but it is not necessary to re-pick the file. In Electron and NWJS, a path is added to the File object in JS, which allows the file to be accessed across sessions (with no further clicks). The File System Access API is only currently available in Chrome and Edge. As for NWJS and Electron, these are Chromium platforms too, but come with a complete copy of Chrome embedded, which means that the app is quite heavy compared to a PWA, so it makes sense more in the context of packaged apps (#549). |
However, all that is for future development in Kiwix JS. In the meantime, you can tell this user to try drag-and-drop in Kiwix JS. We enabled it quite a long time ago, and it could well achieve what the user wants right now. You can drag and drop into the app without Configure being open. It is a very fast way to switch between different ZIMs in a folder. |
@mossroy I noticed that at least in an Edge Extension (and therefore with 99% probability also in a Chrome Extension), the File System Access APIs appear to be available even under the This raises the possibility that we might support this API in the Chromium Extension, and also in the PWA (when/if accessed by a Chromium browser). Unfortunately, Firefox does not support the API (yet?). As I have experience using this API successfully with Kiwix JS Windows, it would be fairly easy to backport. However, I can think of one disadvantage: it creates a lack of parity between the Chromium Extension and the Mozilla Extension. Would you be in favour of backporting the API to Kiwix JS nevertheless? I would only want to do the work if there were consensus that it's OK to introduce a feature that is only fully supported by one browser type, of course. Of course the code falls back to the native file picker, so it wouldn't affect Firefox users negatively, only by comparison. API details: https://developer.mozilla.org/en-US/docs/Web/API/File_System_Access_API (There is no guarantee the API actually works in the |
I did not manage to test this feature using https://kiwix.github.io/kiwix-js-windows/ on Chromium 104 on Ubuntu 20.04 (with the official snap provided by Ubuntu). Even after installing it as a PWA. When I click on "select folder", then choose the directory where all my ZIM files are (and confirm that to Chromium), nothing happens (ZIM files are not listed). It triggers this error message in the console:
However, if I select another folder that contains a split zim file, it opens it properly. Also, if I try to reopen the PWA from the desktop icon (after setting it as executable), it opens a new empty Chromium window (just like if I started a new Chromium) instead of starting kiwix-js-windows. But maybe this comes from my Ubuntu/Chromium/Snap combination? I just tested on Ubuntu 22.04 (with the same Chromium version). On the web site, I manage to make it browse my ZIM directory, even if the UX can be confusing (the "select" button of the pop-up window is grayed if I go inside the directory: I need to select the directory while being in the parent directory). It remembers my previous zim files, and allows to reopen them with 2 clicks: one to choose the ZIM file, another one to confirm I allow Chromium to access it. This might have nothing to do with your implementation, BTW |
Thank you for testing on Linux! I'll try to reproduce the error you are seeing on WSL and get back to you. If you want to select only one File rather than a Directory, you should use the other button (Select File). For the Select Folder button, it will only let you select a Directory, not specific Files in the Directory. This is by design, because one button uses |
Running Ubuntu 20.04.4 LTS in WSL, I wasn't able to install the snap version of Chromium, because the snapd environment is (apparently) very difficult to install on WSL. I tried with a script that was supposed to enable it, but it prevented GUI apps from running in WSL, so I had to uninstall and reinstall Ubuntu 😟 (luckily this is just a few clicks in WSL!). So in the end I had to test in Chrome on Ubuntu. I realize that's not quite the same thing... But for what it's worth, below is a video of what the experience should be like on Chromium browsers -- this is just using pwa.kiwix.org, without installing the PWA (it's not needed to use any of the APIs). While picking the folder looks a bit convoluted, its' because I'm having to click through to the attached Windows drives where I keep my ZIM archives. Where it goes black in the middle is where I exited and relaunched Chrome to show how much quicker it then is to open an archive (two clicks). Once a directory is picked and remembered between sessions, it's a lot easier to open any archive in the directory. |
I forgot to say that the exception you see in your first screenshot above is normal and to be expected. Because the user hasn't given permission, the API throws (it's what it's supposed to do). The second exception from your second screenshot is not one I've been able to reproduce yet. EDIT: If you think it's worth backporting this, I'd be happy (very pleased!) to have your input on how the file/folder selection UI could be made less confusing. I also understand that there are a lot of other things for us to develop on Kiwix JS, so this may not be highest priority. |
Thanks for your feedback. |
It seems to come from the fact that the directory where my ZIM files are is in fact a symlink to a directory on another hard drive. |
I don't consider it noise. Testing is always useful! |
OK so I can test the API correctly now. |
"Select Storage" is only visible after a user has selected a file or folder. It's just a way of tidying away the other two buttons when they're no longer needed and decluttering the UX. In custom app versions, clicking it has the additional function of resetting storage to the default location (the folder where the packaged archive is stored) - because this can't be selected with a file picker. The "Reload" icon allows the user easily to refresh a picked directory, for example after adding more ZIM files to it, or after having downloaded a new ZIM file. But I'm keen to find ways of decluttering and simplifying UX. |
Regarding automatically choosing the last ZIM file, this is only possible in a framework like Firefox OS, Electron, NWJS or UWP, and it works that way in all those versions of Kiwix JS. Unfortunately, with the File System Access API, for security reasons the user permisison prompt can ONLY be triggered by a user gesture. It can't be triggered programmatically. So we have to have a minimum of two clicks to re-launch the last used archive. However, I still find two clicks to be much better UX than asking the user to re-pick the archive every time. The other big advantage is that switching between archives in the folder is very fast (one click on the file, because it doesn't ask for permission again in any one session). The Chrome developers have said they are considering allowing an INSTALLED PWA to have persistent permission, for a native-like experience (so user would not need to grant permission each time). But this is not even in the draft specification yet, so don't hold your breath, and it probably wouldn't apply to the extension version. Note that drag-and-drop integrates automatically with the File System Access API, so dragging a file or folder in will mean it's available to reload on next launch with two clicks and no file picker needed. Finally, on Chrome, the File Handling API (https://web.dev/file-handling/) is also enabled. This means that you can just double click a ZIM archive and it will load in Kiwix JS if it is the default app for handling ZIM archives. This works on Windows if you install the PWA (it has to be installed). I don't htink this is relevant for the Chrome/Edge extensions, however. There may be an extension API that allows it. |
I'm not against using an API that is not supported by all browser engines, if there's a significant advantage of doing so. "Remembering" the last ZIM file is a very popular demand, so might be a good reason to use this new API. But I'm disappointed by the restrictions put on this API in the context of a browser extension, that make it much less interesting than I initially thought (It has nothing to do with your code, of course). It seems to me that the main demand/use-case is to automatically re-open the last ZIM file. With this new API, it can be done, but still needs 2 clicks. With the current version of kiwix-js (without this new API), it needs 3 clicks (or one click and a double-click), as the browser remembers the last directory used. It's true that it should be much faster afterwards to switch to another ZIM file (in the same session), but does not seem to be the main use-case to me. The upstream bug on symlinks mentioned in kiwix/kiwix-js-pwa#283 also makes me reluctant to rush on this. |
@mossroy I understand your perspective. Personally, I don't think it's just about the number of clicks, it's also the fact that when you open the app, all the ZIM archives in your archive folder are listed right there in the app. The clicks needed are both in the same context. It feels less clunky as a result. There is a tangible advantage with split archives, as you only need to click on the .zimaa (in fact the others aren't shown), to open the whole set in the folder. But I agree that the gain is only marginal currently, and there are other priorities that are more urgent. |
It seems there is an issue using this API in Chrome extensions. See WICG/file-system-access#289. |
The corresponding Chromium bug report regarding |
Now that we were forced to use the PWA workaround in order to support Manifest V3 in the Chromium extension, it is more feasible to add the File System Access API. It looks like Chrome is close to implementing persistent permissions for installed PWAs -- see here and here. Persistent means that there would be no further user prompt. Currently there is a small prompt required when accessing files or folders across sessions: it's quick and only requires one click. The idea of the upcoming changes is that if the PWA is installed as an app, rather than merely accessed from an offline Web address, then no further user prompts would be required until permission is revoked. The main issue, it seems, would be conceptual confusion between an Extension and an installed PWA, even if they are the same thing under the hood... |
I have just tested (using Kiwix JS PWA) and in Chromium 117.0.2045.31 the |
@Rishabhg71 As mentioned in #1032, it may be easiest to work on this API holistically with the You can take a look at the implementation in Kiwix PWA (use the development version, because it is unminified code: https://kiwix.github.io/kiwix-js-windows/, and let it update to latest version, currently v2.7.4). The left-hand button in screenshot below (uncircled) works like this: if The right-hand (circled) button works like this: if |
Rather than reinventing the wheel, you could import There is also a useful There is a lot of stuff you don't currently need in I hope this helps. |
PS don't try to implement Origin Private File System (yet). That's a whole other level of complexity, and you have to have the building blocks in place first. |
@Rishabhg71 Sorry, one other thing -- when picking a directory and processing its contents, attention has to be paid to any split ZIM files in the directory. Only the |
Please correct me if I am wrong, But I need to go on and add a similar implementation done in kiwix-js-windows
I am still reading docs about FSA and OriginPFS here so drop any resource if you have any |
Ignore the information about OPFS in that document. Also read https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/webkitdirectory . Then I'd do it in this order:
|
Oh, between 2 and 3, I'd suggest working on what happens when the user reloads the app: it should try to launch the last selected archive. If you follow the process above, then when you attempt to verify permission for the stored File Handle in IndexedDB, it should ask the user for a quick permission prompt, user says yes, and the file should be displayed. Also handle user saying no. |
It is a popular demand, and was not technically possible (except on some specific devices that have specific APIs : Firefox OS and Windows Mobile).
This new API should allow to re-open the last ZIM file after closing/reopening kiwix-js.
It's very recent, and not supported by a lot of browsers (currently only Chrome >=86). Firefox is unfortunately not actively working on it for now. See https://caniuse.com/native-filesystem-api
It should be tested if it also works inside a browser extension
The text was updated successfully, but these errors were encountered: