Files
gitlab-instance-0a899031_sh/d2ypp2/web-console/lib/panelUrls.ts
2026-05-16 19:24:30 -05:00

32 lines
1.1 KiB
TypeScript

/**
* 将服务状态里常见的 0.0.0.0 / 127.0.0.1 等替换为当前浏览器访问的主机名,
* 避免在局域网用 live.local 打开页面时 iframe/新窗口指向不可达地址。
*/
export function normalizeBrowserReachableHttpUrl(url: string): string {
const trimmed = url.trim();
if (typeof window === "undefined" || !trimmed) return trimmed;
try {
const u = new URL(trimmed, window.location.origin);
const h = u.hostname;
const pageHost = window.location.hostname;
if (h === "0.0.0.0" || h === "::" || h === "[::]") {
u.hostname = pageHost || h;
return u.toString();
}
if ((h === "127.0.0.1" || h === "localhost") && pageHost && pageHost !== "127.0.0.1" && pageHost !== "localhost") {
u.hostname = pageHost;
return u.toString();
}
if (h.endsWith(".local") && u.port === "8001") {
return `${u.pathname || "/"}${u.search}${u.hash}`;
}
if (h.endsWith(".local") && pageHost && pageHost !== h) {
u.hostname = pageHost;
return u.toString();
}
return trimmed;
} catch {
return trimmed;
}
}