@@ -274,22 +274,21 @@ export function detectBrowser() {
274274 if ( typeof navigator === 'undefined' || ! navigator ) return { name : 'unknown' , version : 0 } ;
275275 const ua = navigator . userAgent ;
276276
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+ ] ;
285285
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 ) ;
290289 if ( match ?. [ 1 ] ) {
291290 return {
292- name : key . toLowerCase ( ) ,
291+ name,
293292 version : Number ( match [ 1 ] ?. split ( '.' ) [ 0 ] || 0 )
294293 } ;
295294 }
0 commit comments