export function navigateToUrl(url) {
window.location.assign(url);
}
export function isExternalUrl(url) {
try {
const u = new URL(url, window.location.href);
if (u.protocol !== 'http:' && u.protocol !== 'https:') return false;
return u.origin !== window.location.origin;
} catch (_) {
return false;
}
}
export function openExternal(url) {
const isTWA = document.referrer.startsWith('android-app://');
if (isTWA && /^https?:\/\//.test(url)) {
const urlObj = new URL(url);
const intentUrl = `intent://${urlObj.host}${urlObj.pathname}${urlObj.search}${urlObj.hash}#Intent;action=android.intent.action.VIEW;scheme=${urlObj.protocol.replace(':', '')};S.browser_fallback_url=${encodeURIComponent(url)};end;`;
const navigate = window.__mobuxNavigateToUrl || navigateToUrl;
navigate(intentUrl);
return;
}
const a = document.createElement('a');
a.href = url;
a.target = '_blank';
a.rel = 'noopener noreferrer';
a.style.display = 'none';
a.dataset.mobuxExternal = '1';
document.body.appendChild(a);
a.click();
a.remove();
}
export function installExternalLinkHandler(target) {
if (window.__mobuxExternalLinkHandlerInstalled) return;
window.__mobuxExternalLinkHandlerInstalled = true;
(target || document).addEventListener(
'click',
(e) => {
if (
e.defaultPrevented ||
e.button !== 0 ||
e.metaKey ||
e.ctrlKey ||
e.shiftKey ||
e.altKey
)
return;
const anchor = e.target.closest && e.target.closest('a[href]');
if (!anchor) return;
if (anchor.dataset.mobuxExternal) return;
if (anchor.hasAttribute('download')) return;
const href = anchor.getAttribute('href');
if (!href || !isExternalUrl(href)) return;
e.preventDefault();
openExternal(anchor.href);
},
true,
);
}