browser.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. detectFirefox: function () {
  115. var e = window.navigator.userAgent
  116. return e.indexOf('Firefox') !== -1
  117. },
  118. detectChrome: function () {
  119. var e = window.navigator.userAgent
  120. return e.indexOf('Chrome') !== -1 && !this.detectOpera()
  121. },
  122. detectOpera: function () {
  123. var e = window.navigator.userAgent
  124. return e.indexOf('OPR') !== -1
  125. },
  126. detectIOS: function () {
  127. return this.detectIPhone() || this.detectIPad() || this.detectIPod()
  128. },
  129. detectIPad: function () {
  130. var e = window.navigator.userAgent,
  131. t = /iPad/
  132. return t.test(e)
  133. },
  134. detectIPod: function () {
  135. var e = window.navigator.userAgent,
  136. t = /iPod/
  137. return t.test(e)
  138. },
  139. detectIPhone: function () {
  140. var e = window.navigator.userAgent,
  141. t = /iPhone/
  142. return t.test(e)
  143. },
  144. detectAndroid: function () {
  145. var e = window.navigator.userAgent
  146. return e.indexOf('Android') !== -1
  147. },
  148. detectAndroidMobile: function () {
  149. var e = window.navigator.userAgent
  150. return this.detectAndroid() && e.indexOf('Mobile') !== -1
  151. },
  152. detectSamsungNative: function () {
  153. var e = window.navigator.userAgent
  154. return e.indexOf('SM-G900H') !== -1 || e.indexOf('GT-I9500') !== -1 || e.indexOf('SM-N900') !== -1
  155. },
  156. detectSamsungS6: function () {
  157. var e = window.navigator.userAgent
  158. return e.indexOf('SM-G92') !== -1
  159. },
  160. /************************************************************徐世廷*************************************************************/
  161. detectHUAWEI5X: function () {
  162. return -1 !== window.navigator.userAgent.indexOf('KIW-TL00H')
  163. },
  164. /*******************************************************************************************************************************/
  165. detectWebVR: function () {
  166. return !(!window.navigator.getVRDisplays || !window.VRDisplay)
  167. },
  168. getVRDisplay: function () {
  169. var e = $.Deferred()
  170. return this.detectWebVR()
  171. ? (navigator.getVRDisplays().then(function (t) {
  172. t.length >= 1 && e.resolve(t[0]), e.reject(null)
  173. }),
  174. e)
  175. : e.reject(null)
  176. },
  177. iosVersion: function () {
  178. if (!this.detectIOS()) throw new DeviceMismatchException('Did not detect an iDevice')
  179. var e = /((?:\d+\_?){1,3}) like Mac OS/,
  180. t = '_'
  181. return mobileVersion(e, t)
  182. },
  183. androidVersion: function () {
  184. if (!this.detectAndroid()) throw new DeviceMismatchException('Did not detect an Android based device')
  185. var e = /Android ((?:\d+\.?){1,3})/,
  186. t = '.'
  187. return mobileVersion(e, t)
  188. },
  189. valueFromCookie: function (e, t) {
  190. var i = new RegExp(e + '=([0-9a-f]+)(; ?|$)').exec(document.cookie)
  191. if (!i) return t
  192. var n = i[1]
  193. return 'boolean' == typeof t ? 'true' === n || '1' === n : 'number' == typeof t ? parseFloat(n) : n
  194. },
  195. valueFromHash: function (e, t) {
  196. var i = new RegExp('[#&?]' + e + '=([^#&?]*)'),
  197. n = i.exec(window.location.href)
  198. if (!n) return t
  199. var r = n[1]
  200. return 'boolean' == typeof t ? 'true' === r || '1' === r : 'number' == typeof t ? parseFloat(r) : window.decodeURIComponent(r)
  201. },
  202. //-------许钟文:-------------------------------------------------
  203. getProjectNum: function () {
  204. //获取场景projectNum
  205. if (window.__ProjectNum && window.__ProjectNum != '__ProjectNum__') {
  206. return window.__ProjectNum
  207. }
  208. var number = window.location.href.substring(window.location.href.indexOf('=') + 1)
  209. if (number.indexOf('&') != -1) {
  210. number = number.substring(0, number.indexOf('&'))
  211. }
  212. if (number.indexOf('#') != -1) {
  213. number = number.substring(0, number.indexOf('#'))
  214. }
  215. return number
  216. },
  217. urlHasValue: function (key, isGetValue) {
  218. // debugger
  219. // if (getValue) { //得到类似n=1 的 1
  220. // var b = window.location.href.substring(window.location.href.indexOf("?") + 1);
  221. // var a = b.indexOf('&' + t + "=");
  222. // if (a > -1) {
  223. // var s = b.substring(a + ('&' + t + "=").length);
  224. // s.indexOf("&") > -1 && (s = s.substring(0, s.indexOf("&")));
  225. // s.indexOf("#") > -1 && (s = s.substring(0, s.indexOf("#")));
  226. // return s;
  227. // } else return false;
  228. // } else return window.location.search.match("&" + t + "|\\?" + t) != null; //window.location.href.substring(window.location.href.indexOf("?") + 1).indexOf('&' + t) > -1;
  229. //const value = window.location.search.match("&" + t + "|\\?" + t)
  230. if (key === 'm' && window.__ProjectNum && window.__ProjectNum != '__ProjectNum__') {
  231. return window.__ProjectNum
  232. }
  233. let querys = window.location.search.substr(1).split('&')
  234. if (isGetValue) {
  235. for (let i = 0; i < querys.length; i++) {
  236. let keypair = querys[i].split('=')
  237. if (keypair.length === 2 && keypair[0] === key) {
  238. return keypair[1]
  239. }
  240. }
  241. return ''
  242. } else {
  243. //return window.location.search.match("&" + key + "|\\?" + key) != null 有bug
  244. for (let i = 0; i < querys.length; i++) {
  245. let keypair = querys[i].split('=')
  246. if (keypair[0] == key) {
  247. return true
  248. }
  249. }
  250. return false
  251. }
  252. },
  253. urlHasValueFromUrl: function (paraName, url) {
  254. // 如果url中有特殊字符则需要进行一下解码
  255. url = decodeURI(url)
  256. var arrObj = url.split("?");
  257. if (arrObj.length > 1) {
  258. var arrPara = arrObj[1].split("&");
  259. var arr;
  260. for (var i = 0; i < arrPara.length; i++) {
  261. arr = arrPara[i].split("=");
  262. if (arr != null && arr[0] == paraName) {
  263. return decodeURIComponent(arr[1]);
  264. }
  265. }
  266. return "";
  267. } else {
  268. return "";
  269. }
  270. },
  271. /**
  272. * 获取查询参数的值
  273. * @param {String} key
  274. * @returns String
  275. */
  276. urlQueryValue(key) {
  277. return this.urlHasValue(key, true) || ''
  278. },
  279. /**
  280. * 获取hash参数的值
  281. * @param {String} key
  282. * @returns String
  283. */
  284. urlHashValue(key) {
  285. let querys = window.location.hash.substr(1).replace('/?', '').split('&')
  286. for (let i = 0; i < querys.length; i++) {
  287. let keypair = querys[i].split('=')
  288. if (keypair.length === 2 && keypair[0] === key) {
  289. return keypair[1]
  290. }
  291. }
  292. return ''
  293. },
  294. /**
  295. * 判断是否存在hash
  296. * @param {String} key
  297. * @returns Boolean
  298. */
  299. urlIsHasHash(key) {
  300. let querys = window.location.hash.substr(1).replace('/?', '').split('&')
  301. return querys.includes(key)
  302. },
  303. islongPhone: function () {
  304. //是否是刘海全面屏幕 仅仅根据比例判断 - -
  305. //screen.height == 812 && screen.width == 375)
  306. var r = screen.height / screen.width //可能横屏
  307. return this.isMobile() && (r > 1.99 || r < 0.502512) //18/9=2.165 //???
  308. },
  309. detectWeixin: function () {
  310. //微信 包括PC的微信
  311. return window.navigator.userAgent.toLowerCase().match(/MicroMessenger/i) == 'micromessenger'
  312. },
  313. detectWeixinMiniProgram: function () {
  314. return window.navigator.userAgent.match('miniProgram')
  315. },
  316. detectAlipay: function () {
  317. //阿里系
  318. return window.navigator.userAgent.toLowerCase().match(/Alipay/i) == 'alipay'
  319. },
  320. detectEdge: function () {
  321. return window.navigator.userAgent.indexOf("Edge") > -1;
  322. },
  323. detectTT: function () {
  324. //字节跳动
  325. return window.navigator.userAgent
  326. .toLowerCase()
  327. .includes("toutiaomicroapp");
  328. },
  329. detectTcTravel: function () {
  330. return window.navigator.userAgent
  331. .toLowerCase()
  332. .includes("tctravel");
  333. },
  334. detectDingDing: function () {
  335. return window.navigator.userAgent
  336. .toLowerCase()
  337. .includes("dingtalk");
  338. },
  339. detectApp: function () {
  340. return this.urlHasValue('app')
  341. },
  342. /**
  343. * 判断标签页是否切换状态
  344. */
  345. isTabHidden: function () {
  346. var prefixes = ['webkit', 'moz', 'ms', 'o']
  347. if ('hidden' in document) return document.hidden
  348. for (var i = 0; i < prefixes.length; i++) {
  349. if (prefixes[i] + 'Hidden' in document) return document[prefixes[i] + 'Hidden']
  350. }
  351. return false
  352. },
  353. hasURLParam: function (key) {
  354. let querys = window.location.search.substring(1).split('&')
  355. for (let i = 0; i < querys.length; i++) {
  356. let keypair = querys[i].split('=')
  357. if (keypair[0] == key) {
  358. return true
  359. }
  360. }
  361. return false
  362. },
  363. getURLParam: function (key) {
  364. let querys = window.location.search.substring(1).split('&')
  365. for (let i = 0; i < querys.length; i++) {
  366. let keypair = querys[i].split('=')
  367. if (keypair.length === 2 && keypair[0] === key) {
  368. return keypair[1]
  369. }
  370. }
  371. return ''
  372. },
  373. }
  374. export default browser