@@ -274,22 +274,21 @@ export function detectBrowser() {
274
274
if ( typeof navigator === 'undefined' || ! navigator ) return { name : 'unknown' , version : 0 } ;
275
275
const ua = navigator . userAgent ;
276
276
277
- // 缓存正则表达式
278
- const REGEX : { [ key : string ] : RegExp } = {
279
- CHROME : / A p p l e W e b K i t . + C h r o m e \/ ( [ 0 - 9 . ] + ) S a f a r i \/ + / i ,
280
- SAFARI : / A p p l e W e b K i t . + V e r s i o n \/ ( [ 0 - 9 . ] + ) S a f a r i \/ + / i ,
281
- FIREFOX : / F i r e f o x \/ ( [ 0 - 9 . ] + ) / i,
282
- EDGE : / E d g \/ ( [ 0 - 9 . ] + ) / i ,
283
- OPERA : / O P R \/ ( [ 0 - 9 . ] + ) / i,
284
- } ;
277
+ // 正则表达式
278
+ const BROWSER_REGEXES = [
279
+ { name : 'edge' , regex : / E d g \/ ( [ 0 - 9 . ] + ) / i } ,
280
+ { name : 'opera' , regex : / O P R \/ ( [ 0 - 9 . ] + ) / i } ,
281
+ { name : 'chrome' , regex : / C h r o m e \/ ( [ 0 - 9 . ] + ) / i } ,
282
+ { name : 'safari' , regex : / A p p l e W e b K i t . + V e r s i o n \/ ( [ 0 - 9 . ] + ) S a f a r i \/ (? ! . * ( C h r o m e | C h r o m i u m | E d g | O P R ) ) / i } ,
283
+ { name : 'firefox' , regex : / F i r e f o x \/ ( [ 0 - 9 . ] + ) / i } ,
284
+ ] ;
285
285
286
- const keys = Object . keys ( REGEX ) ;
287
- for ( let i = 0 ; i < keys . length ; i ++ ) {
288
- const key = keys [ i ] ;
289
- const match = ua . match ( REGEX [ key ] ) ;
286
+ for ( let i = 0 ; i < BROWSER_REGEXES . length ; i ++ ) {
287
+ const { name, regex } = BROWSER_REGEXES [ i ] ;
288
+ const match = ua . match ( regex ) ;
290
289
if ( match ?. [ 1 ] ) {
291
290
return {
292
- name : key . toLowerCase ( ) ,
291
+ name,
293
292
version : Number ( match [ 1 ] ?. split ( '.' ) [ 0 ] || 0 )
294
293
} ;
295
294
}
0 commit comments