Skip to content

Commit b9e8da5

Browse files
authored
feat(env): 增加H5环境中特定浏览器的检测功能
1 parent 73a5c2f commit b9e8da5

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

src/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ export const utsPlatform = env.UNI_UTS_PLATFORM as UtsPlatform
7474
/** Detect if `process.env.UNI_PLATFORM` is `h5` or if `process.env.UNI_PLATFORM` is `web` */
7575
export const isH5 = platform === 'h5' || platform === 'web'
7676

77+
/** Get user agent string for browser detection */
78+
const userAgent = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase() : ''
79+
80+
/** Detect if running in WeChat browser in H5 environment */
81+
export const isH5Weixin = isH5 && /micromessenger/i.test(userAgent)
82+
83+
/** Detect if running in Weibo client in H5 environment */
84+
export const isH5Weibo = isH5 && /weibo/i.test(userAgent)
85+
86+
/** Detect if running in Alipay client in H5 environment */
87+
export const isH5Alipay = isH5 && /alipayclient/i.test(userAgent)
88+
7789
/** Detect if `process.env.UNI_PLATFORM` is `web` or if `process.env.UNI_PLATFORM` is `h5` */
7890
export const isWeb = platform === 'web' || platform === 'h5'
7991

test/index.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ describe('uni-env', () => {
1111
"subPlatform",
1212
"utsPlatform",
1313
"isH5",
14+
"isH5Weixin",
15+
"isH5Weibo",
16+
"isH5Alipay",
1417
"isWeb",
1518
"isApp",
1619
"isAppPlus",

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"compilerOptions": {
33
"target": "esnext",
4-
"lib": ["esnext"],
4+
"lib": ["esnext", "dom"],
55
"module": "esnext",
66
"moduleResolution": "Bundler",
77
"resolveJsonModule": true,

0 commit comments

Comments
 (0)