diff --git a/toolkit/components/translations/actors/TranslationsChild.sys.mjs b/toolkit/components/translations/actors/TranslationsChild.sys.mjs index cb676f98c52c..1f103105fff1 100644 --- a/toolkit/components/translations/actors/TranslationsChild.sys.mjs +++ b/toolkit/components/translations/actors/TranslationsChild.sys.mjs @@ -20,24 +20,25 @@ export class TranslationsChild extends JSWindowActorChild { */ innerWindowId = null; isDestroyed = false; - #areLangTagsReported = false; + #isPageHidden = false; #wasTranslationsEngineCreated = false; handleEvent(event) { switch (event.type) { case "DOMContentLoaded": - this.innerWindowId = this.contentWindow.windowGlobalChild.innerWindowId; + this.innerWindowId = + this.contentWindow?.windowGlobalChild.innerWindowId; if (!this.#isRestrictedPage()) { - this.#areLangTagsReported = true; this.sendAsyncMessage("Translations:ReportLangTags", { documentElementLang: this.document.documentElement.lang, }); } break; + case "pageshow": + this.#isPageHidden = false; + break; case "pagehide": - if (this.#areLangTagsReported) { - this.sendAsyncMessage("Translations:ClearLangTags"); - } + this.#isPageHidden = true; break; } } @@ -47,6 +48,9 @@ export class TranslationsChild extends JSWindowActorChild { * about:* pages will not be translated. */ #isRestrictedPage() { + if (!this.contentWindow?.location) { + return true; + } const { href } = this.contentWindow.location; // Keep this logic up to date with TranslationsParent.isRestrictedPage. return !( @@ -141,9 +145,12 @@ export class TranslationsChild extends JSWindowActorChild { } createLanguageIdEngine() { - return lazy.LanguageIdEngine.getOrCreate(() => - this.sendQuery("Translations:GetLanguageIdEnginePayload") - ); + return lazy.LanguageIdEngine.getOrCreate(() => { + if (this.#isPageHidden) { + throw new Error("The page was already hidden."); + } + return this.sendQuery("Translations:GetLanguageIdEnginePayload"); + }); } createTranslationsEngine(fromLanguage, toLanguage) { diff --git a/toolkit/components/translations/actors/TranslationsParent.sys.mjs b/toolkit/components/translations/actors/TranslationsParent.sys.mjs index b3dfa61e9bbb..ce84b2204cc4 100644 --- a/toolkit/components/translations/actors/TranslationsParent.sys.mjs +++ b/toolkit/components/translations/actors/TranslationsParent.sys.mjs @@ -456,10 +456,6 @@ export class TranslationsParent extends JSWindowActorParent { case "Translations:GetPreferredLanguages": { return TranslationsParent.getPreferredLanguages(); } - case "Translations:ClearLangTags": { - this.languageState.detectedLanguages = null; - return undefined; - } case "Translations:ReportLangTags": { const { documentElementLang, href } = data; const detectedLanguages = await this.getDetectedLanguages( diff --git a/toolkit/modules/ActorManagerParent.sys.mjs b/toolkit/modules/ActorManagerParent.sys.mjs index 7805aa1da361..7b9db7f281bd 100644 --- a/toolkit/modules/ActorManagerParent.sys.mjs +++ b/toolkit/modules/ActorManagerParent.sys.mjs @@ -489,9 +489,8 @@ let JSWINDOWACTORS = { child: { esModuleURI: "resource://gre/actors/TranslationsChild.sys.mjs", events: { + pagehide: {}, pageshow: {}, - DOMHeadElementParsed: {}, - DOMDocElementInserted: {}, DOMContentLoaded: {}, }, },