-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
125 lines (121 loc) · 2.88 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* [openApp description]
* @param {[type]} options [description]
* @return {[type]} [description]
*
*
* TEST CASE:
*
* location.href : {
* ios8: {
* INSTALLED: OK
* NOT INSTALLED: ALERT ERROR
* }
* ios9: {
* INSTALLED: OK
* NOT INSTALLED: ALERT ERROR
* }
* android: {
* INSTALLED: OK
* NOT INSTALLED: REDIRECT TO ERROR PAGE
* }
* }
*
* iframe: {
* ios8: {
* INSTALLED: ok
* NOT INSTALLED: nothing
* }
* ios9: {
* INSTALLED: nothing
* NOT INSTALLED: nothing
* }
* android: {
* INSTALLED: OK
* NOT INSTALLED: nothing
* }
* }
*
*/
function openApp(options){
if(typeof options === 'undefined'){
options = {};
}
if(typeof options.schema !== 'undefined'){
throw new TypeError('Are you typo? do you want `options.scheme` ?');
}
var ua = navigator.userAgent;
var isChrome = /chrome/i.test(ua);
var isSamsung = /samsung/i.test(ua);
var os = ((/iphone|android/i.exec(ua) || [ 'unknow' ])[0]).toLowerCase();
var version = parseInt((/version\/(\d\.\d)/i.exec(ua) || [ 0 ])[1], 10);
/**
* [createIFrame description]
* @param {[type]} src [description]
* @return {[type]} [description]
*/
function createIFrame(src){
var iframe = document.createElement('iframe');
iframe.src = src;
document.body.appendChild(iframe);
document.body.removeChild(iframe);
}
/**
* Android Intents with Chrome
* see docs https://developer.chrome.com/multidevice/android/intents
*
* intent://
* scan/
* #Intent;
* package=com.google.zxing.client.android;
* scheme=meituanmovie;
* end;
*/
function buildIntent(url, pkg){
var scheme, action = url.replace(/^(\w+):\/\//, function(_, m){
scheme = m;
return '';
});
var o = { scheme: scheme, package: pkg };
var meta = Object.keys(o).map(function(key){
return [ key, o[ key ] ].join('=');
}).map(function(part){
return part + ';';
}).join('');
//
return 'intent://' + action + '#Intent;' + meta + 'end;';
};
/**
* [open description]
* @return {[type]} [description]
*/
return function open(){
/**
* android support iframe call scheme (NOT include SAMSUNG devices),
* iOS9 and later is no longer support iframe call scheme.
*/
if(os == 'android' || (os == 'iphone' && version < 9)){
if(isSamsung){
location.href = buildIntent(
options.scheme,
options.package
);
}else{
createIFrame(options.scheme);
}
}else{
// iOS8 and earlier .
location.href = options.scheme;
}
};
};
// expose `openApp` object
if (typeof define === 'function' && define.amd) {
define([], function() {
return openApp;
});
} else if (typeof module === 'object' && module.exports) {
module.exports = openApp;
} else {
this.openApp = openApp;
}