Blame view

src/utils/browser.ts 2.07 KB
陈文彬 authored
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
function getTypeVersion() {
  const userAgent = navigator.userAgent.toLowerCase();

  const browserTypes = {
    IE: /(?:msie|trident.*rv).([\d.]+)/,
    Edge: /edge.([\d.]+)/,
    Chrome: /chrome.([\d.]+)/,
    Firefox: /firefox.([\d.]+)/,
    Opera: /opera.([\d.]+)/,
    Safari: /(?:safari|version).([\d.]+)/,
  };
  type BrowserKeys = keyof typeof browserTypes;

  /** browser type */
  let type!: BrowserKeys | null;
  /** browser version */
  let version!: string | null;

  for (type in browserTypes) {
    if ((version = browserTypes[type as BrowserKeys].exec(userAgent) as any)) {
      version = version[1];
      break;
    }
  }

  if (version) {
    if (type === 'IE') {
      try {
        document.execCommand('BackgroundImageCache', false, true as any);
      } catch (error) {
        console.log(error);
      }
    }
  } else {
    type = version = null;
  }
  return { type, version };
}

const { type, version } = getTypeVersion();

export function getType() {
  return type;
}

export function getVersion() {
  return version;
}

export function isIeFn() {
  return type === 'IE';
}

export function isChromeFn() {
  return type === 'Chrome';
}

export function isEdgeFn() {
  return type === 'Edge';
}

export function isSafariFn() {
  return type === 'Safari';
}

export function isFirefoxFn() {
  return type === 'Firefox';
}

export function isOperaFn() {
  return type === 'Opera';
}
vben authored
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

/**
 * set page Title
 * @param {*} title  :page Title
 */
const setDocumentTitle = (title: string) => {
  document.title = title;
  const ua = navigator.userAgent;
  const regex = /\bMicroMessenger\/([\d.]+)/;
  // 兼容
  if (regex.test(ua) && /ip(hone|od|ad)/i.test(ua)) {
    const i = document.createElement('iframe');
    i.src = '/favicon.ico';
    i.style.display = 'none';
    i.onload = function () {
      setTimeout(function () {
        i.remove();
      }, 9);
    };
    document.body.appendChild(i);
  }
};

export function setTitle(title: string, appTitle?: string) {
  if (title) {
    const _title = title ? ` ${title}-${appTitle} ` : `${appTitle}`;
    setDocumentTitle(_title);
  }
}