UAUtils.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. import { UAParser } from 'ua-parser-js';
  2. // Constants for browsers
  3. const isAndroid=()=>{
  4. try {
  5. return window.progressier.detection.isAndroid();
  6. } catch (t) {
  7. return false
  8. }
  9. }
  10. const isIOS=()=>{
  11. try {
  12. return window.progressier.detection.isIOS();
  13. } catch (t) {
  14. return false
  15. }
  16. }
  17. const isMacOs=()=>{
  18. try {
  19. return window.progressier.detection.isMacOs();
  20. } catch (t) {
  21. return false
  22. }
  23. }
  24. const isWindows=()=>{
  25. try {
  26. return window.progressier.detection.isWindows();
  27. } catch (e) {
  28. return false
  29. }
  30. }
  31. const isSafari=()=>{
  32. try {
  33. return window.progressier.detection.isSafari();
  34. } catch (e) {
  35. return false
  36. }
  37. }
  38. const isFirefox=()=>{
  39. try {
  40. return window.progressier.detection.isFirefox();
  41. } catch (n) {
  42. return false
  43. }
  44. }
  45. const isSamsungInternet=()=>{
  46. try {
  47. return window.progressier.detection.isSamsungInternet();
  48. } catch (e) {
  49. return false
  50. }
  51. }
  52. const isChromeEdgeOnMacOs=()=>{
  53. try {
  54. return window.progressier.detection.isChromeEdgeOnMacOs();
  55. } catch (e) {
  56. return false
  57. }
  58. }
  59. const isArc=()=>{
  60. try {
  61. return window.progressier.detection.isArc();
  62. } catch (e) {
  63. return false
  64. }
  65. }
  66. const isEdge=()=>{
  67. try {
  68. return window.progressier.detection.isEdge();
  69. } catch (e) {
  70. return false
  71. }
  72. }
  73. const isChrome=()=>{
  74. try {
  75. return window.progressier.detection.isChrome();
  76. } catch (r) {
  77. return false
  78. }
  79. }
  80. const isOpera=()=>{
  81. try {
  82. return window.progressier.detection.isOpera();
  83. } catch (e) {
  84. return false
  85. }
  86. }
  87. const supportsNativeiOSPush=()=>{
  88. try {
  89. return window.progressier.detection.supportsNativeiOSPush();
  90. }
  91. catch (e){
  92. return false;
  93. }
  94. }
  95. const appstore=()=>{
  96. return supportsNativeiOSPush()
  97. }
  98. const supportsPush=()=>{
  99. try {
  100. return window.progressier.detection.supportsPush();
  101. }
  102. catch (e){
  103. return false;
  104. }
  105. }
  106. const isFromTwitter=()=>{
  107. try {
  108. return window.progressier.detection.isFromTwitter();
  109. }
  110. catch (e){
  111. return false;
  112. }
  113. }
  114. const getInAppBrowser=()=>{
  115. try {
  116. var t = window.navigator.userAgent;
  117. var e = ["wv", "Line/", "FBAN", "FBBV", "FBAV", "FB_IAB", "Instagram", "MicroMessenger", "Twitter", "Kakao", "KAKAO"];
  118. var i = "";
  119. e.forEach(function(e) {
  120. if (!t.includes(e)) {
  121. return
  122. }
  123. i = e;
  124. });
  125. return i;
  126. } catch (n) {
  127. return "";
  128. }
  129. }
  130. const isInAppBrowser=()=>{
  131. try {
  132. // return window.progressier.detection.isInAppBrowser();
  133. return getInAppBrowser()!=="";
  134. }
  135. catch (e){
  136. return false;
  137. }
  138. }
  139. const isInFBBrowser=()=>{
  140. try {
  141. let fbua = window.navigator.userAgent.toUpperCase();
  142. let e=["FBAN", "FBBV", "FBAV", "FB_IAB","IAB","FACEBOOK", "INSTAGRAM","WHATSAPP","TELEGRAM"];
  143. for (let i = 0; i < e.length; i++) {
  144. if (fbua.includes(e[i]))return true;
  145. }
  146. return false;
  147. }
  148. catch (e){
  149. return false;
  150. }
  151. }
  152. export { isInFBBrowser };
  153. const isChromeOS=()=>{
  154. try {
  155. return window.progressier.detection.isChromeOS();
  156. } catch (e) {
  157. return false
  158. }
  159. }
  160. const isDesktop=()=>{
  161. try {
  162. return window.progressier.detection.isDesktop();
  163. } catch (e) {
  164. return false
  165. }
  166. }
  167. const isSafariWithPushOnMacOs=()=>{
  168. try {
  169. return window.progressier.detection.isSafariWithPushOnMacOs();
  170. }
  171. catch (e){
  172. return false
  173. }
  174. }
  175. const isSafariWithInstallationMacOS=()=>{
  176. try {
  177. return window.progressier.detection.isSafariWithInstallationMacOS();
  178. } catch (a) {
  179. return false
  180. }
  181. }
  182. const canBePushPrompted=()=>{
  183. try {
  184. return window.progressier.detection.canBePushPrompted();
  185. }
  186. catch (e){
  187. return false
  188. }
  189. }
  190. // PWA 安装/启动判断需要的 isInPc:避免在桌面端走 mobile 引导
  191. const isLinux=()=>{
  192. try {
  193. return /Linux/i.test(navigator.platform || navigator.userAgent || "") && !/Android/i.test(navigator.userAgent || "");
  194. } catch (e) { return false; }
  195. }
  196. const isInPc=()=>{
  197. return isMacOs()||isWindows()||isLinux();
  198. }
  199. const isStandalone=()=>{
  200. try {
  201. return window.progressier.detection.isStandalone();
  202. }
  203. catch (e){
  204. return false
  205. }
  206. }
  207. const iosVersion=()=>{
  208. try {
  209. return window.progressier.detection.iosVersion();
  210. }
  211. catch (e){
  212. return false
  213. }
  214. }
  215. const recentiOS=()=>{
  216. try {
  217. return window.progressier.detection.recentiOS();
  218. }
  219. catch (e){
  220. return false
  221. }
  222. }
  223. const isTWA=()=>{
  224. try {
  225. return window.progressier.detection.isTWA();
  226. }
  227. catch (e){
  228. return false
  229. }
  230. }
  231. const requiresInScopeSw=()=>{
  232. try {
  233. return window.progressier.detection.requiresInScopeSw();
  234. }
  235. catch (e){
  236. return false
  237. }
  238. }
  239. const isIframe=()=>{
  240. try {
  241. return window.progressier.detection.isIframe();
  242. }
  243. catch (e){
  244. return false
  245. }
  246. }
  247. const isShowInstallBox=()=>{
  248. if (isStandalone())return false;
  249. if (isTWA())return false;
  250. return true;
  251. }
  252. const openProgressierInstall=()=>{
  253. if(window.progressier&&window.progressier.flow)window.progressier.flow.init("install",true)
  254. }
  255. const openUrlUseChrome=(url="")=>{
  256. try {
  257. if (url==null||url==="")url=window.location.href;
  258. if (global.BFP!=null&&global.BFP!==""){
  259. if (url.indexOf("?")===-1)url+="?bfp="+global.BFP;
  260. else url+="&bfp="+global.BFP;
  261. }
  262. if (isTWA())return;
  263. if (isIOS()){
  264. let r = "googlechromes://" + url.replace(/^https?:\/\//, "");
  265. window.location.href = r
  266. }
  267. else if (isAndroid()){
  268. const r = url.split(":")[0];
  269. let l = url.replace(/^https?:\/\//, "");
  270. window.location.href = "intent://".concat(l, "#Intent;scheme=").concat(r, ";package=com.android.chrome;end")
  271. }
  272. }
  273. catch (e){
  274. return false;
  275. }
  276. }
  277. const openUrlUseExternal=(url="")=>{
  278. try {
  279. if (url==null||url==="")url=window.location.href;
  280. if (isAndroid()) openUrlUseChrome(url);
  281. else window.open(url,"_blank");
  282. } catch(e){ return false; }
  283. }
  284. export {
  285. openUrlUseExternal,
  286. isAndroid,
  287. isIOS,
  288. isMacOs,
  289. isWindows,
  290. isSafari,
  291. isFirefox,
  292. isSamsungInternet,
  293. isChromeEdgeOnMacOs,
  294. isArc,
  295. isEdge,
  296. isChrome,
  297. isOpera,
  298. supportsNativeiOSPush,
  299. appstore,
  300. supportsPush,
  301. isFromTwitter,
  302. isInAppBrowser,
  303. isChromeOS,
  304. isDesktop,
  305. isSafariWithPushOnMacOs,
  306. isSafariWithInstallationMacOS,
  307. canBePushPrompted,
  308. isStandalone,
  309. iosVersion,
  310. recentiOS,
  311. isTWA,
  312. requiresInScopeSw,
  313. isIframe,
  314. isShowInstallBox,
  315. openUrlUseChrome,
  316. openProgressierInstall,
  317. getInAppBrowser,
  318. isInPc,
  319. isLinux
  320. };