Spaces:
Paused
Paused
File size: 4,290 Bytes
4e7644b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | /**
* Cross-runtime external link opener for browser, legacy pywebview, and Tauri.
* It validates supported protocols and delegates desktop shells to native openers.
*/
import { invoke, isTauri } from "@tauri-apps/api/core";
import { getPyWebViewApi } from "./pywebview";
const URL_WITH_SCHEME_RE = /^[a-z][a-z\d+\-.]*:/i;
const HTTP_PROTOCOLS = new Set(["http:", "https:"]);
// Keep in sync with console/src-tauri/src/external_link.rs.
const SUPPORTED_EXTERNAL_PROTOCOLS = new Set([
"http:",
"https:",
"mailto:",
"tel:",
]);
const TAURI_OPEN_EXTERNAL_LINK_COMMAND = "open_external_link";
type ExternalLinkRuntime = "pywebview" | "tauri" | "browser";
function hasHttpUrlPrefix(url: string): boolean {
return /^https?:\/\//i.test(url);
}
/** Resolve absolute and app-relative URLs while ignoring empty or hash-only links. */
export function resolveExternalUrl(url: string): string | null {
const trimmedUrl = url.trim();
if (!trimmedUrl || trimmedUrl.startsWith("#")) {
return null;
}
try {
if (URL_WITH_SCHEME_RE.test(trimmedUrl)) {
return trimmedUrl;
}
return new URL(trimmedUrl, window.location.origin).toString();
} catch {
return null;
}
}
function protocolOf(url: string): string {
return new URL(url).protocol;
}
/** Return true when a resolved URL is HTTP(S), the legacy desktop bridge's scope. */
export function isHttpExternalUrl(url: string): boolean {
try {
return hasHttpUrlPrefix(url) && HTTP_PROTOCOLS.has(protocolOf(url));
} catch {
return false;
}
}
function isSupportedExternalUrl(url: string): boolean {
try {
const protocol = protocolOf(url);
if (HTTP_PROTOCOLS.has(protocol)) {
return hasHttpUrlPrefix(url);
}
return SUPPORTED_EXTERNAL_PROTOCOLS.has(protocol);
} catch {
return false;
}
}
/** Resolve an input URL only if its protocol is safe to hand to external openers. */
export function resolveSupportedExternalUrl(url: string): string | null {
const resolvedUrl = resolveExternalUrl(url);
if (!resolvedUrl || !isSupportedExternalUrl(resolvedUrl)) {
return null;
}
return resolvedUrl;
}
/** Detect Tauri even after the app has navigated to the backend-hosted console. */
export function isDesktopTauriRuntime(): boolean {
// When Tauri loads a remote-origin console, injected internals can still be
// available even when the SDK helper does not report the runtime.
return isTauri() || hasTauriInternals();
}
/** Check for Tauri's injected invoke hook when the SDK helper is unavailable. */
function hasTauriInternals(): boolean {
if (typeof window === "undefined") return false;
return (
typeof (window as { __TAURI_INTERNALS__?: { invoke?: unknown } })
.__TAURI_INTERNALS__?.invoke === "function"
);
}
// Runtime priority is intentional: the legacy pywebview bridge has its own
// opener, while packaged Tauri should use the native command exposed to the
// WebView, including backend-hosted remote origins allowed by capabilities.
/** Choose which runtime should handle a validated external URL. */
function detectExternalLinkRuntime(fullUrl: string): ExternalLinkRuntime {
const pywebviewApi = getPyWebViewApi();
if (pywebviewApi?.open_external_link && isHttpExternalUrl(fullUrl)) {
return "pywebview";
}
if (isDesktopTauriRuntime()) {
return "tauri";
}
return "browser";
}
/**
* Open a supported external URL in the OS/browser-appropriate target.
* Desktop bridge failures are logged asynchronously because clicks are
* fire-and-forget from the UI's perspective.
*/
export function openExternalLink(
url: string,
target: string = "_blank",
features: string = "noopener,noreferrer",
): void {
if (!url) return;
const fullUrl = resolveSupportedExternalUrl(url);
if (!fullUrl) {
return;
}
const runtime = detectExternalLinkRuntime(fullUrl);
switch (runtime) {
case "pywebview": {
getPyWebViewApi()?.open_external_link?.(fullUrl);
return;
}
case "tauri": {
void invoke(TAURI_OPEN_EXTERNAL_LINK_COMMAND, { url: fullUrl }).catch(
(error) => {
console.warn("[external-link] Tauri open command failed", error);
},
);
return;
}
case "browser":
window.open(fullUrl, target, features);
}
}
|