@dxd_sjtu/call-client
Advanced tools
Comparing version 0.1.3 to 0.1.4
@@ -0,1 +1,6 @@ | ||
<a name="0.1.4"></a> | ||
## [0.1.4](https://github.com/ahungrynoob/call-client/compare/v0.1.3...v0.1.4) (2019-07-22) | ||
<a name="0.1.3"></a> | ||
@@ -5,3 +10,8 @@ ## [0.1.3](https://github.com/ahungrynoob/call-client/compare/v0.1.2...v0.1.3) (2019-07-22) | ||
### Bug Fixes | ||
* add resetDownloadTimer ([b9bef5a](https://github.com/ahungrynoob/call-client/commit/b9bef5a)) | ||
<a name="0.1.2"></a> | ||
@@ -8,0 +18,0 @@ ## [0.1.2](https://github.com/ahungrynoob/call-client/compare/v0.1.0...v0.1.2) (2019-07-02) |
@@ -12,2 +12,3 @@ declare class CallClient { | ||
callApp(): void; | ||
private resetDownloadTimer; | ||
private downloadApp; | ||
@@ -14,0 +15,0 @@ private openApp; |
@@ -23,3 +23,3 @@ "use strict"; | ||
if (!intentLink) { | ||
console.warn(intentLink, 'is null or undefined. CallClient will use deepLink instead.'); | ||
console.warn(intentLink, "is null or undefined. CallClient will use deepLink instead."); | ||
} | ||
@@ -37,2 +37,8 @@ this.deepLink = deepLink; | ||
}; | ||
CallClient.prototype.resetDownloadTimer = function () { | ||
if (this.downloadTimer) { | ||
window.clearTimeout(this.downloadTimer); | ||
this.downloadTimer = null; | ||
} | ||
}; | ||
CallClient.prototype.downloadApp = function (startTime) { | ||
@@ -50,5 +56,6 @@ var _this = this; | ||
// hack for ios because timeout is running in ios; | ||
_this.resetDownloadTimer(); | ||
return; | ||
} | ||
if (!startTime || (endTime - startTime) < threshold) { | ||
if (!startTime || endTime - startTime < threshold) { | ||
// real runTime less than threshold means fail to open app | ||
@@ -62,2 +69,3 @@ if (_this.osSystem.isIOS) { | ||
} | ||
_this.resetDownloadTimer(); | ||
}, waitTime); | ||
@@ -72,3 +80,3 @@ }; | ||
// https://developer.chrome.com/multidevice/android/intents | ||
var major = Number(this.browser.version.split('.')[0]); | ||
var major = Number(this.browser.version.split(".")[0]); | ||
if (major >= 25) { | ||
@@ -88,10 +96,10 @@ var appLink = this.intentLink || this.deepLink; | ||
// https://stackoverflow.com/questions/1421584/how-can-i-simulate-a-click-to-an-anchor-tag | ||
var e = document.createEvent('MouseEvents'); | ||
e.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); | ||
var a = document.querySelector('#temp-smb-link'); | ||
var e = document.createEvent("MouseEvents"); | ||
e.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); | ||
var a = document.querySelector("#temp-smb-link"); | ||
if (!a) { | ||
a = document.createElement('a'); | ||
a.setAttribute('href', url); | ||
a.setAttribute('id', 'temp-smb-link'); | ||
a.style.display = 'none'; | ||
a = document.createElement("a"); | ||
a.setAttribute("href", url); | ||
a.setAttribute("id", "temp-smb-link"); | ||
a.style.display = "none"; | ||
} | ||
@@ -104,6 +112,6 @@ document.body.appendChild(a); | ||
if (!this.iframe) { | ||
this.iframe = document.createElement('iframe'); | ||
this.iframe = document.createElement("iframe"); | ||
this.iframe.id = "callapp_iframe_" + Date.now(); | ||
this.iframe.frameBorder = '0'; | ||
this.iframe.style.cssText = 'display:none;border:0;width:0;height:0;'; | ||
this.iframe.frameBorder = "0"; | ||
this.iframe.style.cssText = "display:none;border:0;width:0;height:0;"; | ||
document.body.appendChild(this.iframe); | ||
@@ -110,0 +118,0 @@ } |
{ | ||
"name": "@dxd_sjtu/call-client", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "A lib to help to call a app", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
223
src/index.ts
// reference: https://suanmei.github.io/2018/08/23/h5_call_app/ | ||
import getBrowser from './getBrowser'; | ||
import getOsSystem from './getOsSystem'; | ||
import getBrowser from "./getBrowser"; | ||
import getOsSystem from "./getOsSystem"; | ||
class CallClient { | ||
private osSystem = getOsSystem(); | ||
private browser = getBrowser(); | ||
private deepLink = ""; | ||
private intentLink = ""; | ||
private iosStore = ""; | ||
private androidStore = ""; | ||
private iframe:(HTMLIFrameElement | null) = null; | ||
private downloadTimer:(number | null) = null; | ||
private osSystem = getOsSystem(); | ||
private browser = getBrowser(); | ||
private deepLink = ""; | ||
private intentLink = ""; | ||
private iosStore = ""; | ||
private androidStore = ""; | ||
private iframe: HTMLIFrameElement | null = null; | ||
private downloadTimer: number | null = null; | ||
constructor(deepLink:string,intentLink:string,iosStore:string,androidStore:string){ | ||
if(!iosStore && !androidStore){ | ||
throw new Error(`please check your CallClient class constructor's args, you must assign a iosStore or androidStore`); | ||
} | ||
if(!deepLink){ | ||
throw new Error(`please check your CallClient class constructor's args, deepLink is null or undefined.`); | ||
} | ||
if(!intentLink){ | ||
console.warn(intentLink,'is null or undefined. CallClient will use deepLink instead.'); | ||
} | ||
this.deepLink = deepLink; | ||
this.intentLink = intentLink; | ||
this.iosStore = iosStore; | ||
this.androidStore = androidStore; | ||
this.callApp = this.callApp.bind(this); | ||
constructor( | ||
deepLink: string, | ||
intentLink: string, | ||
iosStore: string, | ||
androidStore: string | ||
) { | ||
if (!iosStore && !androidStore) { | ||
throw new Error( | ||
`please check your CallClient class constructor's args, you must assign a iosStore or androidStore` | ||
); | ||
} | ||
public callApp(){ | ||
this.openApp(); | ||
const startTime = Date.now(); | ||
this.downloadApp(startTime); | ||
if (!deepLink) { | ||
throw new Error( | ||
`please check your CallClient class constructor's args, deepLink is null or undefined.` | ||
); | ||
} | ||
if (!intentLink) { | ||
console.warn( | ||
intentLink, | ||
"is null or undefined. CallClient will use deepLink instead." | ||
); | ||
} | ||
this.deepLink = deepLink; | ||
this.intentLink = intentLink; | ||
this.iosStore = iosStore; | ||
this.androidStore = androidStore; | ||
this.callApp = this.callApp.bind(this); | ||
} | ||
private downloadApp(startTime:number){ | ||
if(this.downloadTimer){ | ||
return; | ||
} | ||
const waitTime = 1500; | ||
this.downloadTimer = setTimeout(()=>{ | ||
const endTime = Date.now(); | ||
const threshold = waitTime + 300; | ||
const hide = document.hidden || (document as any).webkitHidden; | ||
if(hide){ | ||
// hack for ios because timeout is running in ios; | ||
return; | ||
} | ||
public callApp() { | ||
this.openApp(); | ||
const startTime = Date.now(); | ||
this.downloadApp(startTime); | ||
} | ||
if(!startTime || (endTime - startTime) < threshold){ | ||
// real runTime less than threshold means fail to open app | ||
if(this.osSystem.isIOS){ | ||
this.redirectLocation(this.iosStore || this.androidStore); | ||
}else{ | ||
this.redirectLocation(this.androidStore || this.iosStore); | ||
} | ||
} | ||
},waitTime) | ||
private resetDownloadTimer() { | ||
if (this.downloadTimer) { | ||
window.clearTimeout(this.downloadTimer); | ||
this.downloadTimer = null; | ||
} | ||
} | ||
private openApp(){ | ||
if(this.osSystem.isIOS){ | ||
this.userAnchorLink(this.deepLink); | ||
}else if(this.browser.isChrome){ | ||
// when version >= 25 use intentLink | ||
// https://developer.chrome.com/multidevice/android/intents | ||
const major = Number(this.browser.version.split('.')[0]); | ||
if(major >= 25){ | ||
const appLink = this.intentLink || this.deepLink; | ||
this.redirectLocation(appLink); | ||
}else{ | ||
this.redirectLocation(this.deepLink); | ||
} | ||
}else{ | ||
this.callInIframe(this.deepLink); | ||
} | ||
private downloadApp(startTime: number) { | ||
if (this.downloadTimer) { | ||
return; | ||
} | ||
const waitTime = 1500; | ||
this.downloadTimer = setTimeout(() => { | ||
const endTime = Date.now(); | ||
const threshold = waitTime + 300; | ||
const hide = document.hidden || (document as any).webkitHidden; | ||
if (hide) { | ||
// hack for ios because timeout is running in ios; | ||
this.resetDownloadTimer(); | ||
return; | ||
} | ||
private userAnchorLink(url:string){ | ||
// https://stackoverflow.com/questions/1421584/how-can-i-simulate-a-click-to-an-anchor-tag | ||
const e = document.createEvent('MouseEvents'); | ||
e.initMouseEvent('click',true,true,window,0, 0, 0, 0, 0, false, false, false, false, 0, null); | ||
let a = document.querySelector('#temp-smb-link'); | ||
if(!a){ | ||
a = document.createElement('a'); | ||
a.setAttribute('href',url); | ||
a.setAttribute('id', 'temp-smb-link'); | ||
(a as HTMLAnchorElement).style.display = 'none'; | ||
if (!startTime || endTime - startTime < threshold) { | ||
// real runTime less than threshold means fail to open app | ||
if (this.osSystem.isIOS) { | ||
this.redirectLocation(this.iosStore || this.androidStore); | ||
} else { | ||
this.redirectLocation(this.androidStore || this.iosStore); | ||
} | ||
document.body.appendChild(a); | ||
a.dispatchEvent(e); | ||
return a; | ||
} | ||
this.resetDownloadTimer(); | ||
}, waitTime); | ||
} | ||
private openApp() { | ||
if (this.osSystem.isIOS) { | ||
this.userAnchorLink(this.deepLink); | ||
} else if (this.browser.isChrome) { | ||
// when version >= 25 use intentLink | ||
// https://developer.chrome.com/multidevice/android/intents | ||
const major = Number(this.browser.version.split(".")[0]); | ||
if (major >= 25) { | ||
const appLink = this.intentLink || this.deepLink; | ||
this.redirectLocation(appLink); | ||
} else { | ||
this.redirectLocation(this.deepLink); | ||
} | ||
} else { | ||
this.callInIframe(this.deepLink); | ||
} | ||
} | ||
private callInIframe(url:string){ | ||
if(!this.iframe){ | ||
this.iframe = document.createElement('iframe'); | ||
this.iframe.id = `callapp_iframe_${Date.now()}`; | ||
this.iframe.frameBorder = '0'; | ||
this.iframe.style.cssText = 'display:none;border:0;width:0;height:0;'; | ||
document.body.appendChild(this.iframe); | ||
} | ||
this.iframe.src = url; | ||
private userAnchorLink(url: string) { | ||
// https://stackoverflow.com/questions/1421584/how-can-i-simulate-a-click-to-an-anchor-tag | ||
const e = document.createEvent("MouseEvents"); | ||
e.initMouseEvent( | ||
"click", | ||
true, | ||
true, | ||
window, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
false, | ||
false, | ||
false, | ||
false, | ||
0, | ||
null | ||
); | ||
let a = document.querySelector("#temp-smb-link"); | ||
if (!a) { | ||
a = document.createElement("a"); | ||
a.setAttribute("href", url); | ||
a.setAttribute("id", "temp-smb-link"); | ||
(a as HTMLAnchorElement).style.display = "none"; | ||
} | ||
document.body.appendChild(a); | ||
a.dispatchEvent(e); | ||
return a; | ||
} | ||
private redirectLocation(url:string){ | ||
window.location.href = url; | ||
private callInIframe(url: string) { | ||
if (!this.iframe) { | ||
this.iframe = document.createElement("iframe"); | ||
this.iframe.id = `callapp_iframe_${Date.now()}`; | ||
this.iframe.frameBorder = "0"; | ||
this.iframe.style.cssText = "display:none;border:0;width:0;height:0;"; | ||
document.body.appendChild(this.iframe); | ||
} | ||
this.iframe.src = url; | ||
} | ||
private redirectLocation(url: string) { | ||
window.location.href = url; | ||
} | ||
} | ||
export default CallClient; | ||
export default CallClient; |
26205
665