RussCoder / djvujs

DjVu.js is a program library for working with .djvu files online without any connection with the server. DjVu.js Viewer is a widget that allows viewing .djvu files right in the browser and can be easily embedded into any web page.
https://djvu.js.org/
Other
182 stars 36 forks source link

[Extension] Embed file path into URL for reloading #95

Open minerharry opened 7 months ago

minerharry commented 7 months ago

Feature Request: Embedding the path to the file you are viewing into the URL would make it so on reload, close/reopen, and bookmark you could save and automatically return to the file you were viewing. Especially relevant for the firefox extension where the file:// syntax doesn't work.

RussCoder commented 7 months ago

In Chromium-based browsers it's already so. But in Firefox I think it's not possible. One cannot open a .djvu file in Firefox with a double click, and there is no "Allow access to file URLs" option.

minerharry commented 7 months ago

How about as a query string? (If this is stupid forgive me for my limited knowledge of webdev)

RussCoder commented 7 months ago

It's not about the URL, it's about the API provided (or not provided) by the browser. I can edit the URL as I want, but I cannot get a file path, and even if I get it, I will not be able to load the file.

By default, JavaScript has no access to the file system. So websites cannot scan your hard drive. When you upload a file, only its name is exposed (and some metadata), but not the full path. However, even if the full path is known, there is no API to load the file (again, for security reasons). Thus, JS can read only files that have been explicitly uploaded by the user.

Web extensions are JS applications and use the same browser APIs as websites do. But there are some additional web extension APIs too.

In Chromium-based browsers, there is a separate option "Allow access to file URLs". It should be enabled manually, after the extension has been installed. If the user has enabled it, and opens a file with the browser, then the browser initiates a request to the file using a file URL. This request can be intercepted by the extension, and using the file URL the extension can load the file.

In case of Firefox, it doesn't work. If you try to open a binary file with Firefox, it will show you the download dialog. It doesn't initiate a request, so the exception cannot intercept it. And there is no "Allow access to file URLs" option too.

minerharry commented 6 months ago

Ahhhh I see, very annoying. Is there a way to hint at the file open dialog to open on the previously selected file, or is that all controlled by Firefox as well?

RussCoder commented 6 months ago

It turns out there is an API to do it, but it has not been implemented in Firefox yet, and it allows an app to select the initial directory, only if the app has already got a file handle from that directory. That is, if you open several files on the same page, then this API can be used. But if you close the page, all file handles will be lost.