waybetter/packages/runtime/src/pageLang.ts
CRT faa5bd292b pageLang: report X's language even when installed before <html> exists or before the IPC bridge
At document start (where the app installs the X runtime) there is no <html> element yet, so
the lang report never ran and "Follow X" never learned X's language (found live: siteLang
stayed null). It now reads lang again at readystatechange/DOMContentLoaded, and a report
that fails (no bridge yet) is retried with a bounded backoff. Tests.
2026-09-29 04:01:53 +00:00

63 lines
2.6 KiB
TypeScript

// Tells the app which language the site's own UI is in (its `<html lang>`), so the app can follow
// it ("Follow X" in Settings → Language). Read from the document the site renders anyway; nothing is
// requested. Only a change is reported (`tab_report { lang }`); native code accepts it only from a
// site tab and only as a validated language tag.
import { normalizeLocale } from './i18n.ts';
export type Invoke = (command: string, args: Record<string, unknown>) => Promise<unknown>;
/** The page's language as a canonical tag, or null when it declares none (or nonsense). */
export function readPageLang(doc: Document): string | null {
return normalizeLocale(doc.documentElement?.getAttribute('lang') ?? '');
}
/** Retries of a report that failed (the IPC bridge is not there yet at document start). */
export const LANG_REPORT_RETRIES = 12;
export function installPageLangReport(win: Window & typeof globalThis, invoke?: Invoke): { dispose(): void } {
if (!invoke) return { dispose() {} };
let last = '';
let failures = 0;
let timer: number | undefined;
let disposed = false;
const check = () => {
timer = undefined;
if (disposed) return;
const lang = readPageLang(win.document);
if (!lang || lang === last) return;
last = lang;
invoke('tab_report', { report: { lang } }).then(() => { failures = 0; }, () => {
// The page's `lang` rarely changes after load, so a report lost at document start (no bridge
// yet) would never be sent again: "Follow X" would not learn X's language. Try again.
if (last === lang) last = '';
if (disposed || timer !== undefined || failures >= LANG_REPORT_RETRIES) return;
failures += 1;
timer = win.setTimeout(check, Math.min(4000, 125 * 2 ** failures));
});
};
const observer = new win.MutationObserver(check);
let observed: Element | null = null;
// At document start (where the app installs this) there may be no <html> element yet: watch it,
// and read `lang`, as soon as the parser has made it.
const attach = () => {
const root = win.document.documentElement;
if (root && root !== observed) {
observed = root;
observer.observe(root, { attributes: true, attributeFilter: ['lang'] });
}
check();
};
win.document.addEventListener('readystatechange', attach);
win.document.addEventListener('DOMContentLoaded', attach);
attach();
return {
dispose: () => {
disposed = true;
observer.disconnect();
win.document.removeEventListener('readystatechange', attach);
win.document.removeEventListener('DOMContentLoaded', attach);
if (timer !== undefined) win.clearTimeout(timer);
},
};
}