Created
July 29, 2015 07:02
-
-
Save mindon/d78d0c11e73893cacda8 to your computer and use it in GitHub Desktop.
launch a mobile app from mobile web or inside wechat, or goes to app website to download
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;// to launch mobile app | |
| var insideWeChat = typeof window.WeixinJSBridge == "object"; | |
| var isMobileDevice = 'ontouchstart' in window || /Android|webOS|iPhone|iPad|iPod|BlackBerry|BB10|IEMobile|Opera Mini/i.test(navigator.userAgent); | |
| ;(function(document, window){ // | |
| var ua = navigator.userAgent; | |
| var tid, callfn, duration = 1300; | |
| function bind(el, en, fn, useCapture){ | |
| el.addEventListener ? el.addEventListener(en, fn, useCapture||false): (el.attachEvent ? el.attachEvent('on'+en, fn) : el['on'+en]=fn); | |
| } | |
| function unbind(el, en, fn, useCapture){ | |
| el.removeEventListener ? el.removeEventListener(en, fn, useCapture||false): (el.detachEvent ? el.detachEvent('on'+en, fn) : el['on'+en]=null); | |
| } | |
| var inWechat = insideWeChat; | |
| bind(document, 'WeixinJSBridgeReady', function(){ | |
| inWechat = insideWeChat = true; | |
| }); | |
| var byId = 'getElementById'; | |
| var crEl = 'createElement'; | |
| var appScheme = 'MyApp://'; | |
| var appPackageID = '999999999'; // ios | |
| var appPackageName = 'app.mindon.lab'; // android | |
| var appSite = 'http://mindon.github.com'; | |
| var GooglePlayURL = "https://play.google.com/store/apps/details?id="; | |
| if(/^zh-CN$/i.test(navigator.language)) { | |
| GooglePlayURL = 'http://sj.qq.com/myapp/detail.htm?apkName='; | |
| } | |
| var iOSAppStoreURL = "https://itunes.apple.com/app/id"; | |
| var current = {scheme: appScheme, id: appPackageID, name: appPackageName, site: appSite }; | |
| var mylauncher = document[byId]('app::launcher'); | |
| if(!mylauncher) { | |
| mylauncher = document[crEl]('div'); | |
| mylauncher.style.display = 'none'; | |
| mylauncher.id = 'app::launcher'; | |
| if(document.body) { | |
| document.body.insertBefore(mylauncher, document.body.firstChild); | |
| } | |
| } | |
| function invisible(e) { | |
| tid && clearTimeout(tid); | |
| clean(e, false); | |
| } | |
| function clean(e, expired) { | |
| if( expired ) { | |
| // no app or expired | |
| callfn && callfn(false); | |
| } else { | |
| // success | |
| callfn && callfn(true); | |
| } | |
| unbind(window, 'pagehide', invisible, true); | |
| unbind(window, 'pageshow', invisible, true); | |
| if(tid) clearTimeout(tid); | |
| mylauncher.innerHTML = ''; | |
| } | |
| function visit(u) { | |
| window.location.href = u; | |
| } | |
| function launch(scheme, cb) { | |
| if( mylauncher && mylauncher.childNodes.length > 0 ) | |
| return false; | |
| if(!isMobileDevice) { | |
| return cb(false); | |
| } | |
| bind(window, 'pagehide', invisible, true); | |
| bind(window, 'pageshow', invisible, true); | |
| callfn = cb; | |
| var ios = /iPhone|iPad|iPod/i.test(ua); | |
| if( inWechat ) { | |
| visit(scheme); | |
| } else if(ios || window.chrome) { | |
| ios && setTimeout(function() { | |
| if (!document.webkitHidden) | |
| clean(null, true); | |
| else | |
| clean(null, false); | |
| }, 50); | |
| visit(scheme); | |
| //} else if(window.chrome && /android/i.test(ua)) { // failed | |
| // visit('intent://scan/#Intent;scheme='+scheme.replace(/:\/\/.*/,'')+';package='+current.name+';end'); | |
| } else { | |
| var fr = document[crEl]('iframe'); | |
| fr.onload = function(){ | |
| fr.onload = null; | |
| fr.parentNode.removeChild(fr); | |
| fr = null; | |
| clean(null, false); | |
| }; | |
| fr.src = scheme; | |
| mylauncher.appendChild(fr); | |
| } | |
| var curr = +new Date(); | |
| if(tid) clearTimeout(tid); | |
| tid = setTimeout(function(){ | |
| tid = setTimeout(function(){ | |
| if(+new Date() - curr > duration){ | |
| clean(null, false); | |
| } else { | |
| clean(null, true); | |
| } | |
| }, duration -100); | |
| }, 80); | |
| } | |
| function gonext(ok, from) { | |
| if(!ok) { | |
| if( /Android|BlackBerry|BB10/i.test(ua) ) { | |
| visit(GooglePlayURL + current.name); | |
| } else { | |
| visit(iOSAppStoreURL + current.id); | |
| } | |
| } else if(from == 'wechat') { | |
| launch(current.scheme, gonext); | |
| } | |
| } | |
| function appup(scheme, iOsAppId, packageName, site) { | |
| if(scheme) current.scheme = scheme; | |
| if(iOsAppId) current.id = iOsAppId; | |
| if(packageName) current.name = packageName; | |
| if(site) current.site = site; | |
| if(!inWechat) inWechat = insideWeChat = typeof window.WeixinJSBridge == "object"; | |
| if(inWechat) { | |
| WeixinJSBridge.invoke("getInstallState", { | |
| "packageUrl": scheme, | |
| "packageName": packageName | |
| }, function(res) { | |
| var err = (res.err_msg || ':').split(':')[1], | |
| norxp = /^(no|not_allow|access_denied)$/i; | |
| if (err && !norxp.test(err)) { | |
| gonext(true, 'wechat'); | |
| } else { | |
| if(err == 'not_allow' || err == 'access_denied') { | |
| visit(current.site); | |
| } else { | |
| gonext(false, 'wechat'); | |
| } | |
| } | |
| }); | |
| } else { | |
| launch(scheme, gonext); | |
| } | |
| } | |
| if(!window.appup) // export active a mobile app | |
| window.appup = appup; | |
| //--- | |
| })(document, window); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment