Spaces:
Sleeping
Sleeping
Download packages/web/src/platform.ts from WorstCardGame/worst-card-game: direct link, hf CLI and curl.
- Browser
- Download file 915 Bytes
-
https://huggingface.co/spaces/WorstCardGame/worst-card-game/resolve/main/packages/web/src/platform.ts
- Command line
-
hf download hf://spaces/WorstCardGame/worst-card-game/packages/web/src/platform.ts
-
curl -L -o platform.ts https://huggingface.co/spaces/WorstCardGame/worst-card-game/resolve/main/packages/web/src/platform.ts
915 Bytes
| /** | |
| * 執行環境偵測:app 是被嵌在 Discord Activity 的 iframe 裡,還是一般網頁。 | |
| * 整個前端只在這裡判斷一次,其他地方只問 `platform`,不到處寫 `if (在 discord)`(消除硬編耦合)。 | |
| * | |
| * 偵測依據(任一成立即視為 Discord): | |
| * - host 結尾為 `.discordsays.com`(Activity 一律跑在 Discord 代理網域)。 | |
| * - URL 帶 `frame_id` query(Discord 啟動 Activity 時注入;DiscordSDK 也靠它)。 | |
| */ | |
| export type Platform = "discord" | "web"; | |
| function detect(): Platform { | |
| if (typeof location === "undefined") return "web"; | |
| const inDiscordHost = location.hostname.endsWith(".discordsays.com"); | |
| const hasFrameId = new URLSearchParams(location.search).has("frame_id"); | |
| return inDiscordHost || hasFrameId ? "discord" : "web"; | |
| } | |
| export const platform: Platform = detect(); | |
| export const isDiscordActivity = platform === "discord"; | |