Nessuna descrizione
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. const UA = navigator.userAgent
  2. const isIpad = /(iPad).*OS\s([\d_]+)/.test(UA)
  3. const isIpod = /(iPod)(.*OS\s([\d_]+))?/.test(UA)
  4. const isIphone = !isIpad && /(iPhone\sOS)\s([\d_]+)/.test(UA)
  5. const isIos = isIpad || isIpod || isIphone
  6. const isAndroid = /(Android);?[\s\/]+([\d.]+)?/.test(UA)
  7. const isWechat = /micromessenger/i.test(UA)
  8. const isQQ = /QQ\/([\d\.]+)/.test(UA)
  9. const isQZone = /Qzone\//.test(UA)
  10. const isQQMBrowser = /MQQBrowser/i.test(UA) && !isWechat && !isQQ
  11. const isUCMBrowser = /UCBrowser/i.test(UA)
  12. const isBaiduMBrowser = /mobile.*baidubrowser/i.test(UA)
  13. const isSogouMBrowser = /SogouMobileBrowser/i.test(UA)
  14. const isBaiduApp = /baiduboxapp/i.test(UA)
  15. function noop() {}
  16. function loadJs(src, callback = noop) {
  17. const ref = document.getElementsByTagName('script')[0]
  18. const script = document.createElement('script')
  19. script.src = src
  20. script.async = true
  21. ref.parentNode.insertBefore(script, ref)
  22. script.onload = callback
  23. }
  24. function assign(target, varArgs) {
  25. if (target == null) {
  26. throw new TypeError('Cannot convert undefined or null to object')
  27. }
  28. var to = Object(target)
  29. for (var index = 1; index < arguments.length; index++) {
  30. var nextSource = arguments[index]
  31. if (nextSource != null) {
  32. for (var nextKey in nextSource) {
  33. if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
  34. to[nextKey] = nextSource[nextKey]
  35. }
  36. }
  37. }
  38. }
  39. return to
  40. }
  41. function openAppByScheme(scheme) {
  42. if (isIos) {
  43. location.href = scheme
  44. } else {
  45. var iframe = document.createElement('iframe')
  46. iframe.style.display = 'none'
  47. iframe.src = scheme
  48. document.body.appendChild(iframe)
  49. setTimeout(function() {
  50. iframe && iframe.parentNode && iframe.parentNode.removeChild(iframe)
  51. }, 2000)
  52. }
  53. }
  54. function generateQueryString(queryObj, needEncode = false) {
  55. const arr = []
  56. for (let key in queryObj) {
  57. if (needEncode) {
  58. arr.push(`${key}=${encodeURIComponent(queryObj[key])}`)
  59. } else {
  60. arr.push(`${key}=${queryObj[key]}`)
  61. }
  62. }
  63. return arr.join('&')
  64. }
  65. const Base64 = {
  66. _keyStr: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
  67. encode: function(a) {
  68. var b,
  69. c,
  70. d,
  71. e,
  72. f,
  73. g,
  74. h,
  75. i = '',
  76. j = 0
  77. for (a = Base64._utf8_encode(a); j < a.length; )
  78. (b = a.charCodeAt(j++)), (c = a.charCodeAt(j++)), (d = a.charCodeAt(j++)), (e = b >> 2), (f =
  79. ((3 & b) << 4) | (c >> 4)), (g = ((15 & c) << 2) | (d >> 6)), (h = 63 & d), isNaN(c)
  80. ? (g = h = 64)
  81. : isNaN(d) && (h = 64), (i =
  82. i + this._keyStr.charAt(e) + this._keyStr.charAt(f) + this._keyStr.charAt(g) + this._keyStr.charAt(h))
  83. return i
  84. },
  85. _utf8_encode: function(a) {
  86. a = a.replace(/\r\n/g, '\n')
  87. for (var b = '', c = 0; c < a.length; c++) {
  88. var d = a.charCodeAt(c)
  89. d < 128
  90. ? (b += String.fromCharCode(d))
  91. : d > 127 && d < 2048
  92. ? ((b += String.fromCharCode((d >> 6) | 192)), (b += String.fromCharCode((63 & d) | 128)))
  93. : (
  94. (b += String.fromCharCode((d >> 12) | 224)),
  95. (b += String.fromCharCode(((d >> 6) & 63) | 128)),
  96. (b += String.fromCharCode((63 & d) | 128))
  97. )
  98. }
  99. return b
  100. },
  101. }
  102. function getHostnameFromUrl(url) {
  103. const a = document.createElement('a')
  104. a.href = url
  105. return a.hostname
  106. }
  107. const descTag = document.querySelector('meta[name=description]')
  108. const iconTag = document.querySelector('link[rel*=icon]')
  109. function getContentFromDescTag() {
  110. return Object(descTag).content || ''
  111. }
  112. function getHrefFromIconTag() {
  113. return Object(iconTag).href || `${location.protocol}//${location.hostname}/favicon.ico`
  114. }
  115. function getTitleFromTitleTag() {
  116. return document.title
  117. }
  118. function setDescTagContent(content) {
  119. if (descTag) {
  120. descTag.content = content
  121. } else {
  122. document.head.insertAdjacentHTML('beforeend', `<meta name="description" content="${content}">`)
  123. }
  124. }
  125. function setIconTagHref(href) {
  126. if (iconTag) {
  127. iconTag.href = href
  128. } else {
  129. document.head.insertAdjacentHTML('beforeend', `<link rel="shortcut icon" href="${href}">`)
  130. }
  131. }
  132. function setTitleTagTitle(title) {
  133. document.title = title
  134. }
  135. export {
  136. isWechat,
  137. isQQ,
  138. isQZone,
  139. isIos,
  140. isAndroid,
  141. isQQMBrowser,
  142. isUCMBrowser,
  143. isBaiduMBrowser,
  144. isSogouMBrowser,
  145. isBaiduApp,
  146. loadJs,
  147. noop,
  148. assign,
  149. openAppByScheme,
  150. generateQueryString,
  151. Base64,
  152. getHostnameFromUrl,
  153. getContentFromDescTag,
  154. getHrefFromIconTag,
  155. getTitleFromTitleTag,
  156. setDescTagContent,
  157. setIconTagHref,
  158. setTitleTagTitle,
  159. }