browser.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 (
  17. document.fullscreenElement ||
  18. document.mozFullscreenElement ||
  19. document.mozFullScreenElement ||
  20. document.webkitFullscreenElement ||
  21. document.msFullscreenElement
  22. );
  23. },
  24. supportsFullscreen: function () {
  25. return (
  26. document.fullscreenEnabled ||
  27. document.mozFullscreenEnabled ||
  28. document.mozFullScreenEnabled ||
  29. document.webkitFullscreenEnabled ||
  30. document.msFullscreenEnabled
  31. );
  32. },
  33. isPointerLocked: function () {
  34. return document.pointerLockElement || document.mozPointerLockElement || document.webkitPointerLockElement;
  35. },
  36. requestFullscreen: function (dom, t) {
  37. dom.requestFullscreen
  38. ? dom.requestFullscreen()
  39. : dom.mozRequestFullScreen
  40. ? dom.mozRequestFullScreen()
  41. : dom.webkitRequestFullscreen
  42. ? dom.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT)
  43. : dom.msRequestFullscreen && dom.msRequestFullscreen(),
  44. t && $(document).on("fullscreenchange webkitfullscreenchange mozfullscreenchange MSFullscreenChange", browser.requestPointerLock);
  45. },
  46. requestPointerLock: function () {
  47. var e;
  48. if (document.fullscreenElement) e = document.fullscreenElement();
  49. else if (document.mozFullscreenElement) e = document.mozFullscreenElement();
  50. else if (document.mozFullScreenElement) e = document.mozFullScreenElement();
  51. else {
  52. if (!document.webkitFullscreenElement) return;
  53. e = document.webkitFullscreenElement();
  54. }
  55. (e.requestPointerLock = e.requestPointerLock || e.mozRequestPointerLock || e.webkitRequestPointerLock),
  56. e.requestPointerLock(),
  57. $(document).off("fullscreenchange webkitfullscreenchange mozfullscreenchange MSFullscreenChange", this);
  58. },
  59. exitPointerLock: function () {
  60. (document.exitPointerLock = document.exitPointerLock || document.mozExitPointerLock || document.webkitExitPointerLock), document.exitPointerLock();
  61. },
  62. exitFullscreen: function () {
  63. document.exitFullscreen
  64. ? document.exitFullscreen()
  65. : document.msExitFullscreen
  66. ? document.msExitFullscreen()
  67. : document.mozCancelFullScreen
  68. ? document.mozCancelFullScreen()
  69. : document.webkitExitFullscreen && document.webkitExitFullscreen();
  70. },
  71. details: function () {
  72. var e = navigator.userAgent.match("(Firefox|Chrome|Safari)/([\\d]+)");
  73. return e
  74. ? {
  75. name: e[1],
  76. version: parseInt(e[2]),
  77. platform: navigator.platform,
  78. }
  79. : {};
  80. },
  81. is: function (e) {
  82. return this.details() && this.details().name === e;
  83. },
  84. inIframe: function () {
  85. return window.parent !== window;
  86. },
  87. aspectRatio: function ($elem) {
  88. $elem = $elem || $("#player");
  89. var e = $elem.width() / $elem.height();
  90. return isFinite(e) ? e : 0;
  91. },
  92. userAgent: function () {
  93. return window.navigator.userAgent;
  94. },
  95. isMobile: function () {
  96. var e = navigator.userAgent || navigator.vendor || window.opera;
  97. return (
  98. /(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(
  99. e
  100. ) ||
  101. /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(
  102. e.substr(0, 4)
  103. )
  104. );
  105. },
  106. isLandscape: function () {
  107. return this.isMobile && this.aspectRatio() > 1;
  108. },
  109. isSmallScreen: function () {
  110. var e = screen.width / window.devicePixelRatio;
  111. return e < 240;
  112. },
  113. detectIE: function () {
  114. var e = window.navigator.userAgent,
  115. t = e.indexOf("MSIE ");
  116. return t !== -1 || !!navigator.userAgent.match(/Trident.*rv\:11\./);
  117. },
  118. detectSafari: function () {
  119. var e = window.navigator.userAgent,
  120. t = e.indexOf("Safari");
  121. return t !== -1 && !this.detectOpera() && !this.detectChrome(); //xzw add detectOpera
  122. },
  123. detectFirefox: function () {
  124. var e = window.navigator.userAgent;
  125. return e.indexOf("Firefox") !== -1;
  126. },
  127. detectChrome: function () {
  128. var e = window.navigator.userAgent;
  129. return e.indexOf("Chrome") !== -1 && !this.detectOpera();
  130. },
  131. detectOpera: function () {
  132. var e = window.navigator.userAgent;
  133. return e.indexOf("OPR") !== -1;
  134. },
  135. detectIOS: function () {
  136. return this.detectIPhone() || this.detectIPad() || this.detectIPod();
  137. },
  138. detectIPad: function () {
  139. var e = window.navigator.userAgent,
  140. t = /iPad/;
  141. return t.test(e);
  142. },
  143. detectIPod: function () {
  144. var e = window.navigator.userAgent,
  145. t = /iPod/;
  146. return t.test(e);
  147. },
  148. detectIPhone: function () {
  149. var e = window.navigator.userAgent,
  150. t = /iPhone/;
  151. return t.test(e);
  152. },
  153. detectAndroid: function () {
  154. var e = window.navigator.userAgent;
  155. return e.indexOf("Android") !== -1;
  156. },
  157. detectAndroidMobile: function () {
  158. var e = window.navigator.userAgent;
  159. return this.detectAndroid() && e.indexOf("Mobile") !== -1;
  160. },
  161. detectSamsungNative: function () {
  162. var e = window.navigator.userAgent;
  163. return e.indexOf("SM-G900H") !== -1 || e.indexOf("GT-I9500") !== -1 || e.indexOf("SM-N900") !== -1;
  164. },
  165. detectSamsungS6: function () {
  166. var e = window.navigator.userAgent;
  167. return e.indexOf("SM-G92") !== -1;
  168. },
  169. /************************************************************徐世廷*************************************************************/
  170. detectHUAWEI5X: function () {
  171. return -1 !== window.navigator.userAgent.indexOf("KIW-TL00H");
  172. },
  173. /*******************************************************************************************************************************/
  174. detectWebVR: function () {
  175. return !(!window.navigator.getVRDisplays || !window.VRDisplay);
  176. },
  177. getVRDisplay: function () {
  178. var e = $.Deferred();
  179. return this.detectWebVR()
  180. ? (navigator.getVRDisplays().then(function (t) {
  181. t.length >= 1 && e.resolve(t[0]), e.reject(null);
  182. }),
  183. e)
  184. : e.reject(null);
  185. },
  186. iosVersion: function () {
  187. if (!this.detectIOS()) throw new DeviceMismatchException("Did not detect an iDevice");
  188. var e = /((?:\d+\_?){1,3}) like Mac OS/,
  189. t = "_";
  190. return mobileVersion(e, t);
  191. },
  192. androidVersion: function () {
  193. if (!this.detectAndroid()) throw new DeviceMismatchException("Did not detect an Android based device");
  194. var e = /Android ((?:\d+\.?){1,3})/,
  195. t = ".";
  196. return mobileVersion(e, t);
  197. },
  198. valueFromCookie: function (e, t) {
  199. var i = new RegExp(e + "=([0-9a-f]+)(; ?|$)").exec(document.cookie);
  200. if (!i) return t;
  201. var n = i[1];
  202. return "boolean" == typeof t ? "true" === n || "1" === n : "number" == typeof t ? parseFloat(n) : n;
  203. },
  204. valueFromHash: function (e, t) {
  205. var i = new RegExp("[#&?]" + e + "=([^#&?]*)"),
  206. n = i.exec(window.location.href);
  207. if (!n) return t;
  208. var r = n[1];
  209. return "boolean" == typeof t ? "true" === r || "1" === r : "number" == typeof t ? parseFloat(r) : window.decodeURIComponent(r);
  210. },
  211. //-------许钟文:-------------------------------------------------
  212. getProjectNum: function () {
  213. //获取场景projectNum
  214. if (window.__ProjectNum && window.__ProjectNum != "__ProjectNum__") {
  215. return window.__ProjectNum;
  216. }
  217. var number = window.location.href.substring(window.location.href.indexOf("=") + 1);
  218. if (number.indexOf("&") != -1) {
  219. number = number.substring(0, number.indexOf("&"));
  220. }
  221. if (number.indexOf("#") != -1) {
  222. number = number.substring(0, number.indexOf("#"));
  223. }
  224. return number;
  225. },
  226. urlHasValue: function (key, isGetValue) {
  227. // debugger
  228. // if (getValue) { //得到类似n=1 的 1
  229. // var b = window.location.href.substring(window.location.href.indexOf("?") + 1);
  230. // var a = b.indexOf('&' + t + "=");
  231. // if (a > -1) {
  232. // var s = b.substring(a + ('&' + t + "=").length);
  233. // s.indexOf("&") > -1 && (s = s.substring(0, s.indexOf("&")));
  234. // s.indexOf("#") > -1 && (s = s.substring(0, s.indexOf("#")));
  235. // return s;
  236. // } else return false;
  237. // } else return window.location.search.match("&" + t + "|\\?" + t) != null; //window.location.href.substring(window.location.href.indexOf("?") + 1).indexOf('&' + t) > -1;
  238. //const value = window.location.search.match("&" + t + "|\\?" + t)
  239. if (key === "m" && window.__ProjectNum && window.__ProjectNum != "__ProjectNum__") {
  240. return window.__ProjectNum;
  241. }
  242. let querys = window.location.search.substr(1).split("&");
  243. if (isGetValue) {
  244. for (let i = 0; i < querys.length; i++) {
  245. let keypair = querys[i].split("=");
  246. if (keypair.length === 2 && keypair[0] === key) {
  247. return keypair[1];
  248. }
  249. }
  250. return "";
  251. } else {
  252. //return window.location.search.match("&" + key + "|\\?" + key) != null 有bug
  253. for (let i = 0; i < querys.length; i++) {
  254. let keypair = querys[i].split("=");
  255. if (keypair[0] == key) {
  256. return true;
  257. }
  258. }
  259. return false;
  260. }
  261. },
  262. /**
  263. * 获取查询参数的值
  264. * @param {String} key
  265. * @returns String
  266. */
  267. urlQueryValue(key) {
  268. return this.urlHasValue(key, true) || "";
  269. },
  270. /**
  271. * 获取hash参数的值
  272. * @param {String} key
  273. * @returns String
  274. */
  275. urlHashValue(key) {
  276. let querys = window.location.hash.substr(1).replace("/?", "").split("&");
  277. for (let i = 0; i < querys.length; i++) {
  278. let keypair = querys[i].split("=");
  279. if (keypair.length === 2 && keypair[0] === key) {
  280. return keypair[1];
  281. }
  282. }
  283. return "";
  284. },
  285. /**
  286. * 判断是否存在hash
  287. * @param {String} key
  288. * @returns Boolean
  289. */
  290. urlIsHasHash(key) {
  291. let querys = window.location.hash.substr(1).replace("/?", "").split("&");
  292. return querys.includes(key);
  293. },
  294. islongPhone: function () {
  295. //是否是刘海全面屏幕 仅仅根据比例判断 - -
  296. //screen.height == 812 && screen.width == 375)
  297. var r = screen.height / screen.width; //可能横屏
  298. return this.isMobile() && (r > 1.99 || r < 0.502512); //18/9=2.165 //???
  299. },
  300. detectWeixin: function () {
  301. //微信 包括PC的微信
  302. return window.navigator.userAgent.toLowerCase().match(/MicroMessenger/i) == "micromessenger";
  303. },
  304. detectWeixinMiniProgram: function () {
  305. return window.navigator.userAgent.match("miniProgram");
  306. },
  307. detectEdge: function () {
  308. return window.navigator.userAgent.indexOf("Edge") > -1;
  309. },
  310. detectApp: function () {
  311. return this.urlHasValue("isMiniApp");
  312. },
  313. /**
  314. * 判断标签页是否切换状态
  315. */
  316. isTabHidden: function () {
  317. var prefixes = ["webkit", "moz", "ms", "o"];
  318. if ("hidden" in document) return document.hidden;
  319. for (var i = 0; i < prefixes.length; i++) {
  320. if (prefixes[i] + "Hidden" in document) return document[prefixes[i] + "Hidden"];
  321. }
  322. return false;
  323. },
  324. hasURLParam: function (key) {
  325. let querys = window.location.search.substring(1).split("&");
  326. for (let i = 0; i < querys.length; i++) {
  327. let keypair = querys[i].split("=");
  328. if (keypair[0] == key) {
  329. return true;
  330. }
  331. }
  332. return false;
  333. },
  334. getURLParam: function (key) {
  335. let querys = window.location.search.substring(1).split("&");
  336. for (let i = 0; i < querys.length; i++) {
  337. let keypair = querys[i].split("=");
  338. if (keypair.length === 2 && keypair[0] === key) {
  339. return keypair[1];
  340. }
  341. }
  342. return "";
  343. },
  344. replaceQueryString(url, name, value) {
  345. let re = new RegExp(name + "=[^&]*", "gi");
  346. if (!value) {
  347. let ttt = url.replace(new RegExp('&' + name + "=[^&]*", "gi"), '')
  348. return (ttt.replace(re, '')).replace("?&",'?');
  349. }
  350. return (url.replace(re, name + "=" + value)).replace("?&",'?');
  351. },
  352. openLink(mglink, h5link, appLink,pcLink) {
  353. if (this.getURLParam("isBuyerApp") === "1") {
  354. if (appLink) {
  355. wx.miniProgram.navigateTo({
  356. url: appLink,
  357. });
  358. }
  359. }
  360. if (this.detectWeixin()) {
  361. //ios的ua中无miniProgram,但都有MicroMessenger(表示是微信浏览器)
  362. wx.miniProgram.getEnv((res) => {
  363. if (res.miniprogram) {
  364. //在小程序里
  365. if (mglink) {
  366. wx.miniProgram.navigateTo({
  367. url: mglink,
  368. });
  369. }
  370. } else {
  371. if (h5link) {
  372. if (this.detectIOS()) {
  373. window.location.href = h5link;
  374. } else {
  375. window.open(h5link, "_blank");
  376. }
  377. }
  378. // 不在小程序里
  379. }
  380. });
  381. } else if (this.getURLParam("isMiniApp") === "1") {
  382. if (appLink) {
  383. wx.miniProgram.navigateTo({
  384. url: appLink,
  385. });
  386. }
  387. } else {
  388. if (this.isMobile()) {
  389. if (h5link) {
  390. if (this.detectIOS()) {
  391. window.location.href = h5link;
  392. } else {
  393. window.open(h5link, "_blank");
  394. }
  395. }
  396. }
  397. else {
  398. if (pcLink){
  399. window.location.href = pcLink;
  400. }
  401. }
  402. }
  403. },
  404. };
  405. export default browser;