jelmervdl / translatelocally-web-ext

TranslateLocally for the Browser is a web-extension that enables client side in-page translations for web browsers.
https://addons.mozilla.org/en-GB/firefox/addon/translatelocally-for-firefox/
Mozilla Public License 2.0
66 stars 3 forks source link

Google Chrome #10

Open jelmervdl opened 2 years ago

jelmervdl commented 2 years ago

Can we bring this extension to Chrome (and other Chromium based browsers) as well?

Service workers replace background pages.

Source: https://developer.chrome.com/docs/extensions/mv3/intro/mv3-overview/#feature-summary

Oh dear. This has me worried.

Once it has been loaded, an extension's service worker generally keeps running as long as it is performing an action, such as calling a Chrome API or issuing a network request.

Source: https://developer.chrome.com/docs/extensions/mv3/service_workers/

The background script will not unload until all message ports have shut.

Source: https://developer.chrome.com/docs/extensions/mv3/service_workers/

Oh, that might be our way out. Our current content script has an open port for the full period it is loaded. But I have a feeling that Chrome might crack down on such behaviour.

Connecting to a native messaging host using chrome.runtime.connectNative() in an extension's service worker will keep the service worker alive as long as the port is open.

Source: https://developer.chrome.com/docs/extensions/whatsnew/#m100-native-msg-lifetime

Oh good. That's at least a bit hopeful, even though that has been introduced in 9 February, 2022 and talks about Canary. So I'm not expecting this in mainline anytime soon.

Technically most parts of this extension can run in the content-script but then you'd have TranslateLocally started separately for every iframe and that's just insane. Or TranslateLocally would have to do some tmux-like magic where it can spawn its own daemon and connect to it somehow but don't even think about going down that route.

Other scary bug reports:

… Browsers are like the TSA: in name of security you can barely do anything anymore.

jelmervdl commented 2 years ago

New v3 problem: Web workers do not seem to be available inside the ServiceWorker that's now responsible for the background_script. Right now, I run the WebAssembly instance inside a Worker to stay able to respond to things like translation and model list requests. (WebAssembly calls are blocking, calls to the Worker are through message passing.)

kpu commented 2 years ago

Or TranslateLocally would have to do some tmux-like magic where it can spawn its own daemon and connect to it somehow but don't even think about going down that route.

This actually seems like desirable functionality in the long term, if complicated to implement. Desktop application, browser, word all contacting a shared translation model process. Might also tie in with OS sandboxing. In Linux, I'd just send the stdin/stdout file descriptors as ancillary data over UNIX socket to the master daemon process then close the wrapper process. In windows, this is apparently not possible: https://devblogs.microsoft.com/commandline/af_unix-comes-to-windows/

kpu commented 2 years ago

Windows equivalent to move file descriptors: https://lackingrhoticity.blogspot.com/2015/05/passing-fds-handles-between-processes.html

jelmervdl commented 2 years ago

Current plan for Chrome support: