67 lines
1.6 KiB
TypeScript
67 lines
1.6 KiB
TypeScript
const A_ORIGIN = (import.meta.env.VITE_A_ORIGIN || "http://localhost:3000").replace(
|
|
/\/$/,
|
|
""
|
|
);
|
|
|
|
const errorEl = document.getElementById("aurora-error");
|
|
|
|
function showError(message: string) {
|
|
if (!errorEl) return;
|
|
errorEl.style.display = "block";
|
|
errorEl.textContent = message;
|
|
}
|
|
|
|
function loadScript(src: string): Promise<void> {
|
|
return new Promise((resolve, reject) => {
|
|
const script = document.createElement("script");
|
|
script.src = src;
|
|
script.async = true;
|
|
script.onload = () => resolve();
|
|
script.onerror = () => reject(new Error(`无法加载 ${src}`));
|
|
document.head.appendChild(script);
|
|
});
|
|
}
|
|
|
|
async function boot() {
|
|
try {
|
|
await loadScript(`${A_ORIGIN}/embed/aurora.js`);
|
|
} catch {
|
|
showError(
|
|
`无法加载 A 站脚本:${A_ORIGIN}/embed/aurora.js。请先在 project57 执行 pnpm build:embed 并启动 A 站,或修改 .env 中的 VITE_A_ORIGIN。`
|
|
);
|
|
return;
|
|
}
|
|
|
|
const api = window.AuroraSite;
|
|
if (!api?.mount) {
|
|
showError("A 站脚本已加载,但未找到 AuroraSite.mount。");
|
|
return;
|
|
}
|
|
|
|
api.mount("#aurora-root", {
|
|
assetBase: A_ORIGIN,
|
|
apiBase: A_ORIGIN,
|
|
cssUrl: `${A_ORIGIN}/embed/aurora.css`,
|
|
history: "hash",
|
|
});
|
|
}
|
|
|
|
boot();
|
|
|
|
declare global {
|
|
interface Window {
|
|
AuroraSite?: {
|
|
mount: (
|
|
target: string | Element,
|
|
options?: {
|
|
assetBase?: string;
|
|
apiBase?: string;
|
|
cssUrl?: string;
|
|
history?: "hash" | "browser";
|
|
basename?: string;
|
|
}
|
|
) => { unmount: () => void };
|
|
};
|
|
}
|
|
}
|