import { isTauri, isDesktopShell } from './platform'; // Lowercase the scheme: the opener plugin's URL scope is case-sensitive and only // allows http/https, so a stored "Https://..." is rejected ("Not allowed to open // url"). Leave the rest of the URL untouched. function normalizeUrl(url: string): string { return url.replace(/^([a-zA-Z][a-zA-Z0-9+.-]*):/, (_m, scheme) => `${scheme.toLowerCase()}:`); } // External links leave the webview (which would otherwise navigate the app away). // Desktop hands off to the system browser; mobile opens an in-app browser (Android // Custom Tab) so the user stays in context. The opener:allow-open-url scope grants // the 'inAppBrowser' app for http/https. Plain browser gets a new tab. export async function openExternal(url: string): Promise { const target = normalizeUrl(url); if (isTauri()) { const { openUrl } = await import('@tauri-apps/plugin-opener'); await openUrl(target, isDesktopShell() ? undefined : 'inAppBrowser'); return; } window.open(target, '_blank', 'noopener'); } // One delegated listener for the shells: any absolute http(s) anchor is an // external link (app routes are all relative), whatever component renders it. export function interceptExternalLinks(): void { document.addEventListener('click', (e) => { if (e.defaultPrevented || e.button !== 0) return; const anchor = (e.target as Element).closest?.('a[href]'); const href = anchor?.getAttribute('href') ?? ''; if (!/^https?:/i.test(href)) return; e.preventDefault(); void openExternal(href); }); }