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
65 stars 3 forks source link

Old pages stay connected #17

Closed jelmervdl closed 2 years ago

jelmervdl commented 2 years ago

I noticed that when you navigate between two pages that are in the cache, they both stay connected and interact with the background script.

Using pageshow and pagehide events we can treat these the same as if they are unloaded, but after a pageshow event, much of the javascript state is still there which is really odd. For example, The tab's state variable could be translation-available but the InPageTranslator could still be active from when the page was page-is-translating.

Another effect is that when you navigate from one page to the next, and the new page is asked for a language sample, the old page will also respond. This messes with language detection.

Example: navigate to de.wikpedia.org. Then go to es.wikipedia.org. Then go back to de.wikipedia.org. It is a mess.

jelmervdl commented 2 years ago

Probably best to treat pageshow and pagehide as load & unload, and stop any translations after pagehide to avoid non-visible pages that keep using resources. This will require InPageTranslation to have some way to pause and resume, or see which nodes have already be translated when starting over. It will need to re-submit everything that hasn't been translated since if we treat pagehide as an unload, the background-script will have cleared any pending requests from that page from the translation request queue.