browser.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. function mobileVersion(e, t) {
  2. //ios的版本
  3. var i = window.navigator.userAgent,
  4. n = i.match(e)
  5. return (
  6. (n = n ? n[1].split(t) : []),
  7. {
  8. major: parseInt(n[0]) || 0,
  9. minor: parseInt(n[1]) || 0,
  10. patch: parseInt(n[2]) || 0,
  11. }
  12. )
  13. }
  14. var browser = {
  15. isFullscreen: function () {
  16. return document.fullscreenElement || document.mozFullscreenElement || document.mozFullScreenElement || document.webkitFullscreenElement || document.msFullscreenElement
  17. },
  18. supportsFullscreen: function () {
  19. return document.fullscreenEnabled || document.mozFullscreenEnabled || document.mozFullScreenEnabled || document.webkitFullscreenEnabled || document.msFullscreenEnabled
  20. },
  21. isPointerLocked: function () {
  22. return document.pointerLockElement || document.mozPointerLockElement || document.webkitPointerLockElement
  23. },
  24. requestFullscreen: function (dom, t) {
  25. dom.requestFullscreen
  26. ? dom.requestFullscreen()
  27. : dom.mozRequestFullScreen
  28. ? dom.mozRequestFullScreen()
  29. : dom.webkitRequestFullscreen
  30. ? dom.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT)
  31. : dom.msRequestFullscreen && dom.msRequestFullscreen(),
  32. t && $(document).on('fullscreenchange webkitfullscreenchange mozfullscreenchange MSFullscreenChange', browser.requestPointerLock)
  33. },
  34. requestPointerLock: function () {
  35. var e
  36. if (document.fullscreenElement) e = document.fullscreenElement()
  37. else if (document.mozFullscreenElement) e = document.mozFullscreenElement()
  38. else if (document.mozFullScreenElement) e = document.mozFullScreenElement()
  39. else {
  40. if (!document.webkitFullscreenElement) return
  41. e = document.webkitFullscreenElement()
  42. }
  43. ; (e.requestPointerLock = e.requestPointerLock || e.mozRequestPointerLock || e.webkitRequestPointerLock),
  44. e.requestPointerLock(),
  45. $(document).off('fullscreenchange webkitfullscreenchange mozfullscreenchange MSFullscreenChange', this)
  46. },
  47. exitPointerLock: function () {
  48. ; (document.exitPointerLock = document.exitPointerLock || document.mozExitPointerLock || document.webkitExitPointerLock), document.exitPointerLock()
  49. },
  50. exitFullscreen: function () {
  51. document.exitFullscreen
  52. ? document.exitFullscreen()
  53. : document.msExitFullscreen
  54. ? document.msExitFullscreen()
  55. : document.mozCancelFullScreen
  56. ? document.mozCancelFullScreen()
  57. : document.webkitExitFullscreen && document.webkitExitFullscreen()
  58. },
  59. details: function () {
  60. var e = navigator.userAgent.match('(Firefox|Chrome|Safari)/([\\d]+)')
  61. return e
  62. ? {
  63. name: e[1],
  64. version: parseInt(e[2]),
  65. platform: navigator.platform,
  66. }
  67. : {}
  68. },
  69. is: function (e) {
  70. return this.details() && this.details().name === e
  71. },
  72. inIframe: function () {
  73. return window.parent !== window
  74. },
  75. aspectRatio: function ($elem) {
  76. $elem = $elem || $('#player')
  77. var e = $elem.width() / $elem.height()
  78. return isFinite(e) ? e : 0
  79. },
  80. userAgent: function () {
  81. return window.navigator.userAgent
  82. },
  83. isMobile: function () {
  84. var e = navigator.userAgent || navigator.vendor || window.opera
  85. return (
  86. /(android|bb\d+|meego).+mobile|android|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od|ad)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(
  87. e
  88. ) ||
  89. /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(
  90. e.substr(0, 4)
  91. )
  92. )
  93. },
  94. isLandscape: function () {
  95. return this.isMobile && this.aspectRatio() > 1
  96. },
  97. isSmallScreen: function () {
  98. var e = screen.width / window.devicePixelRatio
  99. return e < 240
  100. },
  101. detectIE: function () {
  102. var e = window.navigator.userAgent,
  103. t = e.indexOf('MSIE ')
  104. return t !== -1 || !!navigator.userAgent.match(/Trident.*rv\:11\./)
  105. },
  106. detectEdge: function () {
  107. return window.navigator.userAgent.indexOf("Edge") > -1;
  108. },
  109. detectSafari: function () {
  110. var e = window.navigator.userAgent,
  111. t = e.indexOf('Safari')
  112. return t !== -1 && !this.detectOpera() && !this.detectChrome() //xzw add detectOpera
  113. },
  114. detectIosOrWebview: function () {
  115. var e = window.navigator.userAgent;
  116. var is_uiWebview = /(iPhone|iPod|iPad).*AppleWebKit(?!.*Version)/i.test(e);
  117. return is_uiWebview
  118. },
  119. detectFirefox: function () {
  120. var e = window.navigator.userAgent
  121. return e.indexOf('Firefox') !== -1
  122. },
  123. detectChrome: function () {
  124. var e = window.navigator.userAgent
  125. return e.indexOf('Chrome') !== -1 && !this.detectOpera()
  126. },
  127. detectOpera: function () {
  128. var e = window.navigator.userAgent
  129. return e.indexOf('OPR') !== -1
  130. },
  131. detectIOS: function () {
  132. return this.detectIPhone() || this.detectIPad() || this.detectIPod()
  133. },
  134. detectIPad: function () {
  135. var e = window.navigator.userAgent,
  136. t = /iPad/
  137. return t.test(e)
  138. },
  139. detectIPod: function () {
  140. var e = window.navigator.userAgent,
  141. t = /iPod/
  142. return t.test(e)
  143. },
  144. detectIPhone: function () {
  145. var e = window.navigator.userAgent,
  146. t = /iPhone/
  147. return t.test(e)
  148. },
  149. detectAndroid: function () {
  150. var e = window.navigator.userAgent
  151. return e.indexOf('Android') !== -1
  152. },
  153. detectAndroidMobile: function () {
  154. var e = window.navigator.userAgent
  155. return this.detectAndroid() && e.indexOf('Mobile') !== -1
  156. },
  157. detectSamsungNative: function () {
  158. var e = window.navigator.userAgent
  159. return e.indexOf('SM-G900H') !== -1 || e.indexOf('GT-I9500') !== -1 || e.indexOf('SM-N900') !== -1
  160. },
  161. detectSamsungS6: function () {
  162. var e = window.navigator.userAgent
  163. return e.indexOf('SM-G92') !== -1
  164. },
  165. /************************************************************徐世廷*************************************************************/
  166. detectHUAWEI5X: function () {
  167. return -1 !== window.navigator.userAgent.indexOf('KIW-TL00H')
  168. },
  169. /*******************************************************************************************************************************/
  170. detectWebVR: function () {
  171. return !(!window.navigator.getVRDisplays || !window.VRDisplay)
  172. },
  173. getVRDisplay: function () {
  174. var e = $.Deferred()
  175. return this.detectWebVR()
  176. ? (navigator.getVRDisplays().then(function (t) {
  177. t.length >= 1 && e.resolve(t[0]), e.reject(null)
  178. }),
  179. e)
  180. : e.reject(null)
  181. },
  182. iosVersion: function () {
  183. if (!this.detectIOS()) throw new DeviceMismatchException('Did not detect an iDevice')
  184. var e = /((?:\d+\_?){1,3}) like Mac OS/,
  185. t = '_'
  186. return mobileVersion(e, t)
  187. },
  188. androidVersion: function () {
  189. if (!this.detectAndroid()) throw new DeviceMismatchException('Did not detect an Android based device')
  190. var e = /Android ((?:\d+\.?){1,3})/,
  191. t = '.'
  192. return mobileVersion(e, t)
  193. },
  194. valueFromCookie: function (e, t) {
  195. var i = new RegExp(e + '=([0-9a-f]+)(; ?|$)').exec(document.cookie)
  196. if (!i) return t
  197. var n = i[1]
  198. return 'boolean' == typeof t ? 'true' === n || '1' === n : 'number' == typeof t ? parseFloat(n) : n
  199. },
  200. valueFromHash: function (e, t) {
  201. var i = new RegExp('[#&?]' + e + '=([^#&?]*)'),
  202. n = i.exec(window.location.href)
  203. if (!n) return t
  204. var r = n[1]
  205. return 'boolean' == typeof t ? 'true' === r || '1' === r : 'number' == typeof t ? parseFloat(r) : window.decodeURIComponent(r)
  206. },
  207. //-------许钟文:-------------------------------------------------
  208. getProjectNum: function () {
  209. //获取场景projectNum
  210. if (window.__ProjectNum && window.__ProjectNum != '__ProjectNum__') {
  211. return window.__ProjectNum
  212. }
  213. var number = window.location.href.substring(window.location.href.indexOf('=') + 1)
  214. if (number.indexOf('&') != -1) {
  215. number = number.substring(0, number.indexOf('&'))
  216. }
  217. if (number.indexOf('#') != -1) {
  218. number = number.substring(0, number.indexOf('#'))
  219. }
  220. return number
  221. },
  222. urlHasValue: function (key, isGetValue) {
  223. // debugger
  224. // if (getValue) { //得到类似n=1 的 1
  225. // var b = window.location.href.substring(window.location.href.indexOf("?") + 1);
  226. // var a = b.indexOf('&' + t + "=");
  227. // if (a > -1) {
  228. // var s = b.substring(a + ('&' + t + "=").length);
  229. // s.indexOf("&") > -1 && (s = s.substring(0, s.indexOf("&")));
  230. // s.indexOf("#") > -1 && (s = s.substring(0, s.indexOf("#")));
  231. // return s;
  232. // } else return false;
  233. // } else return window.location.search.match("&" + t + "|\\?" + t) != null; //window.location.href.substring(window.location.href.indexOf("?") + 1).indexOf('&' + t) > -1;
  234. //const value = window.location.search.match("&" + t + "|\\?" + t)
  235. if (key === 'm' && window.__ProjectNum && window.__ProjectNum != '__ProjectNum__') {
  236. return window.__ProjectNum
  237. }
  238. let querys = window.location.search.substr(1).split('&')
  239. if (isGetValue) {
  240. for (let i = 0; i < querys.length; i++) {
  241. let keypair = querys[i].split('=')
  242. if (keypair.length === 2 && keypair[0] === key) {
  243. return keypair[1]
  244. }
  245. }
  246. return ''
  247. } else {
  248. //return window.location.search.match("&" + key + "|\\?" + key) != null 有bug
  249. for (let i = 0; i < querys.length; i++) {
  250. let keypair = querys[i].split('=')
  251. if (keypair[0] == key) {
  252. return true
  253. }
  254. }
  255. return false
  256. }
  257. },
  258. urlHasValueFromUrl: function (paraName, url) {
  259. // 如果url中有特殊字符则需要进行一下解码
  260. url = decodeURI(url)
  261. var arrObj = url.split("?");
  262. if (arrObj.length > 1) {
  263. var arrPara = arrObj[1].split("&");
  264. var arr;
  265. for (var i = 0; i < arrPara.length; i++) {
  266. arr = arrPara[i].split("=");
  267. if (arr != null && arr[0] == paraName) {
  268. return decodeURIComponent(arr[1]);
  269. }
  270. }
  271. return "";
  272. } else {
  273. return "";
  274. }
  275. },
  276. /**
  277. * 获取查询参数的值
  278. * @param {String} key
  279. * @returns String
  280. */
  281. urlQueryValue(key) {
  282. return this.urlHasValue(key, true) || ''
  283. },
  284. /**
  285. * 获取hash参数的值
  286. * @param {String} key
  287. * @returns String
  288. */
  289. urlHashValue(key) {
  290. let querys = window.location.hash.substr(1).replace('/?', '').split('&')
  291. for (let i = 0; i < querys.length; i++) {
  292. let keypair = querys[i].split('=')
  293. if (keypair.length === 2 && keypair[0] === key) {
  294. return keypair[1]
  295. }
  296. }
  297. return ''
  298. },
  299. /**
  300. * 判断是否存在hash
  301. * @param {String} key
  302. * @returns Boolean
  303. */
  304. urlIsHasHash(key) {
  305. let querys = window.location.hash.substr(1).replace('/?', '').split('&')
  306. return querys.includes(key)
  307. },
  308. islongPhone: function () {
  309. //是否是刘海全面屏幕 仅仅根据比例判断 - -
  310. //screen.height == 812 && screen.width == 375)
  311. var r = screen.height / screen.width //可能横屏
  312. return this.isMobile() && (r > 1.99 || r < 0.502512) //18/9=2.165 //???
  313. },
  314. detectWeixin: function () {
  315. //微信 包括PC的微信
  316. return window.navigator.userAgent.toLowerCase().match(/MicroMessenger/i) == 'micromessenger'
  317. },
  318. detectWeixinMiniProgram: function () {
  319. return window.navigator.userAgent.match('miniProgram')
  320. },
  321. detectAlipay: function () {
  322. //阿里系
  323. return window.navigator.userAgent.toLowerCase().match(/Alipay/i) == 'alipay'
  324. },
  325. detectEdge: function () {
  326. return window.navigator.userAgent.indexOf("Edge") > -1;
  327. },
  328. detectTT: function () {
  329. //字节跳动
  330. return window.navigator.userAgent
  331. .toLowerCase()
  332. .includes("toutiaomicroapp");
  333. },
  334. detectTcTravel: function () {
  335. return window.navigator.userAgent
  336. .toLowerCase()
  337. .includes("tctravel");
  338. },
  339. detectDingDing: function () {
  340. return window.navigator.userAgent
  341. .toLowerCase()
  342. .includes("dingtalk");
  343. },
  344. detectApp: function () {
  345. return this.urlHasValue('app')
  346. },
  347. /**
  348. * 判断标签页是否切换状态
  349. */
  350. isTabHidden: function () {
  351. var prefixes = ['webkit', 'moz', 'ms', 'o']
  352. if ('hidden' in document) return document.hidden
  353. for (var i = 0; i < prefixes.length; i++) {
  354. if (prefixes[i] + 'Hidden' in document) return document[prefixes[i] + 'Hidden']
  355. }
  356. return false
  357. },
  358. hasURLParam: function (key) {
  359. let querys = window.location.search.substring(1).split('&')
  360. for (let i = 0; i < querys.length; i++) {
  361. let keypair = querys[i].split('=')
  362. if (keypair[0] == key) {
  363. return true
  364. }
  365. }
  366. return false
  367. },
  368. getURLParam: function (key) {
  369. let querys = window.location.search.substring(1).split('&')
  370. for (let i = 0; i < querys.length; i++) {
  371. let keypair = querys[i].split('=')
  372. if (keypair.length === 2 && keypair[0] === key) {
  373. return keypair[1]
  374. }
  375. }
  376. return ''
  377. },
  378. }
  379. export default browser