Files
cozsweet-frontend-nextjs/src/utils/browser-detect.ts
T

27 lines
1.1 KiB
TypeScript

/**
* 浏览器 / WebView 检测
*/
export const BrowserDetector = {
/** 当前是否在 Facebook 内嵌浏览器 */
isFacebookInAppBrowser(ua: string | null = null): boolean {
if (typeof navigator === "undefined" && !ua) return false;
const userAgent = ua ?? (typeof navigator !== "undefined" ? navigator.userAgent : "");
return /FBAN|FBAV|Instagram/i.test(userAgent);
},
/** 当前是否在微信内嵌浏览器 */
isWeChatInAppBrowser(ua: string | null = null): boolean {
if (typeof navigator === "undefined" && !ua) return false;
const userAgent = ua ?? (typeof navigator !== "undefined" ? navigator.userAgent : "");
return /MicroMessenger/i.test(userAgent);
},
/** 当前是否在 WebView(不通用;仅作 best-effort 判断) */
isInAppBrowser(ua: string | null = null): boolean {
if (typeof navigator === "undefined" && !ua) return false;
const userAgent = ua ?? (typeof navigator !== "undefined" ? navigator.userAgent : "");
return /WebView|; wv\)|FBAN|FBAV|MicroMessenger|Instagram|Line\//i.test(userAgent);
},
};