browser.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. function mobileVersion(e, t) { //ios的版本
  2. var i = window.navigator.userAgent,
  3. n = i.match(e);
  4. return n = n ? n[1].split(t) : [],
  5. {
  6. major: parseInt(n[0]) || 0,
  7. minor: parseInt(n[1]) || 0,
  8. patch: parseInt(n[2]) || 0
  9. }
  10. }
  11. var browser = {
  12. isFullscreen: function() {
  13. return document.fullscreenElement || document.mozFullscreenElement || document.mozFullScreenElement || document.webkitFullscreenElement || document.msFullscreenElement
  14. },
  15. supportsFullscreen: function() {
  16. return document.fullscreenEnabled || document.mozFullscreenEnabled || document.mozFullScreenEnabled || document.webkitFullscreenEnabled || document.msFullscreenEnabled
  17. },
  18. isPointerLocked: function() {
  19. return document.pointerLockElement || document.mozPointerLockElement || document.webkitPointerLockElement
  20. },
  21. requestFullscreen: function(dom, t) {
  22. dom.requestFullscreen ? dom.requestFullscreen() : dom.mozRequestFullScreen ? dom.mozRequestFullScreen() : dom.webkitRequestFullscreen ? dom.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT) : dom.msRequestFullscreen && dom.msRequestFullscreen(),
  23. t && $(document).on("fullscreenchange webkitfullscreenchange mozfullscreenchange MSFullscreenChange", browser.requestPointerLock)
  24. },
  25. requestPointerLock: function() {
  26. var e;
  27. if (document.fullscreenElement)
  28. e = document.fullscreenElement();
  29. else if (document.mozFullscreenElement)
  30. e = document.mozFullscreenElement();
  31. else if (document.mozFullScreenElement)
  32. e = document.mozFullScreenElement();
  33. else {
  34. if (!document.webkitFullscreenElement)
  35. return;
  36. e = document.webkitFullscreenElement()
  37. }
  38. e.requestPointerLock = e.requestPointerLock || e.mozRequestPointerLock || e.webkitRequestPointerLock,
  39. e.requestPointerLock(),
  40. $(document).off("fullscreenchange webkitfullscreenchange mozfullscreenchange MSFullscreenChange", this)
  41. },
  42. exitPointerLock: function() {
  43. document.exitPointerLock = document.exitPointerLock || document.mozExitPointerLock || document.webkitExitPointerLock,
  44. document.exitPointerLock()
  45. },
  46. exitFullscreen: function() {
  47. document.exitFullscreen ? document.exitFullscreen() : document.msExitFullscreen ? document.msExitFullscreen() : document.mozCancelFullScreen ? document.mozCancelFullScreen() : document.webkitExitFullscreen && document.webkitExitFullscreen()
  48. },
  49. details: function() {
  50. var e = navigator.userAgent.match("(Firefox|Chrome|Safari)/([\\d]+)");
  51. return e ? {
  52. name: e[1],
  53. version: parseInt(e[2]),
  54. platform: navigator.platform
  55. } : {}
  56. },
  57. is: function(e) {
  58. return this.details() && this.details().name === e
  59. },
  60. inIframe: function() {
  61. return window.parent !== window
  62. },
  63. aspectRatio: function($elem) {
  64. $elem = $elem || $('#player')
  65. var e = $elem.width() / $elem.height();
  66. return isFinite(e) ? e : 0
  67. },
  68. userAgent: function() {
  69. return window.navigator.userAgent
  70. },
  71. isMobile: function() {
  72. var e = navigator.userAgent || navigator.vendor || window.opera;
  73. return /(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(e) || /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(e.substr(0, 4))
  74. },
  75. isLandscape: function() {
  76. return this.isMobile && this.aspectRatio() > 1
  77. },
  78. isSmallScreen: function() {
  79. var e = screen.width / window.devicePixelRatio;
  80. return e < 240
  81. },
  82. detectIE: function() {
  83. var e = window.navigator.userAgent,
  84. t = e.indexOf("MSIE ");
  85. return t !== -1 || !!navigator.userAgent.match(/Trident.*rv\:11\./)
  86. },
  87. detectSafari: function() {
  88. var e = window.navigator.userAgent,
  89. t = e.indexOf("Safari");
  90. return t !== -1 && !this.detectOpera() && !this.detectChrome() //xzw add detectOpera
  91. },
  92. detectFirefox: function() {
  93. var e = window.navigator.userAgent;
  94. return e.indexOf("Firefox") !== -1
  95. },
  96. detectChrome: function() {
  97. var e = window.navigator.userAgent;
  98. return e.indexOf("Chrome") !== -1 && !this.detectOpera()
  99. },
  100. detectOpera: function() {
  101. var e = window.navigator.userAgent;
  102. return e.indexOf("OPR") !== -1
  103. },
  104. detectIOS: function() {
  105. return this.detectIPhone() || this.detectIPad() || this.detectIPod()
  106. },
  107. detectIPad: function() {
  108. var e = window.navigator.userAgent,
  109. t = /iPad/;
  110. return t.test(e)
  111. },
  112. detectIPod: function() {
  113. var e = window.navigator.userAgent,
  114. t = /iPod/;
  115. return t.test(e)
  116. },
  117. detectIPhone: function() {
  118. var e = window.navigator.userAgent,
  119. t = /iPhone/;
  120. return t.test(e)
  121. },
  122. detectAndroid: function() {
  123. var e = window.navigator.userAgent;
  124. return e.indexOf("Android") !== -1
  125. },
  126. detectAndroidMobile: function() {
  127. var e = window.navigator.userAgent;
  128. return this.detectAndroid() && e.indexOf("Mobile") !== -1
  129. },
  130. detectSamsungNative: function() {
  131. var e = window.navigator.userAgent;
  132. return e.indexOf("SM-G900H") !== -1 || e.indexOf("GT-I9500") !== -1 || e.indexOf("SM-N900") !== -1
  133. },
  134. detectSamsungS6: function() {
  135. var e = window.navigator.userAgent;
  136. return e.indexOf("SM-G92") !== -1
  137. },
  138. /************************************************************徐世廷*************************************************************/
  139. detectHUAWEI5X: function() {
  140. return -1 !== window.navigator.userAgent.indexOf("KIW-TL00H")
  141. },
  142. /*******************************************************************************************************************************/
  143. detectWebVR: function() {
  144. return !(!window.navigator.getVRDisplays || !window.VRDisplay)
  145. },
  146. getVRDisplay: function() {
  147. var e = $.Deferred();
  148. return this.detectWebVR() ? (navigator.getVRDisplays().then(function(t) {
  149. t.length >= 1 && e.resolve(t[0]),
  150. e.reject(null)
  151. }),
  152. e) : e.reject(null)
  153. },
  154. iosVersion: function() {
  155. if (!this.detectIOS())
  156. throw new DeviceMismatchException("Did not detect an iDevice");
  157. var e = /((?:\d+\_?){1,3}) like Mac OS/,
  158. t = "_";
  159. return mobileVersion(e, t)
  160. },
  161. androidVersion: function() {
  162. if (!this.detectAndroid())
  163. throw new DeviceMismatchException("Did not detect an Android based device");
  164. var e = /Android ((?:\d+\.?){1,3})/,
  165. t = ".";
  166. return mobileVersion(e, t)
  167. },
  168. valueFromCookie: function(e, t) {
  169. var i = new RegExp(e + "=([0-9a-f]+)(; ?|$)").exec(document.cookie);
  170. if (!i)
  171. return t;
  172. var n = i[1];
  173. return "boolean" == typeof t ? "true" === n || "1" === n : "number" == typeof t ? parseFloat(n) : n
  174. },
  175. valueFromHash: function(e, t) {
  176. var i = new RegExp("[#&?]" + e + "=([^#&?]*)"),
  177. n = i.exec(window.location.href);
  178. if (!n)
  179. return t;
  180. var r = n[1];
  181. return "boolean" == typeof t ? "true" === r || "1" === r : "number" == typeof t ? parseFloat(r) : window.decodeURIComponent(r)
  182. },
  183. //-------许钟文:-------------------------------------------------
  184. getProjectNum: function() { //获取场景projectNum
  185. var number = window.location.href.substring(window.location.href.indexOf("=") + 1);
  186. if (number.indexOf("&") != -1) {
  187. number = number.substring(0, number.indexOf("&"));
  188. }
  189. if (number.indexOf("#") != -1) {
  190. number = number.substring(0, number.indexOf("#"));
  191. }
  192. return number;
  193. },
  194. urlHasValue: function(key, isGetValue) {
  195. // debugger
  196. // if (getValue) { //得到类似n=1 的 1
  197. // var b = window.location.href.substring(window.location.href.indexOf("?") + 1);
  198. // var a = b.indexOf('&' + t + "=");
  199. // if (a > -1) {
  200. // var s = b.substring(a + ('&' + t + "=").length);
  201. // s.indexOf("&") > -1 && (s = s.substring(0, s.indexOf("&")));
  202. // s.indexOf("#") > -1 && (s = s.substring(0, s.indexOf("#")));
  203. // return s;
  204. // } else return false;
  205. // } else return window.location.search.match("&" + t + "|\\?" + t) != null; //window.location.href.substring(window.location.href.indexOf("?") + 1).indexOf('&' + t) > -1;
  206. //const value = window.location.search.match("&" + t + "|\\?" + t)
  207. let querys = window.location.search.substr(1).split('&')
  208. if (isGetValue) {
  209. for (let i = 0; i < querys.length; i++) {
  210. let keypair = querys[i].split('=')
  211. if (keypair.length === 2 && keypair[0] === key) {
  212. return keypair[1]
  213. }
  214. }
  215. return ''
  216. } else {
  217. //return window.location.search.match("&" + key + "|\\?" + key) != null 有bug
  218. for (let i = 0; i < querys.length; i++) {
  219. let keypair = querys[i].split('=')
  220. if(keypair[0] == key){
  221. return true
  222. }
  223. }
  224. return false
  225. }
  226. },
  227. urlHasValueFromUrl: function(paraName,url) {
  228. // 如果url中有特殊字符则需要进行一下解码
  229. url = decodeURI(url)
  230. var arrObj = url.split("?");
  231. if (arrObj.length > 1) {
  232. var arrPara = arrObj[1].split("&");
  233. var arr;
  234. for (var i = 0; i < arrPara.length; i++) {
  235. arr = arrPara[i].split("=");
  236. if (arr != null && arr[0] == paraName) {
  237. return decodeURIComponent(arr[1]);
  238. }
  239. }
  240. return "";
  241. } else {
  242. return "";
  243. }
  244. },
  245. /**
  246. * 获取查询参数的值
  247. * @param {String} key
  248. * @returns String
  249. */
  250. urlQueryValue(key) {
  251. return this.urlHasValue(key, true) || ''
  252. },
  253. islongPhone: function() { //是否是刘海全面屏幕 仅仅根据比例判断 - -
  254. //screen.height == 812 && screen.width == 375)
  255. var r = screen.height / screen.width; //可能横屏
  256. return this.isMobile() && (r > 1.99 || r < 0.502512); //18/9=2.165 //???
  257. },
  258. detectWeixin: function() { //微信 包括PC的微信
  259. return window.navigator.userAgent.toLowerCase().match(/MicroMessenger/i) == 'micromessenger';
  260. },
  261. detectWeixinMiniProgram: function() {
  262. return window.navigator.userAgent.match('miniProgram')
  263. },
  264. detectEdge: function() {
  265. return window.navigator.userAgent.indexOf("Edge") > -1;
  266. },
  267. /**
  268. * 判断标签页是否切换状态
  269. */
  270. isTabHidden: function() {
  271. var prefixes = ['webkit', 'moz', 'ms', 'o'];
  272. if ('hidden' in document) return document.hidden;
  273. for (var i = 0; i < prefixes.length; i++) {
  274. if ((prefixes[i] + 'Hidden') in document)
  275. return document[prefixes[i] + 'Hidden']
  276. }
  277. return false;
  278. },
  279. CloneObject: function (copyObj, result) {
  280. //复制json result的可能:普通数字或字符串、普通数组、复杂对象
  281. result = result || {};
  282. if (copyObj instanceof Array) {
  283. if (copyObj[0] instanceof Object) {
  284. //不支持含有 [[Object]] 这样二级数组里面还是复杂数据的,普通和复杂的数据混合可能也不支持
  285. result = [];
  286. } else
  287. return copyObj.slice(0);
  288. //如果是数组,直接复制返回(排除数组内是object
  289. }
  290. for (var key in copyObj) {
  291. if (copyObj[key] instanceof Object)
  292. result[key] = this.CloneObject(copyObj[key]);
  293. else
  294. result[key] = copyObj[key];
  295. //如果是函数类同基本数据,即复制引用
  296. }
  297. return result;
  298. }
  299. }
  300. export default browser